Perl Brown Bag                    FTP           File Transfer Protocol                  Shaun Griffith                  Ap...
Agenda•Net::FTP     •Boring Stuff You Should Know     •Classes, Objects, Methods     •Useful Example     •Reaching Further...
Net::FTPBoring Details You Should Know•Net::FTP is a Perl module that handles FTPcommands within a Perl program.• Net::FTP...
Net::FTPSynopsis:use Net::FTP;my $host = ‘some.host.name’;my $ftp = Net::FTP->new($host)  or die "Cannot connect to $host:...
Classes, Objects, Methods    use Net::FTP;•This tells Perl to compile and run the file NetFTP.pm in theinstalled library. ...
More Methods…    $ftp->login(‘user’,‘password’)      or die "Cannot login ", $ftp->message;login takes a username and pass...
Simple ExampleSimple Example:    use Net::FTP;    my $host = ‘210.68.241.2’;    my $user = ‘ati’;    my $pw     = (shift o...
Reaching FurtherWhat about automation?What about scanning directories?    my @dirs = $ftp->dir();    for my $dir ( @dirs )...
Walking the Dir TreeTo really walk the directory tree, you need either:    •stack    •recursionRecursion is easier to prog...
Next Time?Subroutines?     •Passing parameters     •Catching parameters     •Returning data     •RecursionFilehandles?    ...
Upcoming SlideShare
Loading in...5
×

Perl Intro 6 Ftp

280

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
280
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Perl Intro 6 Ftp

  1. 1. Perl Brown Bag FTP File Transfer Protocol Shaun Griffith April 17, 200605/13/12 ATI Confidential 1
  2. 2. Agenda•Net::FTP •Boring Stuff You Should Know •Classes, Objects, Methods •Useful Example •Reaching Further05/13/12 2
  3. 3. Net::FTPBoring Details You Should Know•Net::FTP is a Perl module that handles FTPcommands within a Perl program.• Net::FTP is part of the “core distribution”, andalways comes with Perl.• In Perl, the double colon “::” indicates asubdirectory in the library.•use dir1::dir2::this looks fordir1dir2this.pm in the library paths.•If your main Perl library is c:Perllib, thenNet::FTP should be inc:Perllibnetftp.pm.• Modules are always “.pm” files (Perl Module).05/13/12 3
  4. 4. Net::FTPSynopsis:use Net::FTP;my $host = ‘some.host.name’;my $ftp = Net::FTP->new($host) or die "Cannot connect to $host: $@";$ftp->login(‘user’,‘password’) or die "Cannot login ", $ftp->message;$ftp->cwd("/pub") or die "Cannot change working directory ", $ftp->message;$ftp->get("that.file") or die "get failed ", $ftp->message;$ftp->quit;05/13/12 4
  5. 5. Classes, Objects, Methods use Net::FTP;•This tells Perl to compile and run the file NetFTP.pm in theinstalled library. my $host = ‘some.host.name’; my $ftp = Net::FTP->new($host) or die "Cannot connect to $host: $@";•Net::FTP is a Class module. It creates and manipulatesNet::FTP Objects.•Objects can only be manipulated by Methods (specialsubroutines).•new is a Method in the Net::FTP class. It takes ahostname, and returns a Net::FTP object reference.•$ftp holds the Net::FTP object reference.•If new fails, the error message will be in $@.05/13/12 5
  6. 6. More Methods… $ftp->login(‘user’,‘password’) or die "Cannot login ", $ftp->message;login takes a username and password, and logs into theremote host. $ftp->cwd(‘/pub’) or die "Cannot change working directory ", $ftp->message;cwd changes the directory. $ftp->get("that.file") or die "get failed ", $ftp->message;get transfers “that.file” to the local machine. $ftp->quit;quit ends the FTP session with the remote host.05/13/12 6
  7. 7. Simple ExampleSimple Example: use Net::FTP; my $host = ‘210.68.241.2’; my $user = ‘ati’; my $pw = (shift or ‘’);Don’t put password in scripts! my $ftp = Net::FTP->new($host) or die; $ftp->login($user,$pw) or die; $ftp->cwd(‘RV530/0414’) or die; $ftp->binary() or die;; # for gzip, etc. $ftp->get(‘/RV530A26_B_FT_X1.tar.gz’) or die; $ftp->quit() or die;05/13/12 7
  8. 8. Reaching FurtherWhat about automation?What about scanning directories? my @dirs = $ftp->dir(); for my $dir ( @dirs ) { if ( $dir =~ /$match_directory/ ) { use_as_directory } else { use_as_file } }05/13/12 8
  9. 9. Walking the Dir TreeTo really walk the directory tree, you need either: •stack •recursionRecursion is easier to program: sub ftp_tree_walk { my @dirs = $ftp->dir(); ($is_dir,$can_read,$name) = $dir =~ m/some_long_regex/; if ( $is_dir =~ /$dir_match/ ) { $ftp->cwd($dir); ftp_tree_walk(); $ftp->cdup(); } else { $ftp->get($dir) } }05/13/12 9
  10. 10. Next Time?Subroutines? •Passing parameters •Catching parameters •Returning data •RecursionFilehandles? •Open •Close •EOF •Pipes05/13/12 10
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×