home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / tcl / expect / scripts / mirror_dir < prev    next >
Encoding:
Internet Message Format  |  1993-04-12  |  8.7 KB

  1. From: Bowyer Jeff <jbowyer@cis.vutbr.cs>
  2. To: libes@cme.nist.gov (Don Libes)
  3. Subject: expect Program #2
  4. Date: Thu, 8 Apr 1993 15:18:42 +0200 (MET DST)
  5.  
  6. On our anonymous ftp server, we also decided to create a directory that
  7. mirrors all rfc files at nic.ddn.mil.
  8.  
  9. Consequently, I wrote the following expect program to get new files and
  10. delete obsoleted files.
  11.  
  12. Jeff
  13.  
  14. *========================================================================*
  15.  Jeff Bowyer                         EMail: 
  16.  Computing Center             jbowyer@cis.vutbr.cs
  17.  Technical University of Brno
  18.  Udolni 19, 602 00 BRNO
  19.  Czech Republic
  20. *========================================================================*
  21.  
  22.  
  23. THE MAIN SHELL PROGRAM
  24. ----------------------------------------------------------------------
  25. MDIR=/users/jbowyer/maint
  26. RDIR=/users/ftp/pub/doc/rfc
  27.  
  28. expect $MDIR/mirror nic.ddn.mil rfc $RDIR
  29. case $? in
  30. 1)    currmin=`date +%M`
  31.     currhour=`date +%H`
  32.     nextmin=`expr $currmin + 15`
  33.     nexthour=$currhour
  34.     if test $nextmin -ge 60
  35.     then
  36.         nexthour=`expr $nexthour + 1`
  37.         nextmin=`expr $nextmin - 60`
  38.     fi
  39.     if test $nexthour -eq 24
  40.     then
  41.         nexthour=0
  42.     fi
  43.     at $nexthour:$nextmin $MDIR/rfcmirror ;;
  44. esac
  45. ----------------------------------------------------------------------
  46.  
  47.  
  48. THE EXPECT PROGRAM
  49. ----------------------------------------------------------------------
  50. match_max -d 100000        ;# max size of a directory listing
  51.  
  52. set destdir "nodir"
  53. set fname "nofile"
  54.  
  55. set currmonth 0
  56. set curryear 0
  57.  
  58. set mday 0
  59. set mmonth 0
  60. set myear 0
  61.  
  62. set localrfcs ""
  63. set needrfcs ""
  64.  
  65. proc initexpect {} {
  66.     expect_before {
  67.                     -re "unreachable*" {
  68.                                          exit 1
  69.                                        }
  70.                     -re "closed connection*" {
  71.                            exec rm -f $destdir/$fname
  72.                                                exit 1
  73.                                              }
  74.                     -re "Connection timed out*" {
  75.                               exec rm -f $destdir/$fname
  76.                               send "quit\r"
  77.                                                   exit 1
  78.                                                 }
  79.                     timeout    {
  80.                       exec rm -f $destdir/$fname
  81.                   send "quit\r"
  82.                   exit 1 
  83.                             }
  84.                     eof {
  85.                       exec rm -f $destdir/$fname
  86.                           exit 1
  87.                         }
  88.                   }
  89. }
  90.  
  91. proc getname {line} {
  92.     # if it's a symbolic link, return local name
  93.     set i [lsearch line "->"]
  94.     if {-1==$i} {
  95.          # not a sym link
  96.          # return last token of line as name, and strip off newline at end
  97.          return [lindex $line 8]
  98.     } else {
  99.          # sym link, return "a" of "a -> b"
  100.          return [lindex $line [expr $i-1]]
  101.     }
  102. }
  103.  
  104. proc getmonth {line} {
  105.     set fmonth [lindex $line [expr [llength $line]-4]]
  106.         return [expr [lsearch {Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec} $fmonth]+1]
  107. }
  108.  
  109. proc getday {line} {
  110.     return [lindex $line [expr [llength $line]-3]]
  111. }
  112.  
  113. proc getyear {line fmonth} {
  114.     global currmonth curryear
  115.     
  116.     set fyear [lindex $line [expr [llength $line]-2]]
  117.     if [regexp ".*:.*" $fyear] {
  118.         if {$fmonth>$currmonth} {
  119.             return [expr $curryear-1]
  120.         } else {
  121.             return $curryear
  122.             }    
  123.     } else {
  124.         return $fyear
  125.     }
  126. }
  127.  
  128. proc buildllist {thedir} {
  129.     global fname localrfcs
  130.  
  131.     send "anonymous\r"
  132.     expect {
  133.              -re "Password:*"
  134.            }
  135.     send "jbowyer@cis.vutbr.cs\r"
  136.     expect {
  137.              "ok*ftp>*"
  138.            }
  139.     send "cd $thedir\r"
  140.     expect {
  141.               "CWD*success*" {
  142.                                     continue -expect
  143.                                    }
  144.              "ftp>*"
  145.            }
  146.     send "dir\r"
  147.     expect {
  148.              "ftp>*"
  149.            }
  150.  
  151.     set buf $expect_out(buffer)\n
  152.     close
  153.  
  154.     for {} 1 {}    {
  155.         set split_buf [split $buf ""]
  156.  
  157.         set i [string first "\n" $buf]
  158.         set line [join [lrange $split_buf 0 $i] ""]
  159.         set buf [join [lrange $split_buf [expr 1+$i] end] ""]
  160.  
  161.         set token [lindex $line 0]
  162.         case $token in    {
  163.             dir\r    {
  164.                     # original command
  165.                 }
  166.             200    {
  167.                     # command successful
  168.                 }
  169.             150    {
  170.                     # opening data connection
  171.                 }
  172.             total    {
  173.                     # directory header
  174.                 }
  175.             226    {
  176.                     # transfer complete, succeeded!
  177.                 }
  178.             ftp>    {
  179.                     return 1
  180.                 }
  181.             .    {
  182.                     # unreadable
  183.                 }
  184.             default {
  185.                     # either file or directory
  186.                     set fname [getname $line]
  187.                     if [regexp -nocase "^rfc0|^rfc1|^rfc2|^rfc3|^rfc4|^rfc5|^rfc6|^rfc7|^rfc8|^rfc9" $fname] {
  188.                       set lday [getday $line]
  189.                       set lmonth [getmonth $line]
  190.                       set lyear [getyear $line $lmonth]
  191.                       lappend localrfcs [list $fname $lday $lmonth $lyear]
  192.                     }
  193.                 }
  194.                 }
  195.         }
  196.     return 0
  197. }
  198.  
  199. proc buildrlist {thedir} {
  200.     global fname masterrfcs
  201.  
  202.     send "anonymous\r"
  203.     expect {
  204.              -re "Password:*"
  205.            }
  206.     send "jbowyer@cis.vutbr.cs\r"
  207.     expect {
  208.              "ok*ftp>*"
  209.            }
  210.     send "cd $thedir\r"
  211.     expect {
  212.               "CWD*success*" {
  213.                                     continue -expect
  214.                                    }
  215.              "ftp>*"
  216.            }
  217.     send "dir\r"
  218.     expect {
  219.              "ftp>*"
  220.            }
  221.  
  222.     set buf $expect_out(buffer)\n
  223.     close
  224.  
  225.     for {} 1 {}    {
  226.         set split_buf [split $buf ""]
  227.  
  228.         set i [string first "\n" $buf]
  229.         set line [join [lrange $split_buf 0 $i] ""]
  230.         set buf [join [lrange $split_buf [expr 1+$i] end] ""]
  231.  
  232.         set token [lindex $line 0]
  233.         case $token in    {
  234.             dir\r    {
  235.                     # original command
  236.                 }
  237.             200    {
  238.                     # command successful
  239.                 }
  240.             150    {
  241.                     # opening data connection
  242.                 }
  243.             total    {
  244.                     # directory header
  245.                 }
  246.             226    {
  247.                     # transfer complete, succeeded!
  248.                 }
  249.             ftp>    {
  250.                     return 1
  251.                 }
  252.             .    {
  253.                     # unreadable
  254.                 }
  255.             default {
  256.                     # either file or directory
  257.                     set fname [getname $line]
  258.                     if [regexp -nocase "^rfc0|^rfc1|^rfc2|^rfc3|^rfc4|^rfc5|^rfc6|^rfc7|^rfc8|^rfc9" $fname] {
  259.                       set lday [getday $line]
  260.                       set lmonth [getmonth $line]
  261.                       set lyear [getyear $line $lmonth]
  262.                       lappend masterrfcs [list $fname $lday $lmonth $lyear]
  263.                     }
  264.                 }
  265.                 }
  266.         }
  267.     return 0
  268. }
  269.  
  270. proc getmasterdates {} {
  271.     global masterfile mday mmonth myear
  272.  
  273.     set mday [expr [lindex $masterfile 1]]
  274.     set mmonth [expr [lindex $masterfile 2]]
  275.     set myear [expr [lindex $masterfile 3]]
  276. }
  277.  
  278. proc newmaster {} {
  279.     global masterfile mday mmonth myear localfile
  280.  
  281.     getmasterdates
  282.  
  283.     set lday [expr [lindex $localfile 1]]
  284.     set lmonth [expr [lindex $localfile 2]]
  285.     set lyear [expr [lindex $localfile 3]]
  286.  
  287.     if $myear>$lyear {
  288.       return 1
  289.     } else {
  290.       if $myear==$lyear {
  291.         if $mmonth>$lmonth {
  292.           return 1
  293.         } else {
  294.           if $mmonth==$lmonth {
  295.         if $mday>$lday {
  296.           return 1
  297.         }
  298.           }
  299.         }
  300.       }
  301.     }
  302.     return 0
  303. }
  304.  
  305. proc getrfile {} {
  306.     global destdir fname needfile mday mmonth myear
  307.     
  308.         set fname [lindex $needfile 0]
  309.     
  310.     send "get $fname\r"
  311.     expect {
  312.                  "ftp>*"
  313.                }
  314.     exec chmod "666" $destdir/$fname
  315.     set touchstr [format "%02d%02d0000%s" $mmonth $mday [string range $myear 2 end]]
  316.     exec touch $touchstr $destdir/$fname
  317.     exec chmod "444" $destdir/$fname
  318. }
  319.  
  320. log_file /users/jbowyer/maint/logs/rfclog
  321.  
  322. set destdir [lindex $argv 3]
  323.  
  324. set currmonth [exec date +%m]
  325. set curryear [exec date +%Y]
  326.  
  327. set timeout 900
  328.  
  329. spawn ftp rhino.cis.vutbr.cs
  330. initexpect
  331. expect {
  332.      -re "unknown host"    {
  333.                    exit 1
  334.                 }
  335.          -re "unreachable*"    {
  336.                   exit 1
  337.                 }
  338.          -re "Name*"
  339.        }
  340. if [buildllist "pub/doc/rfc"] {
  341.     spawn ftp [lindex $argv 1]
  342.     initexpect
  343.     expect {
  344.          -re "unknown host"    {
  345.                        exit 1
  346.                     }
  347.              -re "unreachable*"    {
  348.                       exit 1
  349.                     }
  350.              -re "Name*"
  351.            }
  352.     if [buildrlist [lindex $argv 2]] {
  353.         foreach masterfile $masterrfcs {
  354.             set foundrfc 0
  355.             foreach localfile $localrfcs {
  356.                 if 0==[string compare [lindex $masterfile 0] [lindex $localfile 0]] {
  357.                     if [newmaster] {
  358.                         lappend needrfcs $masterfile
  359.                     }
  360.                     set localindex [lsearch $localrfcs $localfile]
  361.                     set localrfcs [concat [lrange $localrfcs 0 [expr $localindex-1]] [lrange $localrfcs [expr $localindex+1] end]]
  362.  
  363.                     set foundrfc 1
  364.                     break
  365.                 }
  366.             }
  367.             if !$foundrfc {
  368.                 lappend needrfcs $masterfile
  369.             }
  370.         }
  371.         if [llength $localrfcs]>0 {
  372.             foreach killfile $localrfcs {
  373.                 exec rm -f $destdir/[lindex $killfile 0]
  374.             }
  375.         }
  376.         if [llength $needrfcs]>0 {
  377.             spawn ftp [lindex $argv 1]
  378.             initexpect
  379.             expect {
  380.                  -re "unknown host"    {
  381.                                exit 1
  382.                             }
  383.                      -re "unreachable*"    {
  384.                               exit 1
  385.                             }
  386.                      -re "Name*"
  387.                    }
  388.             send "anonymous\r"
  389.             expect {
  390.                      -re "Password:*"
  391.                    }
  392.             send "jbowyer@cis.vutbr.cs\r"
  393.             expect {
  394.                      "ok*ftp>*"
  395.                    }
  396.             send "cd [lindex $argv 2]\r"
  397.             expect {
  398.                       "CWD*success*" {
  399.                                             continue -expect
  400.                                            }
  401.                      "ftp>*"
  402.                    }
  403.             send "binary\r"
  404.             expect {
  405.                          "ftp>*"
  406.                        }
  407.             send "hash\r"
  408.             expect {
  409.                          "ftp>*"
  410.                        }
  411.             send "lcd $destdir\r"
  412.             expect {
  413.                          "ftp>*"
  414.                        }
  415.             foreach needfile $needrfcs {
  416.                 getmasterdates
  417.                 getrfile
  418.             }
  419.         }
  420.     }
  421. }
  422.  
  423. exit 0
  424.