#!/usr/bin/expect

#批量创建用户名

set IP   [lindex $argv 0]   #脚本第一个参数是远程服务器IP

set USER [lindex $argv 1]     #远程服务器用户名

set PASSWD [lindex $argv 2]   #远程服务器密码

set Nuser [lindex $argv 3]    #添加的新用户

set Npasswd [lindex $argv 4]

spawn ssh -l $USER $IP        #spawn启动一个ssh客户端

#如果是第一次连接,要保存密钥再输入密码,如果不是第一次连接则输入密码

expect {

 "yes/no" { send "yes\r"; exp_continue }

 "password:" { send "$PASSWD\r" }

}

expect "*#"

send "useradd -s /bin/sh -d /home/$Nuser $Nuser\r"

expect "*#"

send "passwd $Nuser\r"

expect "*password:"

send "$Npasswd\r"

expect "*password:"

send "$Npasswd\r"

expect "*#"

send "exit\r"