栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

except自动登录的几段代码分享

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

except自动登录的几段代码分享

复制代码 代码如下:
#!/usr/bin/expect -f
set timeout 30
set host "192.168.1.198"
spawn ssh $host
expect_before "no)?" {
send "yesr" }
sleep 1
expect "password:"
send "123456r"
expect "*#"
send "echo my name is fivetrees > /root/fivetrees.txtr"
interact

##----------------------------

复制代码 代码如下:
[root@fivetrees ~]# cat expect
#!/usr/bin/expect
for {set i 10} {$i <= 12} {incr i} {
set timeout 30
set ssh_user [lindex $argv 0]
spawn ssh -i .ssh/$ssh_user abc$i.com
expect_before "no)?" {
send "yesr" }
sleep 1
expect "password*"
send "hellor"
expect "*#"
send "echo hello expect! > /tmp/expect.txtr"
expect "*#"
send "echor"
}
exit

##-------------------------

复制代码 代码如下:
#!/usr/bin/expect
if {$argc!=2} {
    send_user "usage: ./expect ssh_user passwordn"
    exit
}
foreach i {11 12} {
set timeout 30
set ssh_user [lindex $argv 0]
set password [lindex $argv 1]
spawn ssh -i .ssh/$ssh_user root@xxx.yy.com
expect_before "no)?" {
send "yesr" }
sleep 1
expect "Enter passphrase for key*"
send "passwordr"
expect "*#"
send "echo hello expect! > /tmp/expect.txtr"
expect "*#"
send "echor"
}
exit

##---------------------------

复制代码 代码如下:
#!/usr/bin/expect
set timeout 20
if {$argc < 1} {
  puts "Usage: script IP"
  exit 1
}
# 替换你自己的用户名
set user "username"
#替换你自己的登录密码
set password "yourpassword"
foreach IP  $argv {
spawn  ssh $user@$IP
expect
  "(yes/no)?" {
    send "yesr"
    expect "password:?" {
      send "$passwordr"
    }
  } "password:?" {
    send "$passwordr"
}
expect "$?"
# 替换你要执行的命令
send "lastr"
expect "$?"
sleep 10
send "exitr"
expect eof
}
使用方法
script_name   ip1  ip2  ip3 ...

##---------------------

复制代码 代码如下:
#!/bin/sh
# -*- tcl -*-
exec tclsh $0 "$@"
package require Expect
set username [lindex $argv 0]
set password [lindex $argv 1]
set argv [lrange $argv 2 end]
set prompt "(%|#|\$) $"
foreach ip $argv {
    spawn ssh -t $username@$ip sh
    lappend ids $spawn_id
}
expect_before -i ids eof {
    set index [lsearch $ids $expect_out(spawn_id)]
    set ids [lreplace $ids $index $index]
    if [llength $ids] exp_continue
}
expect -i ids "(yes/no)\?" {
    send -i $expect_out(spawn_id) yesr
    exp_continue
} -i ids "Enter passphrase for key" {
    send -i $expect_out(spawn_id) r
    exp_continue
} -i ids "assword:" {
    send -i $expect_out(spawn_id) $passwordr
    exp_continue
} -i ids -re $prompt {
    set spawn_id $expect_out(spawn_id)
    send "echo hello; exitr"
    exp_continue
} timeout {
    exit 1
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/37955.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号