远程登录并做一些事情
[root@m01 /shell4]# vim expect.sh
#!/usr/bin/env expect
set ip 172.16.1.7
set pass 1
set timeout 5
#开启一个程序
spawn ssh $ip
#捕获相关内容
expect {
"(yes/no)?" { send "yes
";exp_continue }
"password:" { send "$pass
" }
}
expect "#"
send "rm -rf /tmp/*
"
send "hostname
"
send "date +%F
"
send "touch /tmp/file{1..3}
"
expect eof
传递参数
[root@m01 /shell4]# vim expect.sh
#!/usr/bin/env expect
set ip [ lindex $argv 0 ]
set pass [ lindex $argv 1 ]
set timeout 5
#开启一个程序
spawn ssh $ip
#捕获相关内容
expect {
"(yes/no)?" { send "yes
";exp_continue }
"password:" { send "$pass
" }
}
expect "#"
send "rm -rf /tmp/*
"
send "hostname
"
send "date +%F
"
send "touch /tmp/file{1..3}
"
expect eof