Re: Using FTP Without any Modules
by blazar (Canon) on May 18, 2006 at 11:33 UTC
|
- When people ask about doing X "without any modules", that's generally because they ignore that they can install them locally, as users, so consider using some module in the first place;
- you can always use, say, system and friends;
- unless I'm mistaken the shell script you supplied does nothing but setting some variables and printing some messages to STDOUT.
| [reply] |
|
|
echo "open $IP
quote USER $user
quote PASS $pass
lcd
" |$ftp -n
pipes the commands through the ftp command.
/J\ | [reply] [d/l] |
|
|
See A Timely Start for an exceptional case where doing FTP through a shell script was preferred over doing it through Perl.
| [reply] |
|
|
I dunno... My first reaction to "without any modules" tends to be "smells like homework" unless there's a reason given.
In any case, if "without any modules" actually means "without installing any modules from CPAN", there are modules such as Socket in the core Perl distribution which can be used to implement FTP within your own code if you don't feel like using system, etc.
| [reply] |
Re: Using FTP Without any Modules
by turo (Friar) on May 18, 2006 at 11:39 UTC
|
blazar, maybe he wants to migrate from ksh to an oister shell :-P jejeje
anyway, i haven't sleept too much today, so for keeping me on, i decided to translate it:
#!/usr/bin/perl
# Give port or mention server name
my $ip='some.machine';
#User ID to login into remote server
my $user='anonymous';
#Password to login in remote server
my $pass='super-secret';
open (FTP, "|-", "/usr/bin/ftp -n");
print FTP <<EOF ;
open $ip
quote user $user
quote pass $pass
lcd
bye
EOF
print "*$_\n" while (<FTP>);
close FTP;
perl -Te 'print map { chr((ord)-((10,20,2,7)[$i++])) } split //,"turo"'
Janitored by Corion: Changed machines and login/password | [reply] [d/l] [select] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: Using FTP Without any Modules
by sgifford (Prior) on May 18, 2006 at 15:57 UTC
|
You can do essentially the same thing from Perl like this (untested):
open(FTP,'| ftp -n ')
or die "Couldn't run FTP: $!\n";
my $ofh = select(FTP); $|=1; select($ofh);
print FTP "open $IP\n";
print FTP "quote USER $user\n";
print FTP "quote PASS $pass\n";
print FTP "lcd\n";
# and so forth
But you probably are better off installing Net::FTP, if nothing else for better error handling and portability. It's a pure-Perl module, so should be easy to install even without any special system privileges.
| [reply] [d/l] |
Re: Using FTP Without any Modules
by TrekNoid (Pilgrim) on May 18, 2006 at 16:07 UTC
|
Here's a snippet of code I've been using for a while, that cleans up a series of FTP sites by spinning through associative array (one entry for each site).
I wrote this years ago, before I knew any better way to do it, and it's been working fine.
To completely understand the code, you need to know that I have two arrays for each site... one stores the site address, and the other stores the login info.
So for ftp site 'example', then, you'd have:
$address{'example'} = "ftp.foobar.com";
$userpwd{'example'} = " user foo bar\n";
The code snippet to get all the files reads as follows:
$ftpcmd = "ftp -n " . $address{$myadd};
$ftpstr = $userpwd{$myadd};
open(TST,"|$ftpcmd");
print TST $ftpstr;
print TST " prompt\n";
print TST " mget *\n";
close(TST);
I don't know that it's the best way to do it, but it's been working for me... I'd definately be open to suggestions for improving it.
Trek
| [reply] [d/l] [select] |
Re: Using FTP Without any Modules
by radiantmatrix (Parson) on May 18, 2006 at 19:22 UTC
|
Yeah, you can do this without modules, but why would you? Is it that you want to do it without having to install any modules?
If so, you should know that Net::FTP comes with Perl 5, so why not just use that?
| [reply] |
Re: Using FTP Without any Modules
by serf (Chaplain) on Oct 26, 2006 at 08:23 UTC
|
Hi perl_devel,
are you Figo Sub - the person that Madhulika wrote this for? Are you Madhulika and you found it at your work?
If you are posting other people's code then you should say it is not yours and where you got it from.
I decided to have a Google for this because I figured someone who would have written that shell could have figured out how to do exactly the same thing in Perl.
Original post (dated 12/10/2003)
#!/bin/ksh
Email1="myname@compname.com"
#Path of Log file
logdir="/log/ftp"
# Path of ftp executable
ftp="/usr/bin/ftp"
# Give port or mention server name
IP="111.111.111.111"
#User ID to login into remote server
user="ftp"
#Password to login in remote server
pass="ftp"
#Mention the path where the file to FTP is lying in the current server
ifile="/mainfold/level2fold/filetoftp"
#Mention the path where the FILE need to FTPied in the remote server
ofile="xyz/filename"
#Mention type of data transfer "asc" or "bin"
type="asc"
# this will take the name of your file
myname=`basename $0`
#selects the mode
verbose="verbose"
#date of FTP - Current date
dd=`date +%d`
# Creating a log file with datestamp
log="$logdir/$myname.$dd.log"
host=`hostname`
rc=0
boj=`date`
#ftp block starting - all will be written to log file
exec 1>$log 2>&1
echo "---------------------------------------------------"
echo " Begin FTP Parameters "
echo "---------------------------------------------------"
echo "Email Sent To $Email1 "
echo "Destination Machine: $IP"
echo "User ID: $user"
echo "Password: ##############"
echo "Destination File: $ofile"
echo "---------------------------------------------------"
echo " End FTP Parameters "
echo "---------------------------------------------------"
echo " Begin FTP Session "
echo "---------------------------------------------------"
echo "open $IP
quote USER $user
quote PASS $pass
$verbose
$type
put $ifile $ofile
close
quit" |$ftp -n
echo "---------------------------------------------------"
echo " End FTP Session "
echo "---------------------------------------------------"
# FTP block ends
#Check the log file for FTP status
foo=`grep -i "cannot" $log`
if [ "$?" -eq "0" ] ; then
rc=1
status="Destination file does not exist\n"
fi
foo=`grep -i "does not" $log`
if [ "$?" -eq "0" ] ; then
rc=1
status="${status}Source file does not exist\n"
fi
foo=`grep -i "killed" $log`
if [ "$?" -eq "0" ] ; then
rc=1
status="${status}File transfer process has abended\n"
fi
foo=`grep -i "space" $log`
if [ "$?" -eq "0" ] ; then
rc=1
status="${status}Ran out of disk space before completion of copy\n"
fi
if [ "$rc" -eq "0" ] ; then
status="Successful"
fi
# find out the time to ftp.
eoj=`date`
echo "\nJob start time: $boj"
echo "Job end time: $eoj"
echo "\nResult code: $rc ($status)"
#Mail the status to the email address specified.
mailx -s "FTP results from $myname" $Email1 <$log
exit 0
| [reply] [d/l] |
|
|
He never said the code was his own, only that he was using it and that he needed help with it.
| [reply] |