更好的自动ssh登录
解决~/.ssh/known_hosts 过期问题。
bash + expect
bash:ssh.sh
- #!/bin/bash
- help(){
- echo "usage: [password]"
- }
- [ -z "$1" ] && { help; exit 1; } || {
- case $1 in
- -*) help; exit ;;
- *) ip=$1;;
- esac
- }
- shift
- ssh.exp "$ip" "$@"
- [ "$?" = "1" ] && ssh-keygen -R $ip && ssh_ivie.exp "$ip" "$@"
expec
ssh.exp
- #!/usr/bin/expect -f
- proc help {} {
- puts {usage: <ivie_ip> [password]}
- }
- if {$argc<1} { help ; exit}
- set ip [ lindex $argv 0 ]
- set password rootroot
- if {$argc==2} { set password [lindex $argv 1] }
- # close the output
- log_user 0
- set timeout 30
- spawn ssh -XY root@$ip
- expect {
- -re ".*:~ # " {}
- "Password: " { send "$password\r" }
- "(yes/no)?" {send "yes\r"; exp_continue}
- "Host key verification failed" { send_user "run: ssh-keygen -R $ip" ; exit 1 }
- timeout {puts "check your ip: $ip"; exit 2}
- eof { puts "check your ip: $ip" ;exit 3 }
- }
- interact
我测试过在一个expect中完成,但是没有成功。还望有谁能够完成,给予指教。