操作
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会停在远程服务器上。