应用要求:
目标机172.25.254.1-172.25.254.10
主机上执行TNetUser.sh 会在目标机中建立namefile中的用户,密码存于passfile文件。
需要的文件
1.脚本文件:TNetUser.sh(主机运行文件),scp.exp(expect文件)
(往目标机发送的文件) Create_user.sh(执行文件)
namefile(存储用户名文件)
passfile(存储密码文件)
(1)vim TNetUser.sh
#!/usr/bin/env tcsh
for NUM in {1..10}
do
ping -c1 -w1 172.25.254.$NUM > /dev/null && (
/mnt/scp.exp /mnt/Create_user.sh/mnt/namefile /mnt/passfile 172.25.254.$NUM /mnt redhat | grep -E"^The|ECDSA|connecting|Warning|password|spawn" -v | sed"s/Permission\ denied\,\ Please\ try\ again\./172.25.254.$NUM password iserror/g"
)
done
(2)vim Create_user.sh
#!/usr/bin/env tcsh
MAXUSER=`wc -l $1 | cut -d "" -f 1`
MAXPASS=`wc -l $1 | cut -d "" -f 1`
[ "$MAXUSER"-eq "$MAXPASS" ] &&(
for NUM in $( seq 1$MAXUSER )
do
USERNAME=`sed -n ${NUM}p$1`
PASSWORD=`sed -n ${NUM}p$2`
CKUSER=`getent passwd$USERNAME`
[ -z "$CKUSER"]&&(
useradd $USERNAME
echo $PASSWORD | passwd--stdin $USERNAME
)||(echo "$USERNAMEexist")
done
)||(echo "Number ofusers and password number does not match")
(3)vim scp.exp
#!/usr/bin/expect
set timeout 3
set FileBash [lindex $argv 0]
set NameFile [lindex $argv 1]
set PassFile [lindex $argv 2]
set Ip [lindex $argv 3]
set Dir [lindex $argv 4]
set Pass [lindex $argv 5]
#set Comm1 [lindex $argv 6]
spawn scp $FileBash $NameFile $PassFile [email protected]$Ip:$Dir
expect {
"yes/no"
{send"yes/r";exp_continue}
"password:"
{send "$Pass\r"}
}
interact
spawn ssh [email protected]$Ip chmod +x $FileBash
expect {
"yes/no"
{send"yes/r";exp_continue}
"password:"
{send "$Pass\r"}
}
spawn ssh [email protected]$Ip sh $FileBash $NameFile $PassFile
expect {
"yes/no"
{send"yes/r";exp_continue}
"password:"
{send "$Pass\r"}
}
interact
(4)namefile
HXL
HXL1
HXL2
(5)passfile
123
123
123