站长网 Linux 推荐一个Linux批量管理脚本

推荐一个Linux批量管理脚本

以下均放在/root目录下 (1) ip.txt 注意前面是IP,后面是密码,用冒号:分割,如果密码有冒号的建议单独处理 IP:密码 (2) remote_operate.sh #!/bin/bash #copyrightbyhwb if[!-d/root/.ssh];then mkdir/root/.ssh fi cat/tmp/authorized_keys/root/.ssh/a

以下均放在/root目录下

(1) ip.txt

注意前面是IP,后面是密码,用冒号:分割,如果密码有冒号的建议单独处理

IP:密码 

(2) remote_operate.sh

#!/bin/bash 

#copyright by hwb  

 

if [ ! -d /root/.ssh ];then  

 mkdir /root/.ssh 

fi 

cat /tmp/authorized_keys >> /root/.ssh/authorized_keys 

(3) batch_sshkey.sh

#!/bin/bash 

#copyright by hwb  

 

for i in `cat ip.txt` 

do 

ip=$(echo "$i"|cut -f1 -d":") 

password=$(echo "$i"|cut -f2 -d":") 

 

expect -c " 

spawn scp /root/.ssh/authorized_keys /root/remote_operate.sh root@$ip:/tmp/ 

 expect { 

 \"*yes/no*\" {send \"yes\r\"; exp_continue} 

 \"*password*\" {send \"$password\r\"; exp_continue} 

 \"*Password*\" {send \"$password\r\";} 

 } 

 

expect -c " 

spawn ssh root@$ip "/tmp/remote_operate.sh" 

 expect { 

 \"*yes/no*\" {send \"yes\r\"; exp_continue} 

 \"*password*\" {send \"$password\r\"; exp_continue} 

 \"*Password*\" {send \"$password\r\";} 

 } 

done 

执行ssh-keygen,该命令会默认在~/.ssh/目录下创建id_rsa、id_rsa.pub两个文件,分别为公钥和私钥

ssh-keygen 

cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys 

本文来自网络,不代表站长网立场,转载请注明出处:https://www.zwzz.com.cn/html/server/linux/2021/0610/10274.html

作者: dawei

【声明】:站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。
联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部