#!/usr/bin/expect
set timeout 1800
set IPADDRESS [lindex $argv 0]
set USERNAME [lindex $argv 1]
set PASSWD [lindex $argv 2]
set SRC_FILE [lindex $argv 3]
set DEST_FILE [lindex $argv 4]
spawn echo $SRC_FILE
spawn echo $DEST_FILE
spawn ssh-keygen -R $IPADDRESS
spawn rsync -avz "$USERNAME@$IPADDRESS:$SRC_FILE" "$DEST_FILE"
expect {
-nocase -re "yes/no" {
exp_send "yes\r"
exp_continue
}
-nocase -re "assword:" {
exp_send "$PASSWD\r"
exp_continue
}
-nocase -re "ERROR" {
send_error "Destination must be a directory when copying more than 1 file.\n"
exit 2
}
timeout {
send_error "Connect to sftp server $USERNAME@$IPADDRESS timeout($timeout s).\n"
exit 8
}
eof
set timeout 1800
set IPADDRESS [lindex $argv 0]
set USERNAME [lindex $argv 1]
set PASSWD [lindex $argv 2]
set SRC_FILE [lindex $argv 3]
set DEST_FILE [lindex $argv 4]
spawn echo $SRC_FILE
spawn echo $DEST_FILE
spawn ssh-keygen -R $IPADDRESS
spawn rsync -avz "$USERNAME@$IPADDRESS:$SRC_FILE" "$DEST_FILE"
expect {
-nocase -re "yes/no" {
exp_send "yes\r"
exp_continue
}
-nocase -re "assword:" {
exp_send "$PASSWD\r"
exp_continue
}
-nocase -re "ERROR" {
send_error "Destination must be a directory when copying more than 1 file.\n"
exit 2
}
timeout {
send_error "Connect to sftp server $USERNAME@$IPADDRESS timeout($timeout s).\n"
exit 8
}
eof
}
当然也可以使用scp
#!/usr/bin/expect
set timeout 1800
set ADDR [lindex $argv 0]
set LOGIN [lindex $argv 1]
set PASSWD [lindex $argv 2]
set DEST_FILE [lindex $argv 3]
set SRC_FILE [lindex $argv 4]
spawn ssh-keygen -R $ADDR
spawn scp -rp "$LOGIN@$ADDR:$SRC_FILE" "$DEST_FILE"
expect {
-re ".*es.*o.*" {
exp_send "yes\n"
exp_continue
}
-re ".*sword.*" {
exp_send "$PASSWD\n"
exp_continue
}
-re ".*100%.*" { exit 0 }
-re ".*No such .*" { exit 1 }
timeout { exit 2 }
eof
}