1. 程式人生 > >利用expect + sftp 實現遠端主機自動登入及下載

利用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 [email protected]$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可以使程式執行完後留在遠端