linux_Batch establish trust relationship

Summary: Linux batch establishment of trust relationship

1. First, you need to check whether expect is installed: rpm -qa|grep expect 2. Then, create a public key on the operating machine: ssh-keygen press Enter all the way 3. After creation, go to /home Under /barfoo/.ssh/, you can see 2 files starting with id, of which id_rsa.pub is the public key file. 4. All you need to do is to transfer this file to other machines: ssh-copy-id -i /home /barfoo/.ssh/id_rsa.pub barfoo@ip 5. Finally, ssh root@ip can log in without a password. 6. The following is the script, which can be modified according to your actual situation
#2017-05-18 #Batch
ssh authentication establishment 

for p in $(cat /home/barfoo/ip.txt) #Note the absolute path of the ip.txt file 
do 
ip=$(echo "$p"|cut -f1 -d":") #Take the file in the ip.txt file ip address 
password=$(echo "$p"|cut -f2 -d":") #take the password in the ip.txt file 

#expect automatic interaction start 
expect -c "  
spawn ssh-copy-id -i /home/ barfoo/.ssh/id_rsa.pub barfoo@$ip 
        expect {  
                \"*yes/no*\" {send \"yes\r\"; exp_continue}  
                \"*password*\" {send \"$password\r\"; exp_continue}  
                \"*Password*\" {send \"$password\r\";}  
        }  
"  
done 
1. ip.txt文件里面ip和密码写法
172.16.4.27:barfoo1
172.16.4.28:barfoo2

1. 也可以这么写
for i in `seq 101 150`
do
/usr/bin/expect << EOF
spawn ssh-copy-id [email protected].$i
expect {
"yes/no" { send "yes\r"; exp_continue; }
"*password" { send "P@ssw0rd\r" }
}

expect "~$ "
send "exit\r" wget http://www.theether.org/pssh/pssh-1.4.3.tar.gz #download the PSSH installation package the PSSH installation package doneDownload EOF
expect eof







tar zxvf pssh-1.4.3.tar.gz
cd pssh-1.4.3
wget 'http://peak.telecommunity.com/dist/ez_setup.py'
python ez_setup.py
python setup.py install

After installation, execute
pssh - P -h ip.txt 'uptime

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326616929&siteId=291194637