20.29 expect脚本远程执行命令

2022-01-06 14:10:52 浏览数 (2)

expect脚本远程执行命令

  • 动远程登录后,执行命令并退出
代码语言:javascript复制
#!/usr/bin/expect
set user "root"
set passwd "123123a"
spawn ssh $user@192.168.133.130

expect {
"yes/no" { send "yesr"; exp_continue}
"password:" { send "$passwdr" }
}

expect "]*"
send "touch /tmp/12.txtr"
expect "]*"
send "echo 1212 > /tmp/12.txtr"
expect "]*"
send "exitr"
  • expect "]" : “” 通配]右边所有字符,
  • send 执行命令
  • A 机器执行脚本
代码语言:javascript复制
[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机器查看脚本运行的命令是否成功
代码语言:javascript复制
[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

0 人点赞