加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Expect自动生成密钥对

发布时间:2020-12-15 16:17:06 所属栏目:安全 来源:网络整理
导读:1 实现expect自动生成ssh-keygen 然后复制pub key到其他主机 (传入3个参数 缺点只能传public key到一台主机) [root@server1scripts]#catauto_ssh.sh#!/usr/bin/expectsettimeout10setusername[lindex$argv0]setpassword[lindex$argv1]sethostname[lindex$a

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"nr"
send_user"/root/.sshr"
exp_continue
"*Overwrite(y/n)*"{
send"nnr"
}
}
"*Enterpassphrase*"{
send"nr"
exp_continue
}
"*Entersamepassphraseagain*"{
send"nr"
exp_continue
}
}
spawnssh-copy-id-i/root/.ssh/id_rsa.pub$username@$hostname
expect{
#firstconnect,nopublickeyin~/.ssh/known_hosts
"Areyousureyouwanttocontinueconnecting(yes/no)?"{
send"yesr"
expect"password:"
send"$passwordr"
}
#alreadyhaspublickeyin~/.ssh/known_hosts
"password:"{
send"$passwordr"
}
"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*

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读