The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
PERL Code talashil Shell Programming and Scripting 2 06-06-2008 02:13 AM
need help to write perl code getdpg Shell Programming and Scripting 0 09-20-2006 10:24 AM
perl code help circleW UNIX for Dummies Questions & Answers 1 11-09-2004 05:28 PM
Perl running C code gdboling Shell Programming and Scripting 1 09-02-2003 07:43 PM
Perl Code Hiding sskb Shell Programming and Scripting 5 01-02-2002 02:21 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-15-2007
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Ftp code in Perl

Hi All, i want to have a ftp function in my perl but i am unfamailiar with the perl syntax. Can any body help ?

The ftp code below is in csh, can anybody help to convert this to perl with the same functionality ?


Code:
foreach t (10.10.10.10 20.20.20.20)

set USER = "xxx"
set PASS = "zzz"
ftp -n $t <<XX > /dev/null
user $USER $PASS
cd /bbb/ccc
lcd /kkk
prompt
ascii
mget *txt
XX

end

  #2 (permalink)  
Old 02-16-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,958
You could make use of the following script which is very basic in nature without any customizations and necessarily critical factors for ftp being configured in a configuration file.



Code:
#! /opt/third-party/bin/perl

use Net::FTP;

my ($server, $user, $pass, $trn_mode, $cwd, $file, $debuglvl);
my ($confFile, $content, @split_line);

$confFile = "ftp.conf";

sub infoCheck {
  my $name = shift;
  my $value = shift;

  if( $name eq "debug" ) {
    if( $value != 0 && $value != 1 ) {
      #Restrict them to 0 or 1
      $value = 0;
    }
    return $value;
  }

  if( length($value) <= 0 ) {
    print "No Info for $name. Terminating\n";
    close(FILE);
    exit 1;
  }
}

open(FILE, "< $confFile") || die "Unable to read conf file: $confFile <$!>\n";
while( chomp($content = <FILE> ) ) {
  @split_line = split(/=/, $content);
  if( $split_line[0] eq "server" ) {
    infoCheck ("server", $split_line[1]);
    $server = $split_line[1];
  }
  elsif( $split_line[0] eq "user" ) {
    infoCheck ("user", $split_line[1]);
    $user = $split_line[1];
  }
  elsif( $split_line[0] eq "pass" ) {
    infoCheck ("pass", $split_line[1]);
    $pass = $split_line[1];
  }
  elsif( $split_line[0] eq "mode" ) {
    $mode = $split_line[1];
  }
  elsif( $split_line[0] eq "cwd" ) {
    $cwd = $split_line[1];
  }
  elsif( $split_line[0] eq "file" ) {
    infoCheck ("file", $split_line[1]);
    $file = $split_line[1];
  }
  elsif( $split_line[0] eq "debug" ) {
    $debuglvl = infoCheck ("debug", $split_line[1]);
  }
  else {
    print "Whatz this unknown conf value!!! ???\n";
    close(FILE);
    exit 1;
  }
}
close(FILE);

$ftp = Net::FTP->new($server, Debug => $debuglvl ) || die "Unable to connect to $server $@ \n";

$ftp->login($user, $pass) || die "Unable to login. ", $ftp->message;

if( length($cwd) > 0 ) {
  $ftp->cwd($cwd) || die "Unable to cwd. ", $ftp->message;
}

if( $mode eq "ascii" ) {
  $ftp->ascii || die "Unable to set mode to ascii. ", $ftp->message;
}

elsif( $mode eq "binary" ) {
  $ftp->binary || die "Unable to set mode to binary. ", $ftp->message;
}

$ftp->get($file) || die "Unable to get file. ", $ftp->message;

$ftp->quit;

exit 0


Code:
>cat ftp.conf
server=server1
user=user1
pass=password1
mode=ascii
cwd=somedir
file=somefile
debug=0


Please do let us know if there are any problems with that.
  #3 (permalink)  
Old 02-16-2007
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
Quote:
Originally Posted by Raynon
Hi All, i want to have a ftp function in my perl but i am unfamailiar with the perl syntax. Can any body help ?
You can use Net::FTP for that. Look at the example on the manpage, it basically demonstrates all of the things you will need. e.g. login, cwd, get etc.

http://search.cpan.org/~gbarr/libnet-1.20/Net/FTP.pm
  #4 (permalink)  
Old 02-22-2007
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Hi cbkihong,

Is there a perl code with same function as the command lcd in csh ?
And how can i put a wild card in the get command ? $ftp->get("xxx*.txt"). It didn;t seem to work below for me with the asterix


Code:
#!/usr/bin/perl
use Net::FTP;

    $ftp = Net::FTP->new("10.10.10.10", Debug => 0)
      or die "Cannot connect to some.host.name: $@";

    $ftp->login("aaa",'bbb')
      or die "Cannot login ", $ftp->message;

    $ftp->cwd("/myreports")
      or die "Cannot change working directory ", $ftp->message;

    $ftp->get("xxx*.txt")
      or die "get failed ", $ftp->message;

    $ftp->quit;

  #5 (permalink)  
Old 03-05-2007
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Can anybody help me with the above ? I would want to lcd to my desired directory and get all files with xxx*.txt where asterix is the wildcard
  #6 (permalink)  
Old 03-05-2007
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
Try the chdir() function, if you really want that.

http://perldoc.perl.org/functions/chdir.html
  #7 (permalink)  
Old 03-05-2007
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Thanks for your suggestion. What about wildcards ? Can i use wild card to get the files that i want as shown below? . Eg xxxabc.txt , xxxbbb.txt , xxxrrr.txt will all be ftped.

$ftp->get("xxx*.txt")
or die "get failed ", $ftp->message;
Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0