码迷,mamicode.com
首页 > 其他好文 > 详细

sshkey 批量免密登录

时间:2020-05-25 15:30:17      阅读:49      评论:0      收藏:0      [点我收藏+]

标签:rsa   wan   inf   copy   install   out   HSM   $1   bash   

需要安装sshpass
ubuntu: apt-get install sshpass
脚本:

#!/bin/bash
line=`awk ‘END{ print NR}‘ password.txt`
for i in `seq 1 $line`
do
    ip=`head -n $i  password.txt |tail -1|awk ‘{print $1}‘`
    port=`head -n $i  password.txt |tail -1|awk ‘{print $2}‘`
    password=`head -n $i  password.txt |tail -1|awk ‘{print $3}‘`
    sshpass -p ${password} ssh-copy-id -i /root/.ssh/id_rsa.pub -p ${port} -o StrictHostKeyChecking=no root@${ip}
done

文本:
password.txt
文本格式:
IP PORT PASSWORD
文本内容:

cat password.txt 
192.168.1.201 22 KbbTnykeM6VW
192.168.1.202 22 SHSMmTCBNb47Kxi

执行过程:

./copysshkey.sh 
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh -p ‘22‘ -o ‘StrictHostKeyChecking=no‘ ‘root@192.168.1.201‘"
and check to make sure that only the key(s) you wanted were added.

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh -p ‘22‘ -o ‘StrictHostKeyChecking=no‘ ‘root@192.168.1.202‘"
and check to make sure that only the key(s) you wanted were added.

脚本思路:
1、获取文本行数
2、循环文本行数
3、获取文本每行每列的字符串
4、执行设置免密操作

sshkey 批量免密登录

标签:rsa   wan   inf   copy   install   out   HSM   $1   bash   

原文地址:https://blog.51cto.com/jiay1/2498202

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!