shell脚本--expect实战:制作密钥推送密钥

准备存放ip的文件ip.txt

vim ip.txt
192.168.226.164
192.168.226.165
#!/usr/bin/bash
##ssh-key ssh-copy-id
##weqiang li

rpm -q expect &> /dev/null
if [ $? -ne 0 ] ;then
        yum install -y expect &>/dev/null
fi



get_keygen(){
/usr/bin/expect <<-EOF &>/dev/null
spawn  ssh-keygen
expect {

		".ssh/id_rsa):"  { send "\r"; exp_continue }
		"Overwrite (y/n)?" { send "y\r"; exp_continue }
		"no passphrase):"  { send "\r"; exp_continue }
		"passphrase again:"  {  send "\r" };

}
	interact
	expect eof
	EOF
}
send_key(){
/usr/bin/expect <<-EOF &>/dev/null    
#(呼唤expct  把我们想用expct的地方 用段落标记把他放进去)
spawn  ssh-copy-id -i  $ip
expect {
		"connecting (yes/no)?"  { send "yes\r"; exp_continue}
		"password:"		{ send "1\r"}


}
	interact
expect eof 
#(说完了   关闭对话)
EOF
}
echo "正在制作密钥"
echo "存放的ip的文件是ip.txt,对方密码是必须是1"
sleep 5

if [ ! -f ~/.ssh/id_rsa ];then
       get_keygen
fi


for ip  in `cat ip.txt`
do 
{
	send_key $ip
	if [ $? -eq 0 ] ;then
		echo "$ip,密钥发送成功"
	else
		echo "$ip,密钥发送失败"
	fi
	
}&
done
wait  
echo "完成任务"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值