打印

[转帖] 使用expect实现ssh自动交互

使用expect实现ssh自动交互

#!/bin/sh
# \
exec expect -- "$0" ${1+"$@"}
exp_version -exit 5.0
if {$argc!=2} {
send_user "usage: remote-exec command password\n"
send_user "Eg. remote-exec \"ssh [email=user@host]user@host[/email] ls\\; echo done\" password\n"
send_user "or: remote-exec \"scp /local-file [email=user@host:/remote-file%5C]user@host:/remote-file\[/email]" password\n"
send_user "or: remote-exec \"scp [email=user@host:/remote-file]user@host:/remote-file[/email] local-file\" password\n"
send_user "or: remote-exec \"rsync --rsh=ssh /local-file [email=user@host:/remote-file%5C]user@host:/remote-file\[/email]" password\n"
send_user "Caution: command should be quoted.\n"
exit
}
set cmd [lindex $argv 0]
set password [lindex $argv 1]
eval spawn $cmd
set timeout 30
while {1} {
expect -re "Are you sure you want to continue connecting (yes/no)?" {
# First connect, no public key in ~/.ssh/known_hosts
send "yes\r"
} -re "assword:" {
# Already has public key in ~/.ssh/known_hosts
send "$password\r"
} -re "Permission denied, please try again." {
# Password not correct
exit
} -re "kB/s|MB/s" {
# User equivalence already established, no password is necessary
set timeout -1
} -re "file list ..." {
# rsync started
set timeout -1
} -re "bind: Address already in use" {
# For local or remote port forwarding
set timeout -1
} -re "Is a directory|No such file or directory" {
exit
} -re "Connection refused" {
exit
} timeout {
exit
} eof {
exit
}
}

别忘了我们还有《 Linux专版 》

TOP

本功能由奇虎搜索实现

相关主题

标题 作者 最后发表
[站外] expect实现ssh自动交互 zz   [转帖] chandlewei 2008-09-06
[站外] 转载百度面试题--很好很强大   [转帖] 张开梦想的翅膀,振翅高飞!! 2008-11-22
[站外] 转载百度面试题--很好很强大   [转帖] 张开梦想的翅膀,振翅高飞!! 2008-11-22
点击阅读更多关于的相关帖子  更多相关主题