加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

免密脚本带端口

发布时间:2020-12-15 23:04:50 所属栏目:安全 来源:网络整理
导读:1.主要脚本 vi mianmi.sh #!/bin/bash #------------------------------------------# # FileName: 自动批量免密登陆 #maker=zsl echo "" /root/b.txt rpm -q expect /dev/null if [ $? -ne 0 ];then yum -y install expect fi if [ ! -f /root/.ssh/id_rsa.

1.主要脚本

vi mianmi.sh

#!/bin/bash
#------------------------------------------#
# FileName: 自动批量免密登陆
#maker=zsl
echo "" >/root/b.txt
rpm -q expect &>/dev/null
if [ $? -ne 0 ];then
yum -y install expect
fi
if [ ! -f /root/.ssh/id_rsa.pub ];then
ssh-keygen -P "" -f /root/.ssh/id_rsa &>/dev/null # 密钥对不存在则创建密钥
fi
while read line;do
ip=`echo $line | cut -d " " -f1` # 提取文件中的ip
user_name=`echo $line | cut -d " " -f2` # 提取文件中的用户名
pass_word=`echo $line | cut -d " " -f3` # 提取文件中的密码
port=`echo $line | cut -d " " -f4` #提取端口
ping -c1 -Wl $ip &>/dev/null
if [ $? -eq 0 ];then
{ echo "ssh -p $port [email?protected]$ip">>/root/b.txt
expect <<EOF
set timeout 4
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub -p $port [email?protected]$ip
expect {
"yes/no" { send "yesn";exp_continue}
"password" { send "$pass_wordn"}
}
expect eof
EOF
}
fi
continue
done < /opt/ip.txt

?

vi?/opt/ip.txt

192.168.1.62 root 123456 22
192.168.1.64 root 123456 23

?如果卡住 ctrl +c 取消多推几次

(编辑:李大同)

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

    推荐文章
      热点阅读