加入收藏 | 设为首页 | 会员中心 | 我要投稿 温州站长网 (https://www.0577zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

推荐一个Linux批量管理脚本

发布时间:2021-06-10 01:39:41 所属栏目: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 "yesr"; exp_continue} 

 "*password*" {send "$passwordr"; exp_continue} 

 "*Password*" {send "$passwordr";} 

 } 

 

expect -c " 

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

 expect { 

 "*yes/no*" {send "yesr"; exp_continue} 

 "*password*" {send "$passwordr"; exp_continue} 

 "*Password*" {send "$passwordr";} 

 } 

done 

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

ssh-keygen 

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

(编辑:温州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读