【遠程用戶創建】

    
應用要求:
    目標機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 root@$Ip:$Dir
expect {
        "yes/no"
        {send"yes/r";exp_continue}
        "password:"
        {send "$Pass\r"}
        }
interact
spawn ssh root@$Ip chmod +x $FileBash
expect {
        "yes/no"
        {send"yes/r";exp_continue}
        "password:"
        {send "$Pass\r"}
        }
spawn ssh root@$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
vim

相關文章
相關標籤/搜索