auto-ssh-keygen.exp
- #!/usr/bin/expect -f
- # optionally set key passwd
- set pw ""
- set timeout 5
- spawn ssh-keygen -q
- expect "*save*id_rsa*"
- send "\n"
- # file is exist tip
- expect "*y/n*" { send "y\n" }
- expect "*passphrase*"
- send "$pw\n"
- expect "*passphrase*" { send "$pw\n" }
- #expect "*#|$*"
- expect eof
nopwdlogin.exp
- #!/usr/bin/expect -f
- ## generate public/private key
- exec ./auto-ssh-keygen.exp
- ## send public keys to target
- set ip [lindex $argv 0]
- set pwd [lindex $argv 1]
- #set usr root
- set filecontent [exec cat /root/.ssh/id_rsa.pub]
- set destdir ~/.ssh
- set timeout 3
- spawn ssh $ip
- # exp_continue # 继续下一个
- expect {
- "(yes/no)?" {send "yes\r";exp_continue}
- "password:" {send "$pwd\r"}
- }
- # handle dir .ssh
- expect "*#*" {send "if ! test -d $destdir; then mkdir $destdir; fi\n"}
- expect "*#*" {send "echo -n $filecontent >> $destdir/authorized_keys \n"}
- expect eof