Expect自动生成密钥对

前端之家收集整理的这篇文章主要介绍了Expect自动生成密钥对前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1 实现expect自动生成ssh-keygen 然后复制pub key到其他主机

(传入3个参数 缺点只能传public key到一台主机)

[root@server1scripts]#catauto_ssh.sh
#!/usr/bin/expect
settimeout10
setusername[lindex$argv0]
setpassword[lindex$argv1]
sethostname[lindex$argv2]
spawnssh-keygen-trsa
expect{
"*fileinwhichtosavethekey*"{
send"\n\r"
send_user"/root/.ssh\r"
exp_continue
"*Overwrite(y/n)*"{
send"n\n\r"
}
}
"*Enterpassphrase*"{
send"\n\r"
exp_continue
}
"*Entersamepassphraseagain*"{
send"\n\r"
exp_continue
}
}
spawnssh-copy-id-i/root/.ssh/id_rsa.pub$username@$hostname
expect{
#firstconnect,nopublickeyin~/.ssh/known_hosts
"Areyousureyouwanttocontinueconnecting(yes/no)?"{
send"yes\r"
expect"password:"
send"$password\r"
}
#alreadyhaspublickeyin~/.ssh/known_hosts
"password:"{
send"$password\r"
}
"Nowtryloggingintothemachine"{
#ithasauthorized,donothing!
}
}
expecteof

2 实现上述脚本, 传入一批主机,并把public key传到一批主机上

实现过程:创建循环脚本和主机列表清单txt 使用循环语句重复执行expect脚本

(利用上述expect脚本 不用传入参数即可循环执行)

用法:把3个文件放在/scritps/之下

主机名/IP写入 serverip.txt

修改auto_issue_SSHpubkey.sh里的密码

[root@server1scripts]#catauto_issue_SSHpubkey.sh
#!/bin/bash
user="root"
passwd="rootpasswd"
foriin`cat/scripts/serverip.txt`;do
/scripts/auto_ssh.sh$user$passwd$i
done
[root@server1scripts]#catserverip.txt
192.168.8.1*
192.168.8.2*
原文链接:https://www.f2er.com/bash/390492.html

猜你在找的Bash相关文章