操作
vi ssh.sh
#!/usr/tcl/bin/expect
if {$argc < 3} {
puts "Usage:cmd <host> <username> <password>"
exit 1
}
set timeout -1
set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh $username@$host
expect {
"*yes/no" {send "yes\r"; exp_continue}
"*password:" {send "$password\r"; exp_continue}
"$username@*" {send "mkdir zzz;mkdir aaa;exit;\r" }
}
#interact
expect eof
exit
chmod +x ssh.sh
./ssh.sh 127.0.0.1 root 123456
注意
- 第一行定要寫這句話,否則會報
spawn: command not found
#!/usr/tcl/bin/expect:使用expect來解釋該腳本;
- 這個腳本和shell腳本部分指令不兼容.注意分開寫
-
這3個必須要有,否則他一登錄就退出掉了,不會執行mkdir的內容。
image.png - interact會停在遠程服務器上。