|
Home | Switchboard | Unix Administration | Red Hat | TCP/IP Networks | Neoliberalism | Toxic Managers |
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and bastardization of classic Unix |
News | Recommended Links | Reference | Sort in Perl |
Regular expressions | History | Humor | Etc |
|
|
Using Net::FTP, which is a standard Perl module, you can write simple FTP clients that transfer files. Here is an example of a client that connects to a remote FTP server and gets a file from the server:
Simple example:
#!/usr/local/bin/perl -w use Net::FTP; $hostname = 'yourhost'; $username = 'youname'; $password = 'yourpass'; # Hardcode the directory and filename to get $home = `pwd`; $target='/home/usr/public_html'; $filename = 'index.shtml'; # Open the connection to the host
$ftp = Net::FTP->new($hostname) or die("Can't connect to the server\n"); # construct object $ftp->login($username, $password) or die("Can't login to the account $username"); # log in print "Connected to $username\@$hostname \n"; # Now let's put file to the ftp servere and leave
put_file($file);
$ftp->quit;
exit;
sub put_file {
$filename=substr($file, $last);
$path=substr($file,0,$last);
$ftp->cwd($path); $ftp->put($filename);
@ls=$ftp->ls("$path/$filename"; if (index (@ls[0]),$filename) > -1) {
print "File $filename was transferred successfully to the $path directory\n";
return 1;
} else {
print File transfer of $filename faled\n";
return 0; }
More complex variant
FTP clients have also been integrated with most World Wide Web browsers, using ftp:// in place of http://. When the URL points to a directory, the browser displays a listing of the directory, where each filename is a link to that file. When the URL points directly to a file, the remote file is downloaded.#!/usr/local/bin/perl -w use Net::FTP; $hostname = 'yourhost'; $username = 'youname'; $password = 'yourpass'; # Hardcode the directory and filename to get $target='/home/usr/public_html'; $filename = 'index.shtml'; # Open the connection to the hostconnect($hostname,$username,$password);# Now let's put files to the ftp servere and leavewhile($file=<SYSIN>) {chomp($file);$last=rindex($file,"/"); unless($last>0) {print "wrong file $file\n";next;} put_file($file);} # for $ftp->quit;exit;sub connect{$ertype=''; $ftp = Net::FTP->new($hostname) or die("Can't' connect to the server\n"); # construct object $ftp->login($username, $password) or die("Can't login to the account $username"); # log inprint "Connected to $username\@$hostname \n";}sub put_file{my ertype='';$filename=substr($file, $last);$path=substr($file,0,$last);$ftp->cwd($path) || ertype="path $path" doe not exit of target computer\n";return 0 if ($ertype);$ftp->put($filename);if (index ($ftp->ls("$path/$filename"),$filename) > -1) {print "File $filename was transferred successfully to the $path directory\n";return 1;} else {print File transfer of $filename faled\n";return 0; }} # sub
Here's an example that uses Net::FTP to list files from a remote FTP server on a web page, with a link from each file to the URL of the file on the remote site:
The Net::FTP module implements a subset (as shown earlier in this chapter) of the FTP protocol as defined in RFC 959. In addition to providing the methods shown below, the module inherits from Net::Cmd. Some of the Net::FTP methods return an object derived from the dataconn class (which is in turn derived from the IO::Socket::INET class), as noted in the entries for those methods.#!/usr/local/bin/perl -w use Net::FTP; $hostname = 'remotehost.com'; # ftp host $username = 'anonymous'; # username $password = '[email protected]'; # password $home = '/pub'; $ftp = Net::FTP->new($hostname); # Net::FTP constructor $ftp->login($username, $password); # log in w/username and password $pwd = $ftp->pwd; # get current directory # Now, output HTML page. print <<HTML; Content-type: text/html <HTML> <HEAD> <TITLE>Download Files</TITLE> </HEAD> <BODY> <B>Current working directory:</B> $pwd<BR> Files to download: <P> HTML @entries = $ftp->ls($home); # slurp all entries into an array foreach (@entries) { # now, output links for all files in the ftp area # as links print "<INPUT TYPE=hidden NAME=\"files\" VALUE=\"$_\">\n"; print "<A HREF=\"ftp://$hostname$_\">", "<IMG SRC=\"http://www/icons/f.gif\" border=0>\n"; print " $_</A><BR>\n"; } print <<HTML; </BODY> </HTML> HTML $ftp->quit; # end FTP session
The following methods are defined by Net::FTP:
machine
name
Specifies a remote machine to which you want to autologin:
Instead ofmachine remotehost.com
machine
name
, you can specify the single
word default
to match any machine name.
This is usually used for anonymous logins to machines not listed in
.netrc.
login
name
If present, identifies
the user who logs in to the remote machine, where
name
is the login name.
password
passwd
If present, gives the password for the user. The autologin process uses the specified password if the remote server requires one.
account
acctpw
If present, gives an additional password for the user. The autologin process uses this password if the remote server requires one.
macdef
name
If present, defines a macro with the specified name. Net::Netrc simply parses this field, in order to maintain compatibility with FTP.
Here's an example of a typical .netrc entry:
Entering your username and password for remote sites in unencrypted form has serious security implications. Many sites consider .netrc files a violation of security policy and do not allow them. In addition, most FTP clients require that the .netrc file be owned by you and readable only by you, with permissions set to 0400 or 0600. If the permissions aren't correctly set, the autologin process aborts. Net::Netrc follows this security convention - if the permissions and ownership are not correct, the .netrc file isn't read, and a warning message is issued.machine remotehost.com login username password userpasswd
Net::Netrc implements a simple interface to the .netrc file, telling you which hosts you can automatically connect to. It doesn't actually connect to any remote hosts; it's simply used to query your configuration file and return the value of the tokens.
The following methods are supplied by Net::Netrc:
Presentation itself is junk, only comments have value
Re: Beginners guide to
Net::FTP by runrig (Monsignor) on Aug 14, 2002 at 17:16 UTC |
by Aristotle (Chancellor) on Aug 14, 2002 at 21:43 UTC |
Re: Beginners guide to
Net::FTP by jbowshaw (Initiate) on Mar 21, 2008 at 15:24 UTC |
Posted by admin (Graham Ellis), 4 September 2002 One of the most common search engine enquiries that brings people our way is "Perl FTP". It's exceptionally easy to do, but the module you need (Net::FTP) is hidden within a group of other modules in the libnet bundle on the CPAN. That's a lower case "l" for lib, unlike most other CPAN names, so you'll often find it listed like a lost soul on the very end of module lists!
Here's an introductory sample that transfers a file from a host computer called seal to the computer on which the Perl program is running, and reports back the size of the file transferred.
Code: use Net::FTP;
print "retrieving file from seal\n";
$ftpobj = Net::FTP -> new ("seal");
$ftpobj -> login("p1","abc123");
$ftpobj -> cwd ("/export/home/www/cosite/about");
$ftpobj -> get ("melksham.html");
$ftpobj -> quit;
print "file size ",-s "melksham.html"," bytes\n";
Posted by bdulfer (bdulfer), 11 October 2002
Another possibility is LWP::UserAgent.
Example:
#!/perl/bin/perl use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $URL = 'ftp://ftp.abc.de/pub/anyfile'; my $req = HTTP::Request->new(GET => $URL); my $contents; $ua->request($req, sub { ($contents, undef) = @_; # the contents of the file is now in $contents # you can save it to a file, send it via email, and whatnot });
Posted by admin (Graham Ellis), 13 October 2002
Indeed - LWP::UserAgent is an excellent alternative, and has a better user interface in many (but not all) circumstances.Typical Perl - there'e half a dozen ways of doing anything
Posted by gbelday (gbelday), 19 August 2005
How would I ftp to mainframe? I am using the follwing code but the put command isn't working..please help.
print "copying file to mainframe\n";
$ftpobj = Net::FTP -> new ("xxx") or
die "Cannot connect to xxx: $!";
$ftpobj -> login("xxx","xxx") or die "Cannot login to ftp ", $ftpobj->message;
$filename = "xxx";
print "copying file";
$ftpobj -> put ("$filename", "'TST.PSNR003N.ISTTRADE.ALLOC.UPLOAD'
(REPL") or die "copy failed ", $ftpobj->message;
Posted by admin (Graham Ellis), 19 August 2005
Have you got the Net::FTP Module loaded? Have you "use"d it? That's just a guess ... if you have got it loaded, please post up details of how your code fails - that's a piece of vital evidence that will help me advise you better
Posted by gbelday (gbelday), 20 August 2005
Thanks a lot for replying.1) Yes, the Net::FTP module is loading correctly. I check each of the line below by giving invalid source, id, password and I get back messages correctly that it can't connect. When I give the correct id and pw, everything is fine.
2) The code that is actually failing is the put command. This code works fine when I ftp to a solaris box but fails when i ftp to mainframe.
This line below gives a message says invalid parentheses. I don't have the exact error message as I am not at work. I can post the exact message tomorrow.
print "copying file";
$ftpobj -> put ("$filename", "'TST.PSNR003N.ISTTRADE.ALLOC.UPLOAD'
(REPL") or die "copy failed ", $ftpobj->message;
When I changed the code to look like below(i took out the single quotes), i get a message saying invalid dataset name for mainframe... The same code works manually or though a korn shell script. Notice the parentheses missing after REPL...this is the correct syntax.
$ftpobj -> put ("$filename", "TST.PSNR003N.ISTTRADE.ALLOC.UPLOAD (REPL") or die "copy failed ", $ftpobj->message;
In korn shell, the follwing command works
put $filename 'TST.PSNR003N.ISTTRADE.ALLOC.UPLOAD' (REPL
Please help if you can!! I'll post exact message when i get to work
on monday.
Posted by admin (Graham Ellis), 20 August 2005
In your manual put, you give three parameters, the third of which is (REPL. In the automated one, you've included the (REPL as the end of the second parameter. Subtle difference!
It's like me saying I have friends ...
Anne Marie Emma and Louise
.... looks like four friends. But if I say ...
"Anne Marie" "Emma" and "Louise"
.... you can see it's just three.
Solution - remove the [space](REPL from the end of your automated put.
Now ... I'm not actually sure what the third parameter on your put means
(it's FTP client dependent); it may simply be a request to replace an existing
file without saying "are you sure" or it might be more significant.
If necessary, have a look at the docs for the ftp client to find out, then
look for the equivalent extra call (I'm sure that's what it would be) in
Net::FTP.
Here's the first:
#!/usr/bin/perl
use Net::FTP;my $host="aplawrence.com";
my $directory="pub";
$ftp=Net::FTP->new($host,Timeout=>240) or $newerr=1;
push @ERRORS, "Can't ftp to $host: $!\n" if $newerr;
myerr() if $newerr;
print "Connected\n";
$ftp->login("ftp","apl\@") or $newerr=1;
print "Getting file list";
push @ERRORS, "Can't login to $host: $!\n" if $newerr;
$ftp->quit if $newerr;
myerr() if $newerr;
print "Logged in\n";
$ftp->cwd($directory) or $newerr=1;
push @ERRORS, "Can't cd $!\n" if $newerr;
myerr() if $newerr;
$ftp->quit if $newerr;
@files=$ftp->dir or $newerr=1;
push @ERRORS, "Can't get file list $!\n" if $newerr;
myerr() if $newerr;
print "Got file list\n";
foreach(@files) {
print "$_\n";
}
$ftp->quit;
sub myerr {
print "Error: \n";
print @ERRORS;
exit 0;
}
Pretty simple, right? Net::FTP makes it all so easy, so let's do something that would absolutely drive me batty without it.
#!/usr/bin/perl
use Net::FTP;
$date=shift @ARGV;
@months=qw(null Jan Feb Mar Apr My Jun Jul Aug Sep Oct Nov Dec);
@hosts=qw(pcunix.org pcunix.com xyz.com);
@dirs=qw(pub pub pub);
@logins=qw(ftp anonymous fred);
@passwords=qw(tony\@ apl\@ fxdfed);
$x=0;
foreach(@months) {
$months{$_}=$x++;
}
# we need this hash laterif (not $date) {
$now=time();
$now -= (24 * 3600 );
# yesterday
($nowsec,$nowmin,$nowhr,$nowday,$nowmon,$nowyr,$nowdow,$nowdoy,$nowdst)=localtime($now);
$nowyr+=1900;$nowmon++;
$date=sprintf("%.2d/%.2d/%.4d",$nowmon,$nowday,$nowyr);
print "Using $date\n";
}
$now=time();
($nowsec,$nowmin,$nowhr,$nowday,$nowmon,$nowyr,$nowdow,$nowdoy,$nowdst)=localtime($now);
$nowyr+=1900;
# need $nowyr later($month,$day,$year)=split /\//,$date;
#
# broken next century - blame me then
#
$year+=2000 if $year < 100;$x=0;
foreach (@hosts) {
$newerr=0;
$ftp=Net::FTP->new($_,Timeout=>240) or $newerr=1;
push @ERRORS, "Can't ftp to $_: $!\n" if $newerr;
next if $newerr;
print "Connected $_\n";
$ftp->login($logins[$x],$passwords[$x]) or $newerr=1;
push @ERRORS, "Can't login to $_: $!\n" if $newerr;
$ftp->quit if $newerr;
next if $newerr;
print "Logged in $_\n";$ftp->cwd($dirs[$x]) or $newerr=1;
push @ERRORS, "Can't cd $dirs[$x] on $_ $!\n" if $newerr;
$ftp->quit if $newerr;
next if $newerr;
print "Getting file list $_\n";
@files=$ftp->dir or $newerr=1;
push @ERRORS, "Can't get file list on $_ $!\n" if $newerr;
$ftp->quit if $newerr;
next if $newerr;
print "Got list $_\n";print "Looking for $date $time\n";
foreach(@files) {
$_=substr($_,41);
s/ */ /g;
s/^ *//g;
chomp;
@stuff=split / /;
# if it's today, the year slot will have time instead
# so make it this year
$stuff[2]=$nowyr if /:/;$ftp->quit if ($stuff[2] < $year);
next if ($stuff[2] < $year);
$ftp->quit if ($months{$stuff[0]} < $month and $stuff[2] == $year);
next if ($months{$stuff[0]} < $month and $stuff[2] == $year);
$ftp->quit if ($stuff[0] < $day and $stuff[2] == $year and $months{$stuff[0]} == $month);
next if ($stuff[1] < $day and $stuff[2] == $year and $months{$stuff[0]} == $month);print "Getting $_\n";
$ftp->get($stuff[3],$stuff[3]) or $newerr=1;
push @ERRORS, "Couldn't get $stuff[3] $!\n" if $newerr;
}$ftp->quit;
}print @ERRORS;
exit 0;
Perl Net--FTP aplawrence.com/Unixart/perlnetftp.html
Beginners guide to NetFTP presendtaion is very weak, but some comments are insightful
Society
Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers : Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism : The Iron Law of Oligarchy : Libertarian Philosophy
Quotes
War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda : SE quotes : Language Design and Programming Quotes : Random IT-related quotes : Somerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose Bierce : Bernard Shaw : Mark Twain Quotes
Bulletin:
Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 : Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law
History:
Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds : Larry Wall : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOS : Programming Languages History : PL/1 : Simula 67 : C : History of GCC development : Scripting Languages : Perl history : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history
Classic books:
The Peter Principle : Parkinson Law : 1984 : The Mythical Man-Month : How to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite
Most popular humor pages:
Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor
The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D
Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.
This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...
|
You can use PayPal to to buy a cup of coffee for authors of this site |
Disclaimer:
The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.
Last modified: March 12, 2019