#!/usr/bin/expect
#脚本第一个参数是远程
服务器IP
set IP [lindex $argv 0]
#远程服务器用户名(通常用root)
set USER [lindex $argv 1]
#远程服务器用户名的密码
set PASSWD [lindex $argv 2]
#添加的新用户
set Nuser [lindex $argv 3]
#新用户的密码
set Npasswd [lindex $argv 4]
#用spawn启动一个ssh客户端
spawn ssh -l $USER $IP
#如果是第一次连接,要保存密钥再输入密码,如果不是第一次连接则输入密码
expect {
"yes/no" { send "yesr"; exp_
continue }
"password:" { send "$PASSWDr" }
}
#如果不是root,要expect "$",下面不讲了,很简单
expect "*#"
send "useradd -s /bin/sh -d /home/$Nuser $Nuserr"
expect "*#"
send "passwd $Nuserr"
expect "*password:"
send "$Npasswdr"
expect "*password:"
send "$Npasswdr"
expect "*#"
send "exitr"