自動分發密鑰腳本

#!/bin/bashbash


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

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

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}
        }
"spa

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
}ip


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

相關文章
相關標籤/搜索