LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1239|回复: 2

如何写一个shell程序,实现ftp登陆主机后或许某个目录里的内容[已解决]

[复制链接]
发表于 2004-11-26 16:13:24 | 显示全部楼层 |阅读模式
我本来象直接人工输入get,但碰到两个问题
1。要或许的目录名是长文件名(并有空格)
2。我要或许该目录及其下所有文件子目录

而好象mget没发get 子目录内容

请教这个shell程序该怎么写,我刚接触linux,还请高手不吝赐教,感谢感谢
发表于 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*>*"
 楼主| 发表于 2004-11-30 09:06:50 | 显示全部楼层
谢谢,刚学,这个对我来说难了点,呵呵。得找本书消化消化
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表