start.sh
#!/bin/bash password="111111" username="test" if [ $# == 1 ] then username=$1 elif [ $# == 2 ] then username=$1 password=$2 else echo "USAGE:$0 USERNAME" echo "USAGE:$0 USERNAME PASSWORD" exit 1 fi echo "Add UserName:"$username echo "Add PassWord:"$password for i in 2 3 4 5 6 do if [ -f "add.sh" ] then ./add.sh ${i} $username $password else echo "Error:add.sh 脚本不存在" exit 1 fi done
add.sh
#!/usr/bin/expect -f set timeout 30 set node [lindex $argv 0] set name [lindex $argv 1] set pawd [lindex $argv 2] spawn ssh root@node${node} "useradd $name;passwd $name" expect "New password:" send "${pawd} " expect "Retype new password:" send "${pawd} " expect "#"
注意:
1.如果Linux上面,没有安装expect,请先安装expect
2.在ssh各个服务器的时候,由于我已经配置了免密码登陆,所以不会出现如下几条语句,如果出现一下语句:
Are you sure you want to continue connecting (yes/no)?
root@node9's password:
如果出现,请自行用expect捕获