自動化 遠端登陸linux伺服器並執行命令 —— expect
阿新 • • 發佈:2019-01-09
Linux伺服器A登陸Linux伺服器B
伺服器A上需要先安裝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\r" <span style="white-space:pre"> </span>//使用者的登陸密碼,這裡是root使用者,密碼是passwd expect "#" //引號裡寫成登入後的shell提示符 send "ls -lth /home/\r" expect "#" //引號裡寫成上一個命令執行後的shell提示符 send "exit\r" interact //定時器作用,與前面 set timeout 配合使用,用於超時退出