home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / perl / 5320 < prev    next >
Encoding:
Text File  |  1992-08-14  |  2.6 KB  |  88 lines

  1. Path: sparky!uunet!mcsun!uknet!ox-prg!bush
  2. From: bush@ecs.ox.ac.uk (Mark Bush)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: How to wait for a pipe to complete a task?
  5. Message-ID: <4288@inca.comlab.ox.ac.uk>
  6. Date: 14 Aug 92 16:41:26 GMT
  7. References: <1992Aug14.145310.29093@raid.dell.com>
  8. Sender: news@comlab.ox.ac.uk
  9. Organization: Oxford University Computing Laboratory
  10. Lines: 76
  11.  
  12. In article <1992Aug14.145310.29093@raid.dell.com> painter@manufing.dell.com (Tom Painter) writes:
  13. #I need to write a program with batch FTP.  I keep hitting the following
  14.  
  15. There are probably a number of ways of doing what you want, however the
  16. following script which uses chat2 is what I use for doing backgrounded FTP
  17. jobs at suitable hours.  I have a version of chat2 which I call chat2verbose
  18. which is obtained using the following patch:
  19.  
  20. *** /usr/local/lib/perl/chat2.pl        Mon Dec  9 16:32:01 1991
  21. --- /usr/local/lib/perl/chat2verbose.pl Fri Dec 13 18:52:49 1991
  22. ***************
  23. *** 221,226 ****
  24. --- 221,227 ----
  25.                                 $nread = sysread(S, $thisbuf, 1024);
  26.                                 if ($nread > 0) {
  27.                                         $S .= $thisbuf;
  28. +                                       printf STDERR "%s", $thisbuf;
  29.                                 } else {
  30.                                         $eof++, redo LOOP; # any error is also eof
  31.                                 }
  32.  
  33. which just helps to produce a record on STDERR of the entire session.
  34.  
  35. Anyway, the important part about the script is that it doesn't send the next
  36. command until it gets the ftp prompt back, so after a put, it will be safe
  37. to delete the file (assuming it transfered ok, but that's another story! 8*).
  38.  
  39. An example script would be:
  40. ---------------
  41. open src.doc.ic.ac.uk
  42. cd gnu
  43. bin
  44. get perl-4.035.tar.Z
  45. quit
  46. ---------------
  47.  
  48. Update $name and $password accordingly, though it is unwise to do this for
  49. non-anonymous ftp!...
  50.  
  51. Changing the script for your purposes should be fairly straight-forward.
  52.  
  53. Mark
  54.  
  55. #!/usr/local/bin/perl
  56.  
  57. require 'chat2verbose.pl';
  58.  
  59. $SCRIPT = shift || "ftp.script";
  60. open(SCRIPT) || die "Can't open $SCRIPT: $!\n";
  61. @messages = <SCRIPT>;
  62. close(SCRIPT);
  63.  
  64. $handle = &chat'open_proc("/usr/ucb/ftp");
  65.  
  66. $count = 0;
  67. $name = "anonymous\n";
  68. $password = "bush@comlab.ox.ac.uk\n";    # My e-mail address
  69.  
  70. while (1)
  71. {
  72.     &chat'expect($handle, 2,
  73.     'EOF', '&chat\'close($handle); exit;',
  74.     '^Name.*$', '&chat\'print($handle, $name)',
  75.     '^ftp\n', '',
  76.     '^Password.*$', '&chat\'print($handle, $password)',
  77.     '^\n', '',
  78.     '^ftp> $', '&sendinfo',
  79.     '^.+\n', '',
  80.     '^.+$', ''
  81.     );
  82. }
  83.  
  84. sub sendinfo
  85. {
  86.     &chat'print($handle, $messages[$count++]);
  87. }
  88.