#!/usr/bin/env expect
#开启一个程序
spawn ssh root@10.1.1.1
#捕获相关内容
expect{
"(yes/no)?" {send "yes\r";exp_continue}
"password" {send "123456\r"}
}
interact
#执行脚本
./xxxx.sh
/xxx/xxx.sh
expect -f xxx.sh
定义变量
set ip 10.1.1.1
set pass 123456
set timeout 5
spawn ssh root@$ip
expect{
#\r为回车键
"(yes/no)?" {send "yes\r";exp_continue}
"password" {send "$pass\r"}
}
interact
#set ip [ lindex $argv x ] 执行脚本文件时第x-1个参数 ./xxx.sh 0 1 2
set ip 10.1.1.1
set pass 123456
set timeout 5
spawn ssh root@$ip
expect{
"(yes/no)?" {send "yes\r";exp_continue}
"password" {send "$pass\r"}
}
expect "#"
send "rm -rf /tmp/*\r"
send "rouch /tmp/file{1..3}\r"
send "exit\r"
expect eof