#!/usr/bin/expect -f
# vi:set ts=8 sw=4 noet sta:
#
# Author: dearvoid (MSN: )
#
# $Source: /cvsroot/bin/expect/enable-auto-ssh.exp,v $
# $Revision: 1.5 $
#
## variables
set prefix "\033\[1;31m>>>\033\[0m"
proc usage {} {
regsub ".*/" $::argv0 "" name
send_user "Usage:\n"
send_user " $name \[user@]host password\n"
send_user "\n"
send_user "Report bugs to <\033\[1;31mdearvoid@263.net\033\[0m>\n"
exit 1
}
proc check_id_files {} {
if {! [file exists $::id_file]} {
send_user "$::prefix id file not found, try creating ...\n"
if {[catch { spawn ssh-keygen -t rsa } error]} {
send_error "$::prefix $error\n"
exit 1
}
expect -nocase -re "\(.*\):"
send -- "\r"
expect -nocase -re "passphrase.*:"
send -- "\r"
expect -nocase -re "passphrase.*again:"
send -- "\r"
expect eof
send_user "$::prefix id file successfully created\n"
}
}
proc remove_known_hosts_entry {host} {
regsub ".*/" $::argv0 "" name
set tmp_file "/tmp/$name.tmp"
set known_hosts "$::env(HOME)/.ssh/known_hosts"
send_user "$::prefix trying to remove '$host' from ~/.ssh/known_hosts ... "
if {[catch {
set fd_known_hosts [open $known_hosts r]
set fdTmp [open $tmp_file w]
while 1 {
gets $fd_known_hosts line
if [eof $fd_known_hosts] {
break
}
if [regexp "(\[^, ]+,)*${host}(,\[^, ]+)* " $line] {
continue
}
puts $fdTmp $line
}
close $fd_known_hosts
close $fdTmp
file rename -force $tmp_file $known_hosts
send_user "OK\n"
} error]} {
send_user "failed\n"
send_user "$::prefix $error\n"
exit 1
}
}
## get host and password from command line parameters
if {[llength $argv] != 2} {
usage
}
set [lindex $argv 0]
set passwd [lindex $argv 1]
## create public key file if not found
set id_file "$env(HOME)/.ssh/id_rsa.pub"
check_id_files
## ssh to host
set yes_no 0
set ok_string SUCCESS
set timeout 5
set done 0
while {!$done} {
spawn ssh } echo $ok_string
expect {
-nocase -re "yes/no" {
set yes_no 1
send -- "yes\r"
set done 1
}
-nocase -re "password: " {
set done 1
}
$ok_string {
send_user "$prefix ok\n"
exit 0
}
"@@@@@@@@@@@@@@@@@@@@" {
expect eof
set indexOfAtSign [string first "@" }]
incr indexOfAtSign
set hostname [string range } $indexOfAtSign end]
remove_known_hosts_entry $hostname
}
eof {
send_error "$prefix failed\n"
exit 1
}
timeout {
send_error "$prefix timeout\n"
exit 1
}
}
}
if {$yes_no} {
expect {
$ok_string {
send_user "$prefix ok\n"
exit 0
}
-nocase -re "password: " {}
}
}
send -- "$passwd\r"
expect {
-nocase "try again" {
send_error "$prefix passwd error\n"
exit 1
}
$ok_string {}
}
expect eof
## append public key file to remote host's ~/.ssh/authorized_keys
if {[catch {
set IDFILE [open $id_file RDONLY]
set pub_key [read $IDFILE]
close $IDFILE
} error]} {
send_error "$prefix $error\n"
exit 1
}
set pub_key [string trimright $pub_key "\r\n"]
spawn ssh } "cd; mkdir .ssh 2> /dev/null; echo '$pub_key' >> .ssh/authorized_keys"
expect -nocase -re "password:"
send -- "$passwd\r"
expect eof
send_user "$prefix gook luck\n"
## THE END
mkdir -p ~/.ssh
chmod 755 ~/.ssh
/usr/bin/ssh-keygen -t rsa
/usr/bin/ssh-keygen -t dsa
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys
echo -n "wait another host $1 run finish, press twice key continue !!!"
read a
read a
ssh $1 cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
ssh $1 cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys
chmod 644 ~/.ssh/authorized_keys
阅读(1464) | 评论(0) | 转发(0) |