码迷,mamicode.com
首页 > 其他好文 > 详细

更好的自动ssh登录

时间:2015-09-04 14:18:13      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

 更好的自动ssh登录

解决~/.ssh/known_hosts 过期问题。
bash + expect

bash:ssh.sh

  1. #!/bin/bash

  2. help(){
  3.     echo "usage: [password]"
  4. }
  5. [ -z "$1" ] && { help; exit 1; } || {
  6.     case $1 in
  7.         -*) help; exit ;;
  8.         *) ip=$1;;
  9.     esac
  10. }
  11. shift
  12. ssh.exp "$ip" "$@"
  13. [ "$?" = "1" ] && ssh-keygen -R $ip && ssh_ivie.exp "$ip" "$@"


expec
ssh.exp

  1. #!/usr/bin/expect -f

  2. proc help {} {
  3. puts {usage: <ivie_ip> [password]}
  4. }

  5. if {$argc<1} { help ; exit}
  6. set ip [ lindex $argv 0 ]

  7. set password rootroot
  8. if {$argc==2} { set password [lindex $argv 1] }


  9. # close the output
  10. log_user 0


  11. set timeout 30
  12. spawn ssh -XY root@$ip
  13. expect {
  14.     -re ".*:~ # " {}
  15.     "Password: " { send "$password\r" }
  16.     "(yes/no)?" {send "yes\r"; exp_continue}
  17.     "Host key verification failed" { send_user "run: ssh-keygen -R $ip" ; exit 1 }
  18.     timeout {puts "check your ip: $ip"; exit 2}
  19.     eof { puts "check your ip: $ip" ;exit 3 }
  20. }
  21. interact


我测试过在一个expect中完成,但是没有成功。还望有谁能够完成,给予指教。

更好的自动ssh登录

标签:

原文地址:http://www.cnblogs.com/timdes/p/4781150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!