自动分发密钥脚本

#!/bin/bash


hostname=$(hostname)
ip_list=${1:?'no ip_list! use the ip_list.txt as $1'}

key_send()
{
for i in `cat $1`
do
ip=$(echo "$i"|cut -f1 -d":")
password=$(echo "$i"|cut -f2 -d":")

expect -c "
spawn scp /root/.ssh/id_rsa.pub  root@$ip:~/.ssh/$hostname.pub
        expect {
                \"*yes/no*\" {send \"yes\r\"; exp_continue}
                \"*password*\" {send \"$password\r\"; exp_continue}
                \"*Password*\" {send \"$password\r\"; exp_continue}
        }
"

expect -c "
spawn ssh root@$ip \"cd ~/.ssh;cat $hostname.pub>>authorized_keys;rm $hostname.p
ub;chmod 600 authorized_keys\"
        expect {
                \"*yes/no*\" {send \"yes\r\"; exp_continue}
                \"*password*\" {send \"$password\r\"; exp_continue}
                \"*Password*\" {send \"$password\r\"}
        }
"
done
}


[[ -f ~/.ssh/id_rsa.pub ]] && (echo "key exits,continue";key_send $ip_list) || (
echo "key not exits,exit")
 

你可能感兴趣的:(ssh,自动,秘钥脚本)