S29.shell脚本每日一练

57.expect实现ssh自动登录并禁用selinux

[root@rocky8 ~]# vim expect5.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-22
#FileName:      expect5.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
NET=172.31.0
user=root
password=123456

for ID in 6 7;do
ip=$NET.$ID

expect <<EOF
set timeout 20
spawn ssh $user@$ip
expect {
    "yes/no" { send "yes\n";exp_continue }
    "password" { send "$password\n" }     
}
expect "]#" { send "sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config\n" } 
expect "]#" { send "setenforce 0\n" }
expect "]#" { send "exit\n" }
expect eof
EOF
done

[root@rocky8 ~]# chmod +x expect5.sh 
[root@rocky8 ~]# bash expect5.sh
spawn ssh [email protected]
[email protected]'s password: 
Last login: Fri Oct 22 16:07:11 2021 from 172.31.1.8
[root@centos6 ~]# sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@centos6 ~]# setenforce 0
setenforce: SELinux is disabled
[root@centos6 ~]# exit
logout
Connection to 172.31.0.6 closed.
spawn ssh [email protected]
[email protected]'s password: 
Last login: Fri Oct 22 16:07:11 2021 from 172.31.1.8
[root@centos7 ~]# sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@centos7 ~]# setenforce 0
setenforce: SELinux is disabled
[root@centos7 ~]# exit
logout
Connection to 172.31.0.7 closed.

58.shell脚本利用循环调用expect在CentOS和Ubuntu上批量创建用户

[root@rocky8 ~]# vim expect6.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-22
#FileName:      expect6.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
NET=172.31.0
user=root
password=123456
for ID in 6 7;do
ip=$NET.$ID
expect <<EOF
set timeout 20
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
expect "#" { send "useradd test\n" }
expect "#" { send "exit\n" }
expect eof
EOF
done

[root@rocky8 ~]# bash expect6.sh 
spawn ssh [email protected]
[email protected]'s password: 
Last login: Fri Oct 22 16:06:48 2021 from 172.31.1.8
[root@centos6 ~]# useradd test
[root@centos6 ~]# exit
logout
Connection to 172.31.0.6 closed.
spawn ssh [email protected]
[email protected]'s password: 
Last login: Fri Oct 22 16:06:47 2021 from 172.31.1.8
[root@centos7 ~]# useradd test
[root@centos7 ~]# exit
logout
Connection to 172.31.0.7 closed.

猜你喜欢

转载自blog.csdn.net/qq_25599925/article/details/126772788