#scp.exp
#!/usr/bin/expect
set route [lindex $argv 0]
set password 123456
spawn scp $route Lyndon@172.16.12.4:/home/Lyndon/trunk/
set timeout 300
expect "Lyndon@172.16.12.4's password:"
set timeout 300
send "$password\r"
set timeout 300
send "exit\r"
expect eof

使用[lindex $argv 0]获取shell终端输入,该脚本实现功能为将本地路径“route"下的文件远程拷贝到4号服务器

执行方法为:

./scp.exp ./a.txt  即可将a.txt文件远程拷贝到4号服务器上

 

 该脚本ssh自动连接到4号服务器,并执行命令 之后退出4号服务器

#start.exp
#!/usr/bin/expect -f
set command [lindex $argv 0]
set password 123456
set timeout 300
#4
spawn ssh -t Lyndon@172.16.12.4
expect "Lyndon@172.16.12.4's password:"
send "$password\r"
expect "a"
send "cd /home/Lyndon/trunk/\r"
send "./$command\r"
send  "exit\r"
expect eof

通过终端输入了一个脚本名,首先ssh到4号服务器,之后cd到指定目录,最后执行脚本,退出4号服务器。

当然可以在脚本中实现一个循环,这样就可以远程操作多台机器。

参考链接:

https://linux.die.net/man/1/expect

http://xstarcd.github.io/wiki/shell/expect_description.html

posted on 2017-08-09 16:58  LyndonYoung  阅读(481)  评论(0编辑  收藏  举报