Ssh fully-automated dual-host trust script

Source: Internet
Author: User

In fact, ssh dual-host password-less login is nothing more than key verification. This article uses the trusted CT command to achieve key distribution.

First, let's talk about the ssh key login process)


In fact, the key login is to use its own private key to verify whether there is a corresponding public key on the target host.
1. First, the host will obtain the id_rsa private key file under the. ssh directory in its home directory.
2. Then the host will look for whether the target host has authorized_keys in its directory.
3. If there is one, use your own private key to decrypt it to see if it is your own public key. If yes, the login is successful.
4. Otherwise, start password verification.

Setting the sshd configuration file before the experiment will greatly improve the ssh link speed.
First, make sure that ssh has enabled key verification (enabled by default ).
# RSAAuthentication yes
# PubkeyAuthentication yes
# AuthorizedKeysFile. ssh/authorized_keys
Mainly through these three options

Whether to enable password verification
PasswordAuthentication yes

Disabling dns resolution will speed up ssh connection
UseDNS no

Lab environment: centos5.4 x86

All ssh users used by the script are root users.

The script is as follows:

 

 
 
  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} 

This script has been successfully tested. You can use it with confidence. The script works in a very simple way and cannot be automated by using reverse CT.

In this article, I will update the CT content later. Stay tuned

This article is from the blog "jeff for linux" and will not be reposted!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.