Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9169437
  • 博文数量: 1669
  • 博客积分: 16831
  • 博客等级: 上将
  • 技术积分: 12594
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-25 07:23
个人简介

柔中带刚,刚中带柔,淫荡中富含柔和,刚猛中荡漾风骚,无坚不摧,无孔不入!

文章分类

全部博文(1669)

文章存档

2023年(4)

2022年(1)

2021年(10)

2020年(24)

2019年(4)

2018年(19)

2017年(66)

2016年(60)

2015年(49)

2014年(201)

2013年(221)

2012年(638)

2011年(372)

分类: LINUX

2013-02-16 09:25:48

ssh全自动实现双机信任脚本  

2013-02-04 10:28:22|  分类: rhel_ssh |  标签: |字号 

其实ssh的双机无密码登陆无非就是实现密钥验证而已,本文通过expect命令来实现了密钥分发的我过程 帮助广大同胞解放双手

先说下ssh的密钥登陆的过程(个人理解)


其实密钥登陆也就是用自己的私钥去验证在目的主机上是否有相对应的公钥
1.首先主机会获取自己家目录下的.ssh目录下的id_rsa私钥文件
2.然后主机会去查找目的主机在其加目录下是否有authorized_keys
3.如果有的话会用自己的私钥来解密看是否是自己的公钥 如果是则登陆成功
4.否则启动密码验证。

实验前设置好sshd的配置文件将会很大提高ssh的链接速度
首先要确保ssh开启了密钥验证(默认开启)我么也可以通过
#RSAAuthentication yes
##PubkeyAuthentication yes
#AuthorizedKeysFile     .ssh/authorized_keys
主要是通过这三个选项

是否开启密码验证
PasswordAuthentication yes

关闭dns解析会加快ssh连接的速度
UseDNS no

实验环境:centos5.4 x86

脚本使用的ssh用户都为root 大家都自行修改

脚本如下:

 

	
  1. #!/usr/bin/expect 
  2. #2013-01-18 
  3. #author zhangyifei 
  4. #blog http://zyfforlinux.blog.51cto.com 
  5. set local_passwd "server" 
  6. set des_passwd "server" 
  7. set timeout 10 
  8. set localip "192.168.0.254" 
  9. set desip "192.168.0.251" 
  10. spawn ssh-keygen -t rsa 
  11. expect "Enter file*:" {send "\r"
  12. expect "Enter passphrase*" {send "\r"
  13. expect "Enter same*" {send "\r";exp_continue} 
  14. spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@$desip 
  15. expect { 
  16. "yes/no" { send "yes\r";exp_continue} 
  17. "password:" {send "$des_passwd\r";exp_continue} 
  18.  
  19. spawn ssh $desip "ssh-keygen -t rsa" 
  20. expect "Enter file*:" {send "\r"
  21. expect "Enter passphrase*" {send "\r"
  22. expect "Enter same*" {send "\r";exp_continue} 
  23.  
  24. spawn scp $desip:/root/.ssh/id_rsa.pub /root/.ssh/authorized_keys 
  25. expect { 
  26. "yes/no" { send "yes\r";exp_continue} 
  27. "password:" {send "$local_passwd\r";exp_continue} 

此脚本我已经测试成功大家可以放心使用,脚本原理很简单,无法就是借助了expect来实现自动化而已。

阅读(1233) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~