spawn bash -c "ssh -t root@$argv"
expect "* (yes/no)*"
send "yes/r"
expect -timeout 1
expect "* password:*"
send "123456/r"
expect -timeout 5
send "export DATE=`date +%F-%R`/r"
expect -timeout 1
send "export IP=`ifconfig |grep /"inet addr/"|grep /"10/"|cut -d ':' -f2|cut -d ' ' -f1`/r"
expect -timeout 1
send "tar cvf /root//$IP--/$DATE.tgz /root/workspace/r"
expect -timeout 1
send "scp -r /root/*.tgz serverIP:/root/back/r"
expect "* (yes/no)*"
send "yes/r"
expect -timeout 1
expect "* password:*"
send "123456/r"
expect eof
用个笨方法
CODE:
spawn ssh -t root@$argv
expect {
"* (yes/no)*" { send "yes/r" ; set sshkey 1}
"* password:*" { send "123456/r" }
if $sshkey!=1 {
expect "* password:*"
send "123456/r" }
expect {
"* (yes/no)*" { send "yes/r" ; set sshkey 1}
"* password:*" { send "123456/r" }
if $sshkey!=1 {
expect "* password:*"
send "123456/r" }