expect batch traversal ip to execute the specified code [Zhenxiang warning]

True fragrance reason

  • I'll just say one thing. If you want to restart a service of other servers, you can't build a conventional shell except for the expect method [if you're beaten, I'll delete it]
  • Expect doesn't support any traversal syntax, so it's not too fragrant to use EOF to traverse expect [I don't know whether it's EOF or expect]

Define password method

Code display

  • Expect has a very strange problem. If expect uses password free login, the execution process will be very slow.
    If you use the following method to define the password, it is super fast. The consequence is that the password will remain in the file and others may see the password.
[root@controll ccx]# cat expect.sh 
#!/bin/bash

# This is the IP address file. The format is: IP password
file=/ccx/iplist.txt
cat $file|while read line
do
# Define an a array
a=($line)    
/usr/bin/expect<<EOF      
spawn ssh root@${a[0]}     
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*password:" { send "${a[1]}\r" } 
}
expect "#"
send "tail -n 5 /etc/sysconfig/iptables\r"
send "rpm -qa | grep iptable\r"
send "systemctl restart iptables.service\r"
send "exit\r"            
expect eof
EOF
done
[root@controll ccx]#
[root@controll ccx]# cat iplist.txt 
192.168.59.128 root
192.168.59.129 root
192.168.59.130 root
[root@controll ccx]# 

Note: no blank line is allowed in the IP file [it will not affect the operation result, but will produce error content]

Execution mode and effect

  • Execute: SH expext sh
    The effect is as follows
[root@controll ccx]# sh expect.sh 
spawn ssh root@192.168.59.128
root@192.168.59.128's password: 
Last login: Sat May 22 01:19:38 2021 from 192.168.59.133
[root@centso76_1 ~]# tail -n 5 /etc/sysconfig/iptables
# test iptables1
# test iptables2
# test iptables3
# test iptables4
# test iptables5
[root@centso76_1 ~]# rpm -qa | grep iptable
iptables-1.4.21-28.el7.x86_64
iptables-services-1.4.21-28.el7.x86_64
[root@centso76_1 ~]# systemctl restart iptables.service
[root@centso76_1 ~]# exit
 Log out
Connection to 192.168.59.128 closed.
spawn ssh root@192.168.59.129
root@192.168.59.129's password: 
Last login: Fri May 21 05:30:49 2021 from 192.168.59.133
[root@centos76_2 ~]# tail -n 5 /etc/sysconfig/iptables
# test iptables1
# test iptables2
# test iptables3
# test iptables4
# test iptables5
[root@centos76_2 ~]# rpm -qa | grep iptable
iptables-1.4.21-28.el7.x86_64
iptables-services-1.4.21-28.el7.x86_64
[root@centos76_2 ~]# systemctl restart iptables.service
[root@centos76_2 ~]# exit
logout
Connection to 192.168.59.129 closed.
spawn ssh root@192.168.59.130
root@192.168.59.130's password: 
Last login: Fri May 21 17:24:45 2021 from 192.168.59.133
[root@centos76_3 ~]# tail -n 5 /etc/sysconfig/iptables
# test iptables1
# test iptables2
# test iptables3
# test iptables4
# test iptables5
[root@centos76_3 ~]# rpm -qa | grep iptable
iptables-1.4.21-28.el7.x86_64
iptables-services-1.4.21-28.el7.x86_64
[root@centos76_3 ~]# systemctl restart iptables.service
[root@centos76_3 ~]# exit
logout
Connection to 192.168.59.130 closed.
[root@controll ccx]# 

Secret free mode

  • What is secret free?
    You don't need to enter a password to log in. You can execute SSH copy ID.

Code display

  • Since the secret free IP may only be part of the IP address, I changed the execution method. There is no file specified. The method of manually entering files is more flexible.
  • It is not recommended to use this method. The execution process is very slow (but safe, because you don't need to enter a password) [if you pursue speed but don't care about leaving a password, go to a computer that doesn't have a password, and use the above methods]
  • The logic is basically the same as the above, except that there is no need to specify the ip file, there is no array, and the ip file is only ip information without password
  • Because my tester is not password free, the following is the test I did in the production environment [so I manually changed the first two digits of IP]
[root@controller01 ccx]# cat restart.sh 
#!/bin/bash
cat $1|while read line
do
/usr/bin/expect<<EOF
spawn ssh root@$line
expect {
"*yes/no" { send "yes\r"; exp_continue}
}
expect "#"
send "hostname\r"
send "exit\r"
expect eof
EOF
done
[root@controller01 ccx]# 
[root@controller01 ccx]# cat compu1-5.txt 
0.0.101.1
0.0.101.2
0.0.101.3
0.0.101.4
0.0.101.5
[root@controller01 ccx]# 

Execution mode and effect

  • Execution method: SH expand sh compu1-5. txt
    The effect is as follows
[root@controller01 ccx]# sh restart.sh compu1-5.txt 
spawn ssh root@0.0.101.1
Last login: Fri May 21 15:23:19 2021 from controller01
[root@compute01 ~]# hostname
compute01
[root@compute01 ~]# exit
logout
Connection to 0.0.101.1 closed.
spawn ssh root@0.0.101.2
Last login: Fri May 21 15:24:22 2021 from controller01
[root@compute02 ~]# hostname
compute02
[root@compute02 ~]# exit
logout
Connection to 0.0.101.2 closed.
spawn ssh root@0.0.101.3
Last login: Fri May 21 15:26:22 2021 from controller02
[root@compute03 ~]# hostname
compute03
[root@compute03 ~]# exit
logout
Connection to 0.0.101.3 closed.
spawn ssh root@0.0.101.4
Last login: Fri May 21 15:26:22 2021 from controller02
[root@compute04 ~]# hostname
compute04
[root@compute04 ~]# exit
logout
Connection to 0.0.101.4 closed.
spawn ssh root@0.0.101.5
Last login: Fri May 21 15:26:22 2021 from controller02
[root@compute05 ~]# hostname
compute05
[root@compute05 ~]# exit
logout
Connection to 0.0.101.5 closed.
[root@controller01 ccx]# 

Keywords: Linux shell

Added by monkeynote on Thu, 10 Feb 2022 06:22:14 +0200