FTP自動実行(cron&expect)

基本的な流れ
spawnでftp起動
expectで応答待ち
sendで入力文字列送信
※expectオプション「-re」は正規表現利用時

以下参考スクリプト
(cronなどで実行する)

#!/usr/bin/expect

set host “hostname”
set user “username”
set pass “password”
set file “filename”
set prompt “Name.*”
set lcd_cmd “lcd /local/to/dir”

spawn ftp $host;

expect -re “$prompt”
send “$user\r”
expect “Password:”
send “$pass\r”
expect “ftp>”
send “$lcd_cmd\r”
expect “ftp>”
send “get $file\r”
expect “ftp>”
send “bye\r”

interact
exit

====================================
ディフォルトのタイムアウトは10秒。
大きいファイルを転送する場合などは
expect -timeout -1 “ftp>”
などとしてタイムアウトしないようにする。
(任意の値(秒数)も設定可能)

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA