#!/bin/sh -- # A comment mentioning perl, indented bash's sake. eval 'exec perl -S $0 ${1+"$@"}' if 0; chop($Program = `basename $0`); $Usage = "Usage: $Program url "; $mode = 'file'; if ( $ARGV[0] eq '-d' ) { $mode = 'dir'; shift; } elsif ( $ARGV[0] eq '-l' ) { $mode = 'listdir'; shift; } $url = $ARGV[0]; $localfile = $ARGV[1]; if ( $url =~ m,//([^/]+)(/.*)$, ) { $host = $1; $file = $2; } if ( $localfile eq '' ) { $localfile = $file; $localfile =~ s,^.*/,,; } print STDERR "\n$Program starting ...\n"; print STDERR " host: $host\n"; print STDERR " remote-file: $file\n"; print STDERR " local-file: $localfile\n"; $tmp = "/tmp/$Program.$$"; #require 'open2.pl'; #$ftp_pid = open(FTP, "|ftp $host 1> $tmp 2>&1"); $ftp_pid = open(FTP, "|ftp $host"); die "$!" unless $ftp_pid; #$tail_kid = system("sleep 2; tail -f $tmp &"); select(FTP); $| = 1; select(STDERR); $| = 1; select(STDOUT); $| = 1; sleep 1; $netrc = "$ENV{HOME}/.netrc"; if ( -f "$netrc" ) { chop($has_passive = `grep passive $netrc`); chop($has_binary = `grep binary $netrc`); } print FTP "passive\n" unless $has_passive; sleep 1; print FTP "binary\n" unless $has_binary; sleep 1; if ( $mode eq 'dir' ) { chop($parent = `dirname $file`); chop($dir = `basename $file`); print FTP "cd $parent\n"; sleep 1; print FTP "get $dir.tar \"|tar -xvvf -\"\n"; } elsif ( $mode eq 'listdir' ) { chop($parent = `dirname $file`); chop($dir = `basename $file`); print FTP "cd $file\n"; sleep 1; print FTP "dir\n"; } else { print FTP "get $file $localfile\n"; } sleep 1; print FTP "quit\n"; sleep 1; wait; system("ls -l $localfile") unless $mode eq 'listdir'; close(FTP); kill 9, $ftp_pid; exit 0; sub WaitFor { local($pattern, $handle, $mode) = @_; local($c, $str, $db); $db = 1; &msg("\nINTO WaitFor (/$pattern/, $handle, $mode)\n") if $db; while (1) { $c = getc($handle); $str .= $c; &msg($c) if $mode =~ /echo/; last if $str =~ /$pattern/; } &msg("GOTIT WaitFor ($str)\n") if $db; return $str; } sub msg { print STDERR @_; }