#!/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"