利用expect + sftp 实现远程主机自动登录及下载

利用expect + sftp 实现远程主机自动登录及下载:
(ssh是一样的)

#!/usr/bin/expect -f
## Create by Cyril.
##  "Usage:./downFilesFromStation USER PWD RemoteIP SourcePath DownloadPath"

if {$argc<5} {
        puts stderr "Usage:./downFilesFromStation USER PWD RemoteIP SourcePath DownloadPath"
        exit 1
}
set timeout 5
set USER [lindex $argv 0]  
set PWD [lindex $argv 1]
set RemoteIP [lindex $argv 2]
set SourcePath [lindex $argv 3]
set TargetPath [lindex $argv 4]
set cmd_prompt "sftp>"

spawn sftp $USER@$RemoteIP
# spawn ssh $USER@$RemoteIP

expect {
    "Are you sure you want to continue connecting (yes/no)?"  { send "yes\r"; }
}
expect {
    "Password:"  {send "${PWD}\r"; }
}
expect {
    "sftp>"  {send "get -r ${SourcePath} ${TargetPath} \r";}
}
expect {
    "sftp>" {send "exit \r"}
}
##interact ##interact可以使程序执行完后留在远端

猜你喜欢

转载自blog.csdn.net/u011865919/article/details/81365351