expect ssh批量登录服务器

  • Post author:
  • Post category:小程序

expect批量登录服务器,操作完成退出,自动登录下一个

0x0 代码

#!/usr/bin/expect -f
set f [open passwd.txt r]
while { [gets $f line] >= 0 } {
  set timeout 600
  set host    [lindex $line 0]
  set password [lindex $line 1]
  spawn ssh root@$host
  expect {
    "yes/no" {send "yes\n";exp_continue}
    "password:" {send "$password\n"}
  }
  expect "]*"
  interact
}

0x1 passwd.txt 格式

127.0.0.1 123456
127.0.0.2 900001
127.0.0.3 290900
127.0.0.4 123456
127.0.0.5 123456

0x2 单个登录代码

#!/usr/bin/expect
set timeout 30
set host 127.0.0.1
set password  mypassword
spawn ssh -l root $host
expect {
  "yes/no" {send "yes\r";exp_continue}
  "password:" {send "$password\r"}
}

interact