更好的自动ssh登录

时间:2023-03-08 16:54:37

更好的自动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中完成,但是没有成功。还望有谁能够完成,给予指教。