20.29 expect脚本远程执行命令

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

分享
expect脚本远程执行命令
动远程登录后,执行命令并退出
#!/usr/bin/expect
set user "root"
set passwd "123123a"
spawn ssh [email protected]
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 机器执行脚本

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

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台