20.29 expect腳本遠程執行命令

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 "]" : 「」 通配]右邊全部字符,mysql

  • send 執行命令linux

  • A 機器執行腳本nginx

[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.txt
  • B機器查看腳本運行的命令是否成功
[root@aminglinux-01 ~]# ls /tmp/
12.txt      systemd-private-7723d189c8d3469ab732edadbbc340cb-nginx.service-eKHAIK
mysql.sock  systemd-private-7723d189c8d3469ab732edadbbc340cb-vmtoolsd.service-1yxB4M
[root@aminglinux-01 ~]# cat /tmp/12.txt
1212
相關文章
相關標籤/搜索