sshkey 批量免密登录

需要安装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' '[email protected]'"
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' '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

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

猜你喜欢

转载自blog.51cto.com/jiay1/2498202
今日推荐