{"id":34491,"date":"2017-11-26T03:01:54","date_gmt":"2017-11-25T17:01:54","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=34491"},"modified":"2017-11-26T05:36:50","modified_gmt":"2017-11-25T19:36:50","slug":"perl-ftp-and-the-three-ps-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/perl-ftp-and-the-three-ps-tutorial\/","title":{"rendered":"Perl ftp and the Three Ps Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Perl ftp and the Three Ps Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url_3ps.jpg\" title=\"Perl ftp and the Three Ps Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Perl ftp and the Three Ps Tutorial<\/p><\/div>\n<p>Yesterday&#8217;s <a title='Python ftp Primer Tutorial' href='#ppt'>Python ftp Primer Tutorial<\/a> is given a &#8220;return of the Three Ps&#8221; feel today.  Yes, we code some Perl to do similar ftp listing logic as we did with Python yesterday, so that &#8230;<\/p>\n<ul>\n<li><b>P<\/b>HP <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php\" title=\"Click picture\">supervises ftp listing functionality<\/a> provided by either &#8230;<\/li>\n<li><b>P<\/b>ython &#8230; or &#8230;<\/li>\n<li><b>P<\/b>erl &#8230; and this new Perl code is &#8230;<\/li>\n<\/ul>\n<p><code><br \/>\n#!\/usr\/bin\/perl<br \/>\n# ftp_url.pl<br \/>\n# Ftp URL listing<br \/>\n# RJM Programming<br \/>\n# November, 2017<br \/>\n# Thanks to https:\/\/www.foo.be\/docs\/tpj\/issues\/vol1_3\/tpj0103-0007.html<br \/>\n<b><\/b><br \/>\nuse Net::FTP;<br \/>\n#use File::Listing qw(parse_dir);<br \/>\n<b><\/b><br \/>\n# Create a new connection to the ftp server<br \/>\nsub connection {    # Create a new NET::FTP object<br \/>\n        $ftp = Net::FTP->new($CPANhost, Timeout => 60)<br \/>\n               or die \"Can't contact $CPANhost: $!\";<br \/>\n        # We shall login to the ftp server as anonymous;<br \/>\n        $ftp->login($CPANusername,$CPANpassword)<br \/>\n               or die \"Can't login ($CPANhost):\" .<br \/>\n               $ftp->message;<br \/>\n        # Change the working directory<br \/>\n        $ftp->cwd($CPANpath) or die<br \/>\n               \"Can't change directory ($CPANhost):\".<br \/>\n               $ftp->message;<br \/>\n        return $ftp;<br \/>\n    }<br \/>\n<b><\/b><br \/>\n<b><\/b><br \/>\n# The path to the CPAN\/modules directory on most CPAN hosts<br \/>\n$CPANpath = '\/';<br \/>\n<b><\/b><br \/>\n$CPANusername = 'mkuulma@ozemail.com.au';<br \/>\n$CPANpassword = '';<br \/>\n<b><\/b><br \/>\n# Change this to the name of your nearest CPAN host<br \/>\n$CPANhost = '';<br \/>\nmy $size = @ARGV;<br \/>\nif ($size == 2) {<br \/>\n  $CPANhost = 'ftp.ozemail.com.au';<br \/>\n  $CPANpath = $ARGV[1];<br \/>\n  $CPANpassword = $ARGV[0];<br \/>\n} else {<br \/>\n if ($size == 1) {<br \/>\n  $CPANhost = 'ftp.ozemail.com.au';<br \/>\n  $CPANpassword = $ARGV[0];<br \/>\n } else {<br \/>\n  if ($size == 3) {<br \/>\n   $CPANhost = $ARGV[0];<br \/>\n   $CPANusername = $ARGV[1];<br \/>\n   $CPANpassword = $ARGV[2];<br \/>\n  } else {<br \/>\n   if ($size == 4) {<br \/>\n    $CPANhost = $ARGV[0];<br \/>\n    $CPANusername = $ARGV[1];<br \/>\n    $CPANpassword = $ARGV[2];<br \/>\n    $CPANpath = $ARGV[3];<br \/>\n   } else {<br \/>\n    print \"Usage is via ... perl ftp_url.pl [ftphost=ftp.ozemail.com.au] [ftpusername=mkuulma@ozemail.com.au] ftppassword [subpath=\/]\\n\";<br \/>\n   }<br \/>\n  }<br \/>\n }<br \/>\n}<br \/>\n<b><\/b><br \/>\nif ($CPANpassword eq '') {<br \/>\n $CPANpassword = '';<br \/>\n} else {<br \/>\n # Create the initial connection<br \/>\n $ftp = connection();<br \/>\n<b><\/b><br \/>\n $ftp->pasv(); # passive mode<br \/>\n<b><\/b><br \/>\n # Retrieve a recursive directory listing<br \/>\n my @ls = $ftp->ls('-l');<br \/>\n<b><\/b><br \/>\n for my $filex (@ls) {<br \/>\n  my $file = $filex;<br \/>\n  my (@description, $size);<br \/>\n  if (-e $file) {<br \/>\n   push @description, 'binary' if (-B _);<br \/>\n   push @description, 'a socket' if (-S _);<br \/>\n   push @description, 'a text file' if (-T _);<br \/>\n   push @description, 'a block special file' if (-b _);<br \/>\n   push @description, 'a character special file' if (-c _);<br \/>\n   push @description, 'a directory' if (-d _);<br \/>\n   push @description, 'executable' if (-x _);<br \/>\n   push @description, (($size = -s _)) ? \"$size bytes\" : 'empty';<br \/>\n   push @description, (($daysago = -M _)) ? \" modified $daysago days ago\" : '';<br \/>\n   print \"\\n\\t$file is \", join(', ',@description),\"\";<br \/>\n  }<br \/>\n }<br \/>\n print \"\\n\\t\";<br \/>\n}<br \/>\n<\/code><\/p>\n<p> &#8230; or you can download it as <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.pl_GETME\">ftp_url.pl<\/a> (supervised by <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php-GETME\">ftp_url.php<\/a> changed in <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php-GETME\">this way<\/a>, where you may notice multiple HTML input type=submit buttons used to differentiate the two strands of ftp functionality modes).<\/p>\n<p>With this Perl coding we again use command line arguments as the technique to handle user defined data to define the ftp connection.<\/p>\n<p>Think server side languages, think &#8220;The Three Ps&#8221;!<\/p>\n<hr>\n<p id='ppt'>Previous relevant <a target=_blank title='Python ftp Primer Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/python-ftp-primer-tutorial\/'>Python ftp Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Python ftp Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.jpg\" title=\"Python ftp Primer Tutorial\"   \/><\/a><p class=\"wp-caption-text\">Python ftp Primer Tutorial<\/p><\/div>\n<p>Python is a great server side language to learn.  Some web server configurations use it as their &#8220;go to&#8221; server language, though ours here at rjmprogramming.com.au uses PHP (and so we&#8217;ve written a PHP &#8220;supervisor&#8221; to dovetail with today&#8217;s Python code).<\/p>\n<p>We&#8217;re going to write this Python code, that facilitates the listing of an ftp URL &#8220;path&#8221;, to run as if from a Linux (or Mac OS X, which we tested with for the work today) command line.<\/p>\n<p>So, underlying the PHP supervisory <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php\" title=\"Click picture\">live run<\/a> today, the hard working &#8220;duck child&#8221; Python goes like &#8230;<\/p>\n<p><code><br \/>\n\"\"\"<br \/>\nftp_url.py - RJM Programming - November 2017<br \/>\nShow some ftp functionality ... thanks to Python & XML by O'Reilly ISBN: 0-596-00128-2<br \/>\n\"\"\"<br \/>\nimport sys<br \/>\n<b><\/b><br \/>\nfrom urllib import urlopen<br \/>\n<b><\/b><br \/>\ndef main(argv):<br \/>\n<b><\/b><br \/>\n if len(sys.argv) == 5:<br \/>\n  fd=urlopen(\"ftp:\/\/\" + sys.argv[2] + \":\" + sys.argv[3] + \"@\" + sys.argv[1] + sys.argv[4])<br \/>\n else:<br \/>\n  if len(sys.argv) == 4:<br \/>\n   fd=urlopen(\"ftp:\/\/\" + sys.argv[2] + \":\" + sys.argv[3] + \"@\" + sys.argv[1])<br \/>\n  else:<br \/>\n   if len(sys.argv) == 3:<br \/>\n    fd=urlopen(\"ftp:\/\/mkuulma@ozemail.com.au:\" + sys.argv[1] + \"@ftp.ozemail.com.au\" + sys.argv[2])<br \/>\n   else:<br \/>\n    if len(sys.argv) == 2:<br \/>\n     fd=urlopen(\"ftp:\/\/mkuulma@ozemail.com.au:\" + sys.argv[1] + \"@ftp.ozemail.com.au\")<br \/>\n    else:<br \/>\n     print \"Usage is via ... python ftp_url.py [ftphost=ftp.ozemail.com.au] [ftpusername=mkuulma@ozemail.com.au] ftppassword [subpath=\/]\"<br \/>\n     exit()<br \/>\n<b><\/b><br \/>\n print fd.read()<br \/>\n exit()<br \/>\n<b><\/b><br \/>\nif __name__ == \"__main__\":<br \/>\n   main(sys.argv[1:])<br \/>\n<b><\/b><br \/>\n<\/code><\/p>\n<p> &#8230; or you can download it as <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.py_GETME\">ftp_url.py<\/a> (supervised by <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/ftp_url.php_GETME\">ftp_url.php<\/a>).<\/p>\n<p>Interesting, huh?!<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d34479' onclick='var dv=document.getElementById(\"d34479\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/python\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d34479' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d34491' onclick='var dv=document.getElementById(\"d34491\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/perl\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d34491' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday&#8217;s Python ftp Primer Tutorial is given a &#8220;return of the Three Ps&#8221; feel today. Yes, we code some Perl to do similar ftp listing logic as we did with Python yesterday, so that &#8230; PHP supervises ftp listing functionality &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/perl-ftp-and-the-three-ps-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,37],"tags":[174,234,252,405,469,707,710,916,932,997,1012,2334,1319,1345],"class_list":["post-34491","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","tag-button","tag-command-line","tag-connection","tag-exec","tag-ftp","tag-linux","tag-list","tag-perl","tag-php","tag-programming","tag-python","tag-submit","tag-tutorial","tag-url"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/34491"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=34491"}],"version-history":[{"count":7,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/34491\/revisions"}],"predecessor-version":[{"id":34504,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/34491\/revisions\/34504"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=34491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=34491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=34491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}