Linux record -Shell automated deployment and batch quantities to create a user SSH configuration (reprint)

IF [! $ # -eq 2]; the then 
echo "! Please enter your user name and password separated by a space" 
Exit 

the else 
name = "$ 1" 
passwd = "$ 2" 
fi 


CAT hosts | the while the Read hosts 
do 

echo "is $ on hosts user name $ " 
the Expect << EOF 
spawn SSH $ hosts" useradd $ name; echo $ name: $ passwd | chpasswd " 
the Expect { 
" * yes / NO "{the send" yes \ r "; exp_continue} 
" * password: "{the send "Dongliang \ r"; exp_continue} 
} 

EOF 

echo "successfully established" 

DONE
IF [! $ # -eq 2]; the then 
echo "Please enter your password separated by a space" 
Exit 
the else 
# username 
the uname = "$. 1" 
# Password 
the passwd = "$ 2" 
Fi 

# perform detection and install expect module 
ep = `rpm -qa | grep expect` 

IF [the -Z EP $]; the then 
 echo "detect your system, expect module is not installed, the installation ready" 
 SLEEP 2 
 yum the install -Y expect 

the else 
 echo "expect module is installed, after 3 seconds begin ssh file synchronization " 
 SLEEP 3 
fi 

# no ssh public key authentication module 
CAT hosts | the while the Read Host 
do 

echo" is currently transmitting files to ssh Host on $ " 
the Expect << EOF 
spawn scp -r .ssh / $ Host: / $ the uname 
Expect { 
"* Yes / NO" {Send "Yes \ R & lt";} exp_continue 
"*password:" {send "$passwd\r" ;exp_continue  }
}

EOF

echo "public key authentication is currently being carried out on $ ..... Host" 
SLEEP 2 
the Expect << EOF 
spawn SSH-Copy-the above mentioned id -i .ssh / id_rsa.pub $ Host 
the Expect { 
"* yes / NO" {the send " yes \ r "; exp_continue} 
" * password: "{the send" $ passwd \ r "; exp_continue} 
} 

EOF 

echo" authentication success .... " 

DONE 

# to switch hosts file root permission to distribute 

echo" synchronization of the machine hosts files to each machine, " 

SLEEP 1 

# synchronization this machine hosts file to each other machine 

CAT hosts | the while the Read Host 
do 

scp -r / etc / hosts $ Host: / etc / 


DONE 

echo" hosts file synchronization is complete. "
IF [! $ # -eq 2]; the then 

echo "Please enter your user name and password separated by a space" 
Exit 
the else 

# user name 
uname = "$ 1" 
unified password # user 
pd = "$ 2" 
fi 


# public without ssh authentication modules 
CAT hosts | the while the Read Host 
do 

echo "is currently transmitting on $ host ssh to file" 
the Expect << EOF 
spawn scp -r .ssh / $ Host: / Home / $ uname 
the Expect { 
"* yes / NO" {the send " Yes \ R & lt ";} exp_continue 
" * password: "Send {" $ PD \ R & lt "; exp_continue} 
} 


the EOF 

echo" $ are in the public key authentication performed ..... Host " 
SLEEP 2 
Expect the EOF << 
the spawn SSH -i .ssh the above mentioned id--copy / id_rsa.pub $ Host 
the Expect { 
"* yes / NO" {the send "yes\r" ;exp_continue}
"*password:" {send "$pd\r" ;exp_continue  }yes \ r "; exp_continue} 
} 


EOF 

echo" authentication success .... "

done

Guess you like

Origin www.cnblogs.com/xinfang520/p/11611525.html