Chinaunix首页 | 论坛 | 博客
  • 博客访问: 253374
  • 博文数量: 61
  • 博客积分: 1370
  • 博客等级: 中尉
  • 技术积分: 452
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-04 23:40
文章分类

全部博文(61)

文章存档

2012年(1)

2011年(60)

我的朋友

分类: LINUX

2011-09-17 21:15:05

【转】使用expect和ssh远程执行命令的脚本

作者:达达,时间:2011-05-05,分类:实验,标签:Erlang、Linux

晚上找了个脚本,可以用来远程执行命令,这个脚本的用处就是让你可以密码和命令一次性作为参数带入,单纯用ssh只能手工输入密码。有了这个脚本再结合“erl -detached”的方式就可以实现远程开服了,感谢原作者

2011年5月6日:增加了ssh私钥验证的支持。

#!/usr/bin/expect -f
exp_version -exit 5.0
if {$argc!=2} {
send_user "usage: remote-exec command password\n"
send_user "Eg. remote-exec \"ssh user@host ls\\; echo done\" password\n"
send_user "or: remote-exec \"scp /local-file user@host:/remote-file\" password\n"
send_user "or: remote-exec \"scp user@host:/remote-file local-file\" password\n"
send_user "or: remote-exec \"rsync --rsh=ssh /local-file user@host:/remote-file\" 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 "Enter passphrase for key " {
# Already has public key in ~/.ssh/known_hosts
send "$password\r"
} -re "password:" {
# 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
}
阅读(3065) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

zhnghaijin2013-02-25 13:52:58

不错。
多嘴一句,个人觉得密码在参数那边携带并不是好习惯。
还有,貌似最后少了一个 ‘ } ’