Reply To: tcl FTP package

Homepage Clovertech Forums Read Only Archives Cloverleaf Tcl Library tcl FTP package Reply To: tcl FTP package

#56622
Rob Lindsey
Participant

I have loaded the tcllib1.2 package that contains ftp.

I have written TCL programs outside the engine to use this ftp package.

Here is some code that I use to connect to a client site (of course use at your own risk):

Code:

#!/hci/root3.8.1P/bin/hcitcl

package require FTP 1.2

proc main { } {
   puts “Job started at [clock format [getclock]]”
   puts “”
   set addresses “group”

   set host [hostname]
   if { $host == “test_system” } {
       set server “1.1.1.1”
       set local_out “/clients//OUTBOUND/”
       set local_inb “/clients/
/INBUILD/”
       set local_in “/clients/
/INBOUND/”
       set local_arch “/clients/
/ARCHIVE/”
       set build “/”               ;# building place on
                                                      # remote system
       set inbound “/INBOUNDT/”         ;# inbound dir on remote
       set archive_in “/Archive/”   ;# archive dir on remote
       set archive_out “/Archive/” ;# archive dir on remote
       set outbound “/OUTBOUNDT/”       ;# outbound dir on remote
       set user “
       set passwd “
   } elseif { $host == “prod_system” } {
       set server “”
       set local_out “/clients//OUTBOUND”
       set local_inb “/clients/
/INBUILD”
       set local_in “/clients/
/INBOUND”
       set local_arch “/clients/
/ARCHIVE/”
       set build “”               ;# building place on
                                                      # remote system
       set inbound “”          ;# inbound dir on remote
       set archive_in “”    ;# archive dir on remote
       set archive_out “”  ;# archive dir on remote
       set outbound “”        ;# outbound dir on remote
       set user “”
       set passwd “”
   }

   if { $host == “” } {
       set subject “No host set to go to …. exiting.”
       puts $subject
       set err [error_mail $subject $addresses $subject]
       exit
   }

   # open a new connection
   if {[set conn [FTP::Open $server $user $passwd -progress Progress]] == -1} {
           set subject “Connection refused to $server”
           puts $subject
           set err [error_mail $subject $addresses $subject]
           exit
   }

   cd $local_out

   set return [FTP::Type ascii]

   set locallist [readdir [exec pwd]]

   set return [FTP::Cd $build]     ;# go to where we send the initial file
   if { $return == 0 } {
       set subject “Can not change directory to $build”
       puts $subject
       set err [error_mail $subject $addresses $subject]
       FTP::Close
       exit
   }

   foreach file $locallist {
       if { [file isdirectory $file] } { continue }
       sleep 1

       set return [FTP::Put $file]
       if { $return == 0 } {
           set subject “PUT of file – $file – failed!”
           puts $subject
           set err [error_mail $subject $addresses $subject]
           FTP::Close
           exit
       }

       set to “$inbound$file”

       set return [FTP::Rename $file $to]
       if { $return == 0 } {
           set subject “Rename of file  – $file – to — $to – failed!”
           puts $subject
           set err [error_mail $subject $addresses $subject]
           FTP::Close
           exit
       }

       set newfile “$local_arch$file”

       set err [file rename -force $file $newfile] ;# mv file to archive

   }    ;# end of foreach file $locallist

   ################  go and get files from the system  #################

   set return [FTP::Cd $outbound]
   if { $return == 0 } {
       set subject “Can not change directory to $outbound”
       puts $subject
       set err [error_mail $subject $addresses $subject]
       FTP::Close
       exit
   }

   cd $local_inb

   foreach file [FTP::NList $outbound] {
       puts “file == $filen”
       set tmpfile [lindex [split $file ;] 0]
       set outext [file extension $tmpfile]
       set short [lindex [split $file ] ] 1]
       set return [FTP::Get $file $short]
       if { $return == 0 } {
           set subject “GET of file – $file – failed!”
           puts $subject
           set err [error_mail $subject $addresses $subject]
           FTP::Close
           exit
       }

       set to “$archive_out$file”
       set return [FTP::Rename $file $to]
       if { $return == 0 } {
           set subject “Rename of file  – $file – to — $to – failed!”
           puts $subject
           set err [error_mail $subject $addresses $subject]
           FTP::Close
           exit
       }

       set final “$local_in$file”
       regsub “;” $final “_” final
       set err [file rename -force $file $final]

   }     ;# end of foreach file [FTP::NList $outbound]

   # close connection
   FTP::Close
   puts “”
   puts “Job completed SUCCESSFULLY at [clock format [getclock]]”
   puts “”

}       ;# end of proc main

# define callback
proc Progress {total} {
       puts “$total bytes transfered!”
}

proc error_mail { subject addresses error_text } {

   puts “calling error_mail”
   global error_flag
   set host [exec hostname]
   exec /bin/mail $addresses << "From: FTP_put_get_for_IBT    To: $addresses    From: FTP_put_get_for_IBT    Subject: $subject    host $host, please check!       happened at [exec date]    $error_text" }     ;# end of proc error_mail set err [main] if { $err == 1 } {    puts "errored out" } exit

Hope this gives you some ideas.  

Rob

Forum Statistics

Registered Users
5,115
Forums
28
Topics
9,291
Replies
34,426
Topic Tags
286
Empty Topic Tags
10