|
发表于 2004-11-26 16:32:49
|
显示全部楼层
expect
可以用TCL/TK 的EXPECT实现,下面是个例子。你把它改改加入到crontab中就可以自动实现了。不懂的地方自己去找书吧。
#!/usr/local/bin/expect
# ftpblob - ftp blob file made by fmc from backup.
# It will start on 14:18 everyday at backup using manager.
# ftpblob wrote on 2003.11.11
# version 1.1
# done by DongHui
# ftpblob is much like ftp except the command. It copy every file at
# /ap/log/blob_file/ directory to DATA CENTER host dtcnbk(168.151.101.20)
# After transfered each file, it delete the file immediately.
set timeout 5
#exec /ap/log/blob_log/colblob.scr
# change to directory where the files need to ftp.
cd /ap/log/blob_send
# get the files number
set n [ exec ls -txr | wc -w ]
# define the array
set file_arr(0) 1
send_user "$n\n"
# get the file name to array elements.
for { set i 1 } { $i <= $n } { incr i } {
set file_arr($i) [ exec ls -rt1 | sed -n "$i,//p" 2> /dev/null ]
send_user "file_arr($i)=$file_arr($i)\n"
}
# start ftp command.
spawn ftp 168.151.101.20
expect "Name* "
send "administrator\r"
expect " assword:*"
send "dtcn2050bs\r"
expect "ftp>"
# begin to log
exec /ap/log/blob_log/ftplog -------start-----
# config the ftp argument.
send "asc\r"
expect "*ftp>*"
send "pwd\r"
expect "*ftp>*"
send "prompt n\r"
expect "*ftp>*"
# get from array and put the file to dtcn, then delete from source directory.
for { set m 1 } { $m <= $n } {incr m} {
send "put $file_arr($m)\r"
expect "*ftp>*"
send "!surm $file_arr($m)\r"
expect "*ftp>*"
exec /ap/log/blob_log/ftplog $file_arr($m)
}
# end the ftp session.
send "bye\r"
expect "221*>*" |
|