利用expect + sftp 實現遠程主機自動登錄及下載

利用expect + sftp 實現遠程主機自動登錄及下載:

#!/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

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可以使程序執行完後留在遠端
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章