自己主动化 远程登陆linuxserver并运行命令 —— expect

原文地址:http://blog.csdn.net/wangyuling1234567890/article/details/41149429


LinuxserverA登陆LinuxserverB

serverA上须要先安装expect。

test.exp

#!/usr/bin/expect  -f // expect安装文件夹

set timeout 10 
set username [lindex $argv 0]  //保存脚本參数到变量中
set password [lindex $argv 1]  
set hostname [lindex $argv 2]  

spawn ssh -l root 172.16.128.16 
expect "password:" 
send "passwd
" <span style="white-space:pre">	</span>//用户的登录password,这里是root用户。密码是passwd

expect "#"  //引號里写成登录后的shell提示符
send "ls -lth /home/
"

expect "#"  //引號里写成上一个命令运行后的shell提示符
send "exit
"

interact  //定时器作用,与前面 set timeout 配合使用。用于超时退出


原文地址:https://www.cnblogs.com/zsychanpin/p/7399443.html