Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

using system or Shell

by lanier (Initiate)
on Sep 05, 2002 at 16:41 UTC ( [id://195435]=perlquestion: print w/replies, xml ) Need Help??

lanier has asked for the wisdom of the Perl Monks concerning the following question:

I am new to perl programming. I am trying to create a program that will test ftp for several different servers. I do system(ftp servername) then I want to someone input the user name, password, test a file, delete the file, and close the ftp without having to type that info into the command line. I tried to pipe the info in like mylogin|system(ftp servername) when i tried using Shell nothing happened at all.

Replies are listed 'Best First'.
Re: using system or Shell
by Zaxo (Archbishop) on Sep 05, 2002 at 16:48 UTC

    Once you fire a system call you don't get to interact with it. LWP or Net::FTP will let you do that in a more shell-like way.

    After Compline,
    Zaxo

Re: using system or Shell
by Snuggle (Friar) on Sep 05, 2002 at 16:49 UTC
    Check out NET::FTP. It does everything and the kitchen sink with the FTP protocol.

    Anyway, no drug, not even alcohol, causes the fundamental ills of society. If we're looking for the source of our troubles, we shouldn't test people for drugs, we should test them for stupidity, ignorance, greed and love of power.

    --P. J. O'Rourke
Re: using system or Shell
by davorg (Chancellor) on Sep 05, 2002 at 16:50 UTC

    You should probably be looking at the Net::FTP module.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: using system or Shell
by rbi (Monk) on Sep 05, 2002 at 16:53 UTC
    You might find this useful, I hope:
    #!/bin/sh : # @(#) transfer Transfer files via ftp to/from remote host. # Author: James W. Brown, July 1992. PATH=$PATH:/usr/sbin; export PATH # Add directory containing ping exit_status=0 # No error condition exit code lflag="" # Don't use log by default tdirection=mget # Retrieve file by default tmode=ascii # Use ASCII representation type by default LogFile=$HOME/.transfer_log # Log file path usage_exit(){ cat << EOF >&2 Usage: `basename $0` [-p] [-b] [-l] [-s] [-r rem-dir] -h host file... -p Put instead of take the file -b Use binary instead of ASCII mode -l Store messages from ftp in log file -s Silent mode ( no loging ) -r rem-dir Specify remote directory instead of default (.) -h host Remote host name file... File(s) to be transferred EOF exit_status=1 ; exit } # Set trap to restore .netrc file: trap '[ -f $HOME/.netrc% ] && mv $HOME/.netrc% $HOME/.netrc; exit $exit_status' 0 1 2 3 15 # Process command-line options: if [ "$OPTIND" = 1 ]; then # can use getopts while getopts bh:lpr:s options; do echo $options case "$options" in b) tmode=binary ;; h) host=$OPTARG ;; l) lflag=yes ;; p) tdirection=mput ;; r) remdir=$OPTARG ;; s) LogFile=/dev/null; lflag=yes;; \?) usage_exit ;; esac done shift `expr $OPTIND - 1` # shift options of the way else # getopts not available while [ $# -gt 0 ]; do case $1 in -b) tmode=binary ; shift ;; -h) if [ ! "$2" ]; then echo "-h option requires an argument" >&2 usage_exit else host=$2 ; shift ; shift fi ;; -l) lflag=yes ; shift ;; -s) LogFile=/dev/null; lflag=yes ; shift ;; -p) tdirection=mput ; shift ;; -r) if [ ! "$2" ]; then echo "-r option requires an argument" >&2 usage_exit else remdir=$2 ; shift ; shift fi ;; --) shift; break ;; # end of option list -*) echo "Unrecognized option \"$1\"" >&2 usage_exit ;; *) break ;; # saw first non-option argument esac done fi # Check for non-option (file) argument(s): case $# in 0) echo "Must specify at least one file argument" >&2 usage_exit ;; esac # Check for remote system name, remote directory: if [ ! "$host" ]; then echo "You must specify a remote host machine." >&2 usage_exit elif [ ! "$remdir" ]; then # If no remote directory, then remdir="." # use current directory fi # Echo a packet to see if remote host is reachable: if ping $host 64 1 >/dev/null 2>&1; then # if ping $host >/dev/null 2>&1; then : # No problem else echo "$host either down or doesn't exist." >&2 ; exit_status=2; exit fi # Check .netrc file: if [ -f $HOME/.netrc ]; then hostinfo=`grep $host ${HOME}/.netrc` if [ ! "$hostinfo" ]; then echo "No $host entry found in $HOME/.netrc, exiting..." >&2 exit_status=4 ; exit fi mv $HOME/.netrc $HOME/.netrc% # Save original file # Create new .netrc that's only accessible by owner: touch $HOME/.netrc chmod go-rw $HOME/.netrc else echo "$HOME/.netrc not found, exiting ..." >&2; exit_status=3 ; exit fi # Put the set-up information into .netrc: cat << EOF >> $HOME/.netrc $hostinfo macdef init cd $remdir $tmode prompt $tdirection $* quit EOF # Log transaction in file or on standard output: if [ "$lflag" ]; then date >> $LogFile # note date/time ftp $host >> $LogFile # log ftp messages else ftp $host fi

    ciao,
    Roberto

    "A child of five could understand this. Fetch me a child of five." (G.Marx)

      I think the original poster meant the Shell module, and not a shell script, actually. :)

        which doesn't change the fact that Net::FTP is what he needs... ;)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://195435]
Approved by Snuggle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-24 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found