简单写的一个复用性高的脚本,备份下吧。
#!/usr/bin/expect
set REMOTEIP [lindex $argv 0]
set USER [lindex $argv 1]
set PWD [lindex $argv 2]
set PATH [lindex $argv 3]
set FILE [lindex $argv 4]
set DIR [lindex $argv 5]
set timeout 30
spawn ssh ${USER}@$REMOTEIP
expect "password"
send "$PWD\r"
expect "\\$"
send "cd ${PATH}\r"
expect "\\$"
send "tar cfz ${FILE} ${DIR}\r"
expect "\\$"
send "exit\r\n"
expect eof
set timeout 30
spawn sftp ${USER}@$REMOTEIP
expect "password"
send "$PWD\r"
expect ">"
send "cd ${PATH}\r"
expect ">"
send "get ${FILE}\r"
expect ">"
send "\\rm ${FILE}\r"
expect ">"
send "exit\r\n"
expect eof