SlideShare una empresa de Scribd logo
1 de 10
Perl Brown Bag


                    FTP
           File Transfer Protocol


                  Shaun Griffith
                  April 17, 2006

05/13/12            ATI Confidential   1
Agenda

•Net::FTP
     •Boring Stuff You Should Know
     •Classes, Objects, Methods
     •Useful Example
     •Reaching Further


05/13/12                             2
Net::FTP
Boring Details You Should Know
•Net::FTP is a Perl module that handles FTP
commands within a Perl program.
• Net::FTP is part of the “core distribution”, and
always comes with Perl.
• In Perl, the double colon “::” indicates a
subdirectory in the library.
•use dir1::dir2::this looks for
dir1dir2this.pm in the library paths.
•If your main Perl library is c:Perllib, then
Net::FTP should be in
c:Perllibnetftp.pm.
• Modules are always “.pm” files (Perl Module).




05/13/12                                             3
Net::FTP
Synopsis:
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
Classes, Objects, Methods
    use Net::FTP;
•This tells Perl to compile and run the file NetFTP.pm in the
installed 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 manipulates
Net::FTP Objects.
•Objects can only be manipulated by Methods (special
subroutines).
•new is a Method in the Net::FTP class. It takes a
hostname, 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
More Methods…
    $ftp->login(‘user’,‘password’)
      or die "Cannot login ", $ftp->message;
login takes a username and password, and logs into the
remote 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
Simple Example
Simple 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
Reaching Further

What 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
Walking the Dir Tree
To really walk the directory tree, you need either:
    •stack
    •recursion
Recursion 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
Next Time?
Subroutines?
     •Passing parameters
     •Catching parameters
     •Returning data
     •Recursion
Filehandles?
     •Open
     •Close
     •EOF
     •Pipes




05/13/12                                 10

Más contenido relacionado

La actualidad más candente

Perl for System Automation - 01 Advanced File Processing
Perl for System Automation - 01 Advanced File ProcessingPerl for System Automation - 01 Advanced File Processing
Perl for System Automation - 01 Advanced File ProcessingDanairat Thanabodithammachari
 
HaskellとDebianの辛くて甘い関係
HaskellとDebianの辛くて甘い関係HaskellとDebianの辛くて甘い関係
HaskellとDebianの辛くて甘い関係Kiwamu Okabe
 
Command Line Tools
Command Line ToolsCommand Line Tools
Command Line ToolsDavid Harris
 
Basic unix commands
Basic unix commandsBasic unix commands
Basic unix commandsswtjerin4u
 
Linux basic commands with examples
Linux basic commands with examplesLinux basic commands with examples
Linux basic commands with examplesabclearnn
 
Hadoop 20111117
Hadoop 20111117Hadoop 20111117
Hadoop 20111117exsuns
 
Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)
Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)
Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)Ahmed El-Arabawy
 
Basic linux commands for bioinformatics
Basic linux commands for bioinformaticsBasic linux commands for bioinformatics
Basic linux commands for bioinformaticsBonnie Ng
 
Anandha ganesh linux1.ppt
Anandha ganesh linux1.pptAnandha ganesh linux1.ppt
Anandha ganesh linux1.pptanandha ganesh
 
Basic command ppt
Basic command pptBasic command ppt
Basic command pptRohit Kumar
 
Dive into Fluentd plugin v0.12
Dive into Fluentd plugin v0.12Dive into Fluentd plugin v0.12
Dive into Fluentd plugin v0.12N Masahiro
 
OpenLDAP Replication Strategies
OpenLDAP Replication StrategiesOpenLDAP Replication Strategies
OpenLDAP Replication StrategiesGavin Henry
 
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commandsSagar Kumar
 
Course 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment VariablesCourse 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment VariablesAhmed El-Arabawy
 

La actualidad más candente (20)

Perl for System Automation - 01 Advanced File Processing
Perl for System Automation - 01 Advanced File ProcessingPerl for System Automation - 01 Advanced File Processing
Perl for System Automation - 01 Advanced File Processing
 
HaskellとDebianの辛くて甘い関係
HaskellとDebianの辛くて甘い関係HaskellとDebianの辛くて甘い関係
HaskellとDebianの辛くて甘い関係
 
Command Line Tools
Command Line ToolsCommand Line Tools
Command Line Tools
 
Basic unix commands
Basic unix commandsBasic unix commands
Basic unix commands
 
Linux basic commands with examples
Linux basic commands with examplesLinux basic commands with examples
Linux basic commands with examples
 
Rhel6
Rhel6Rhel6
Rhel6
 
Hadoop 20111117
Hadoop 20111117Hadoop 20111117
Hadoop 20111117
 
Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)
Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)
Embedded Systems: Lecture 11: Introduction to Git & GitHub (Part 2)
 
Basic linux commands for bioinformatics
Basic linux commands for bioinformaticsBasic linux commands for bioinformatics
Basic linux commands for bioinformatics
 
Anandha ganesh linux1.ppt
Anandha ganesh linux1.pptAnandha ganesh linux1.ppt
Anandha ganesh linux1.ppt
 
Basic linux commands
Basic linux commandsBasic linux commands
Basic linux commands
 
Basic command ppt
Basic command pptBasic command ppt
Basic command ppt
 
Cp command in Linux
Cp command in LinuxCp command in Linux
Cp command in Linux
 
Dive into Fluentd plugin v0.12
Dive into Fluentd plugin v0.12Dive into Fluentd plugin v0.12
Dive into Fluentd plugin v0.12
 
Basic linux commands
Basic linux commandsBasic linux commands
Basic linux commands
 
OpenLDAP Replication Strategies
OpenLDAP Replication StrategiesOpenLDAP Replication Strategies
OpenLDAP Replication Strategies
 
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commands
 
tools
toolstools
tools
 
Ppt
PptPpt
Ppt
 
Course 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment VariablesCourse 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment Variables
 

Destacado

Perl Intro 9 Command Line Arguments
Perl Intro 9 Command Line ArgumentsPerl Intro 9 Command Line Arguments
Perl Intro 9 Command Line ArgumentsShaun Griffith
 
Perl Intro 8 File Handles
Perl Intro 8 File HandlesPerl Intro 8 File Handles
Perl Intro 8 File HandlesShaun Griffith
 
Perl Intro 2 First Program
Perl Intro 2 First ProgramPerl Intro 2 First Program
Perl Intro 2 First ProgramShaun Griffith
 
Perl Intro 7 Subroutines
Perl Intro 7 SubroutinesPerl Intro 7 Subroutines
Perl Intro 7 SubroutinesShaun Griffith
 
Perl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And SubstitutionsPerl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And SubstitutionsShaun Griffith
 
Perl Intro 3 Datalog Parsing
Perl Intro 3 Datalog ParsingPerl Intro 3 Datalog Parsing
Perl Intro 3 Datalog ParsingShaun Griffith
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsLinkedIn
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerLuminary Labs
 

Destacado (9)

Perl Intro 9 Command Line Arguments
Perl Intro 9 Command Line ArgumentsPerl Intro 9 Command Line Arguments
Perl Intro 9 Command Line Arguments
 
Perl Intro 8 File Handles
Perl Intro 8 File HandlesPerl Intro 8 File Handles
Perl Intro 8 File Handles
 
Perl Intro 4 Debugger
Perl Intro 4 DebuggerPerl Intro 4 Debugger
Perl Intro 4 Debugger
 
Perl Intro 2 First Program
Perl Intro 2 First ProgramPerl Intro 2 First Program
Perl Intro 2 First Program
 
Perl Intro 7 Subroutines
Perl Intro 7 SubroutinesPerl Intro 7 Subroutines
Perl Intro 7 Subroutines
 
Perl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And SubstitutionsPerl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And Substitutions
 
Perl Intro 3 Datalog Parsing
Perl Intro 3 Datalog ParsingPerl Intro 3 Datalog Parsing
Perl Intro 3 Datalog Parsing
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
 

Similar a Perl Intro 6 Ftp

PHP Streams
PHP StreamsPHP Streams
PHP StreamsG Woo
 
Practical PHP 5.3
Practical PHP 5.3Practical PHP 5.3
Practical PHP 5.3Nate Abele
 
Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011Jacopo Romei
 
The Essential Perl Hacker's Toolkit
The Essential Perl Hacker's ToolkitThe Essential Perl Hacker's Toolkit
The Essential Perl Hacker's ToolkitStephen Scaffidi
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappersPositive Hack Days
 
Oozie or Easy: Managing Hadoop Workloads the EASY Way
Oozie or Easy: Managing Hadoop Workloads the EASY WayOozie or Easy: Managing Hadoop Workloads the EASY Way
Oozie or Easy: Managing Hadoop Workloads the EASY WayDataWorks Summit
 
BITS: Introduction to Linux - Text manipulation tools for bioinformatics
BITS: Introduction to Linux - Text manipulation tools for bioinformaticsBITS: Introduction to Linux - Text manipulation tools for bioinformatics
BITS: Introduction to Linux - Text manipulation tools for bioinformaticsBITS
 
Configuration surgery with Augeas (OggCamp 12)
Configuration surgery with Augeas (OggCamp 12)Configuration surgery with Augeas (OggCamp 12)
Configuration surgery with Augeas (OggCamp 12)Dominic Cleal
 
101 3.3 perform basic file management
101 3.3 perform basic file management101 3.3 perform basic file management
101 3.3 perform basic file managementAcácio Oliveira
 
Drupal 8 configuration management
Drupal 8 configuration managementDrupal 8 configuration management
Drupal 8 configuration managementAlexander Tkachev
 
Streams, sockets and filters oh my!
Streams, sockets and filters oh my!Streams, sockets and filters oh my!
Streams, sockets and filters oh my!Elizabeth Smith
 
Debugging Network Issues
Debugging Network IssuesDebugging Network Issues
Debugging Network IssuesApcera
 
UPHPU Meeting, February 17, 2012
UPHPU Meeting, February 17, 2012UPHPU Meeting, February 17, 2012
UPHPU Meeting, February 17, 2012andersonjohnd
 
OpenLDAP configuration brought to Apache Directory Studio
OpenLDAP configuration brought to Apache Directory StudioOpenLDAP configuration brought to Apache Directory Studio
OpenLDAP configuration brought to Apache Directory StudioLDAPCon
 
Drupal 8 Services And Dependency Injection
Drupal 8 Services And Dependency InjectionDrupal 8 Services And Dependency Injection
Drupal 8 Services And Dependency InjectionPhilip Norton
 
Writing and using php streams and sockets tek11
Writing and using php streams and sockets   tek11Writing and using php streams and sockets   tek11
Writing and using php streams and sockets tek11Elizabeth Smith
 

Similar a Perl Intro 6 Ftp (20)

PHP Streams
PHP StreamsPHP Streams
PHP Streams
 
Practical PHP 5.3
Practical PHP 5.3Practical PHP 5.3
Practical PHP 5.3
 
Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011
 
The Essential Perl Hacker's Toolkit
The Essential Perl Hacker's ToolkitThe Essential Perl Hacker's Toolkit
The Essential Perl Hacker's Toolkit
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappers
 
Oozie or Easy: Managing Hadoop Workloads the EASY Way
Oozie or Easy: Managing Hadoop Workloads the EASY WayOozie or Easy: Managing Hadoop Workloads the EASY Way
Oozie or Easy: Managing Hadoop Workloads the EASY Way
 
BITS: Introduction to Linux - Text manipulation tools for bioinformatics
BITS: Introduction to Linux - Text manipulation tools for bioinformaticsBITS: Introduction to Linux - Text manipulation tools for bioinformatics
BITS: Introduction to Linux - Text manipulation tools for bioinformatics
 
Puppet
PuppetPuppet
Puppet
 
Configuration surgery with Augeas (OggCamp 12)
Configuration surgery with Augeas (OggCamp 12)Configuration surgery with Augeas (OggCamp 12)
Configuration surgery with Augeas (OggCamp 12)
 
Docker
DockerDocker
Docker
 
101 3.3 perform basic file management
101 3.3 perform basic file management101 3.3 perform basic file management
101 3.3 perform basic file management
 
Drupal 8 configuration management
Drupal 8 configuration managementDrupal 8 configuration management
Drupal 8 configuration management
 
Perl basics for Pentesters
Perl basics for PentestersPerl basics for Pentesters
Perl basics for Pentesters
 
Streams, sockets and filters oh my!
Streams, sockets and filters oh my!Streams, sockets and filters oh my!
Streams, sockets and filters oh my!
 
Debugging Network Issues
Debugging Network IssuesDebugging Network Issues
Debugging Network Issues
 
UPHPU Meeting, February 17, 2012
UPHPU Meeting, February 17, 2012UPHPU Meeting, February 17, 2012
UPHPU Meeting, February 17, 2012
 
OpenLDAP configuration brought to Apache Directory Studio
OpenLDAP configuration brought to Apache Directory StudioOpenLDAP configuration brought to Apache Directory Studio
OpenLDAP configuration brought to Apache Directory Studio
 
Drupal 8 Services And Dependency Injection
Drupal 8 Services And Dependency InjectionDrupal 8 Services And Dependency Injection
Drupal 8 Services And Dependency Injection
 
Writing and using php streams and sockets tek11
Writing and using php streams and sockets   tek11Writing and using php streams and sockets   tek11
Writing and using php streams and sockets tek11
 
Ch23 system administration
Ch23 system administration Ch23 system administration
Ch23 system administration
 

Perl Intro 6 Ftp

  • 1. Perl Brown Bag FTP File Transfer Protocol Shaun Griffith April 17, 2006 05/13/12 ATI Confidential 1
  • 2. Agenda •Net::FTP •Boring Stuff You Should Know •Classes, Objects, Methods •Useful Example •Reaching Further 05/13/12 2
  • 3. Net::FTP Boring Details You Should Know •Net::FTP is a Perl module that handles FTP commands within a Perl program. • Net::FTP is part of the “core distribution”, and always comes with Perl. • In Perl, the double colon “::” indicates a subdirectory in the library. •use dir1::dir2::this looks for dir1dir2this.pm in the library paths. •If your main Perl library is c:Perllib, then Net::FTP should be in c:Perllibnetftp.pm. • Modules are always “.pm” files (Perl Module). 05/13/12 3
  • 4. Net::FTP Synopsis: 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. Classes, Objects, Methods use Net::FTP; •This tells Perl to compile and run the file NetFTP.pm in the installed 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 manipulates Net::FTP Objects. •Objects can only be manipulated by Methods (special subroutines). •new is a Method in the Net::FTP class. It takes a hostname, 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. More Methods… $ftp->login(‘user’,‘password’) or die "Cannot login ", $ftp->message; login takes a username and password, and logs into the remote 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. Simple Example Simple 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. Reaching Further What 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. Walking the Dir Tree To really walk the directory tree, you need either: •stack •recursion Recursion 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. Next Time? Subroutines? •Passing parameters •Catching parameters •Returning data •Recursion Filehandles? •Open •Close •EOF •Pipes 05/13/12 10