#!/opt/ActiveTcl-8.4/bin/tclsh
package require ftp
package require md5

#=======================================================================
#			FTP
#=======================================================================

proc ftp_start {args} {
		array set myarr $args
		set server $myarr(-server)
		set account $myarr(-account)
		set passwd $myarr(-passwd)
		set timeout $myarr(-timeout)
		set file $myarr(-file)
		set md5 $myarr(-md5)
		
		set i 0
		while {1} {
			after 2000
			catch {ftp::Open $server $account $passwd -timeout $timeout} ch
			if {$ch=="-1"} {
				puts "[clock format [clock seconds] -format %Y%m%d_%H:%M:%S]=============can't open ftp session"
				continue
			}
			catch {ftp::Get $ch $file $file} ret
			if {!$ret} {
				puts "[clock format [clock seconds] -format %Y%m%d_%H:%M:%S]===========can't get file"
				catch {ftp::Close $ch}
				continue
			}
			catch {ftp::Close $ch}
			puts [info vars ::ftp::*]	        
			if {[string compare -nocase [md5::md5 -hex -file ${file} $md5]!=0} {
				puts "[clock format [clock seconds] -format %Y%m%d_%H:%M:%S]============there is a data loss: $file"
			}
		}
	
	return
}

###########main

set ftp_server 1.1.130.3
set timeout 30
set file 2M
set md5ck(2M) "3fdd51e2744a4d4fd09b4ddd8c70a883"


ftp_start -server $ftp_server -account "ftpuser" -passwd "123" -timeout $timeout -file $file -md5 $md5ck($file)]

