expect批量建立用户名实例

#!/usr/bin/expect 服务器

#批量建立用户名 ssh

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

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

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

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

set Npasswd [lindex $argv 4] 密码

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

#若是是第一次链接,要保存密钥再输入密码,若是不是第一次链接则输入密码 客户端

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"

相关文章
相关标签/搜索