20.29 expect脚本远程执行命令

2018-03-01 11:10:24来源:oschina作者:运维小白人点击

分享
expect脚本远程执行命令
动远程登录后,执行命令并退出
#!/usr/bin/expect
set user "root"
set passwd "123123a"
spawn ssh $user@192.168.133.130
expect {
"yes/no" { send "yes/r"; exp_continue}
"password:" { send "$passwd/r" }
}
expect "]*"
send "touch /tmp/12.txt/r"
expect "]*"
send "echo 1212 > /tmp/12.txt/r"
expect "]*"
send "exit/r"

expect "]" : “” 通配]右边所有字符,

send 执行命令

A 机器执行脚本

[root@aminglinux-02 sbin]# ./2.expect
-bash: ./2.expect: 权限不够
[root@aminglinux-02 sbin]# chmod a+x 2.expect
[root@aminglinux-02 sbin]# ./!$
./2.expect
spawn ssh root@192.168.133.130
root@192.168.133.130's password:
Last login: Wed Sep 20 23:31:49 2017 from 192.168.133.131
[root@aminglinux-01 ~]# touch /tmp/12.txt
[root@aminglinux-01 ~]# echo 1212 > /tmp/12.txtB机器查看脚本运行的命令是否成功
[root@aminglinux-01 ~]# ls /tmp/
12.txtsystemd-private-7723d189c8d3469ab732edadbbc340cb-nginx.service-eKHAIK
mysql.socksystemd-private-7723d189c8d3469ab732edadbbc340cb-vmtoolsd.service-1yxB4M
[root@aminglinux-01 ~]# cat /tmp/12.txt
1212

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台