下面是远程ssh登录后在远程主机创建一个用户的例子
#!/usr/bin/expect
set timeout 5
set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
#连接ssh
spawn ssh root@$host
expect {
"password" {send "100trust\r"}
"yes/no" {send "yes\r";exp_continue}
}
#在远程主机创建一个用户
expect -re ".*
\$#
\$#
"
send "cd /usr\r"
send "echo -e '${password}\n${password}\n\n\n\n\n\nY\n' | adduser ${username}\r"
send "exit\r"
expect eof
#interact
执行脚本,输入参数
./adduser.sh 192.168.10.8 xiaoming 123456