-
我只说一个,如果要重启其他服务器的某个服务,除了expect方式,常规shell怎么造都不行【话就摆这了,被打脸了我再删掉】
-
而expect是不支持任何遍历语法的,所以利用EOF遍历expcet,简直不要太香了【也不知道是EOF香还是expect香】
=====================================================================
- expect有一个很奇怪的问题,如果expect使用免密登陆的,执行过程会很慢。
而如果是使用了下面这定义密码的方法,超级快,后果就是密码会留在文件中,别人可能看到密码。
[root@controll ccx]# cat expect.sh
#!/bin/bash
这个是IP地址文件,格式为:IP 密码
file=/ccx/iplist.txt
cat $file|while read line
do
定义一个a数组
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]#
注:IP文件中不允许有空白行【不影响运行结果,但会产生报错内容】
- 执行:
sh expext.sh
效果如下
[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
登出
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]#
===================================================================
- 何为免密?
不需要输入密码登陆的这种,执行:ssh-copy-id这种方式实现的。
-
因为免密的IP可能只是一部分,所以我这执行方式换了一种,没有指定文件,手动输入文件的方式,更灵活一点。
-
不建议用这种,这种执行过程挺慢的(但安全,因为不用输入密码)【如果追求速度但无所谓留下密码就去一台没有加免密的上面用上面种方法吧】
-
逻辑和上面基本一样,只是不用指定ip文件,没有了数组,ip文件中也仅仅是ip信息没有密码
-
因为我测试机没有加免密,所以下面是我在生产环境中做的测试【所以手动把IP前2位改了】
[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]#
- 执行方式:
sh expext.sh compu1-5.txt
效果如下
[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.