栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

CentOS利用expect批量推送ssh public key的脚本

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

CentOS利用expect批量推送ssh public key的脚本

方法1:

#!/bin/bash
#
#********************************************************************
#Author:            Razor
#QQ:                254456122
#Date:              2021-10-29
#FileName           sshkey.sh
#URL:               https://blog.csdn.net/mandarin_meng?spm=1019.2139.3001.5343
#Description        The test script
#Copyright (C):     2021 All rights reserved
#********************************************************************
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
COLOR="echo -e E[1;32m"
END="E[0m"
PASSWORD=123456
IPLIST="
192.168.8.8
192.168.8.21
"
[ ! -f ~/.ssh/id_rsa ] && ssh-keygen -P "" -f ~/.ssh/id_rsa &>/dev/null
rpm -q expect &> /dev/null || yum -y -q install expect &>/dev/null
for ip in $IPLIST ;do
{
expect < 

范例:基于key验证实现批量主机管理

[root@cent8 data]#cat host.txt
192.168.8.8
192.168.8.21

[root@cent8 data]#for i in `cat host.txt`;do ssh $i hostname -I ;done
192.168.8.8
192.168.8.21

方法2:

或者将远程需要推送的主机列表放到一个文件中,这样以后就不需要每次都修改脚本了

#!/bin/bash
#
#********************************************************************
#Author:            Razor
#QQ:                254456122
#Date:              2021-10-29
#FileName           sshkey1.sh
#URL:               https://blog.csdn.net/mandarin_meng?spm=1019.2139.3001.5343
#Description        The test script
#Copyright (C):     2021 All rights reserved
#********************************************************************
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
PASS=123456
rpm -q expect &> /dev/null || yum -y install expect &> /dev/null
[ ! -f ~/.ssh/id_rsa ] && ssh-keygen  -t rsa -P "" -f /root/.ssh/id_rsa &> /dev/null && echo "ssh key is created"
while read IP ;do
expect < /dev/null
set timeout 20
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@$IP
expect {
"yes/no" { send "yesr";exp_continue }
"password" { send "$PASSr" }
}
expect eof
EOF
echo $IP is ready
done < hosts.txt
[root@cent8 data]#cat hosts.txt
192.168.8.8
192.168.8.21
[root@cent8 data]#
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/361642.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号