shell批量复制ssh-key

#!/bin/bash

export SSHPASS="123456"
file=$1
user="root"

if [ !-n $file ];then
echo "Please set IP address list."
exit 1
fi
ssh-keygen -t rsa -q -N "" -f ~/.ssh/id_rsa
cat $file

for ip in `cat $file`
do
echo $ip
sshpass -e ssh $user@$ip -o stricthostkeychecking=no "ls ~/.ssh >/dev/null 2>&1"
if [ $? -ne 0 ];then
sshpass -e ssh $user@$ip -o stricthostkeychecking=no "mkdir ~/.ssh"
fi
sshpass -e ssh $user@$ip -o stricthostkeychecking=no "chmod 700 ~/.ssh"
sshpass -e scp ~/.ssh/id_rsa.pub $user@$ip:~/.ssh/
sshpass -e ssh $user@$ip -o stricthostkeychecking=no "cat ~/.ssh/id_rsa.pub >>~/.ssh/authorized_keys"
sshpass -e ssh $user@$ip -o stricthostkeychecking=no "chmod 400 ~/.ssh/authorized_keys"
ssh $ip -C "ls" >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "$ip is successful"
fi
done

posted @ 2022-06-30 15:51  guoxin0126  阅读(111)  评论(0)    收藏  举报