批量上传ssh公钥文件到远程主机

#!/bin/bash

# 生成ssh公钥文件并批量上传到远程主机。
# 将iplist和此脚本放在同一目录下执行。
# ssh-copy-id命令默认使用的端口为22,如果不是,请手动更改。
# iplist文件中每行一个ip地址。

#WARNA KESUKAAN
cyan='\e[0;36m'
green='\e[0;34m'
okegreen='\033[92m'
lightgreen='\e[1;32m'
white='\e[1;37m'
red='\e[1;31m'
yellow='\e[0;33m'
BlueF='\e[1;34m' #Biru
RESET="\033[00m" #normal
orange='\e[38;5;166m'

function pldlnx () {
echo ""
echo -e $orange "  +-----------------------------------------+"
echo -e $orange "  |$white [$okegreen 1$white ]$yellow Generate public key in local machine. |"
echo -e $orange "  |$white [$okegreen 2$white ]$yellow Upload public key to remote machines. |"
echo -e $orange "  |$white [$okegreen 3$white ]$yellow Exit......                            |"
echo -e $orange "  +-----------------------------------------+"
echo ""
echo -ne $okegreen "  Choose 1,2,3 : ";tput sgr0
read pld
case $pld in
1)
echo "***************************************"
echo "Generate public key file in quiet mode."
echo "***************************************"
# 以静默、强制覆盖模式生成本地公钥文件。
ssh-keygen -q -t rsa -f ~/.ssh/id_rsa -N "" -C ""
pldlnx
;;
2)
echo "**************************************************************************"
echo "Upload public key file to remote machines in iplist by execute ssh-copy-id."
echo "**************************************************************************"

for ip in `cat iplist`
    do
    # 将本地公钥文件拷贝至远程主机。
    echo 'ssh-copy-id -f -i ~/.ssh/id_rsa.pub -p 22 itadmin@$ip'
    ssh-copy-id -f -i ~/.ssh/id_rsa.pub -p 22 itadmin@$ip    
    done
pldlnx
;;
3)
exit
;;
*)
echo ""
echo -ne $red "Wrong choose , choose between 1 and 3 :"
pldlnx
;;
esac
}
pldlnx
posted @ 2019-10-13 16:35  heycomputer  阅读(851)  评论(0编辑  收藏  举报