Ftp code in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ftp code in Perl
# 1  
Old 02-15-2007
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  
Old 02-16-2007
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. Smilie
# 3  
Old 02-16-2007
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  
Old 02-22-2007
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;

This User Gave Thanks to Raynon For This Post:
# 5  
Old 03-05-2007
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  
Old 03-05-2007
Try the chdir() function, if you really want that.

http://perldoc.perl.org/functions/chdir.html
# 7  
Old 03-05-2007
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;
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to ftp the files in perl.?

i want to upload two different files in to two different directories(path) on ftp server.but i am able to ftping only one at a time :(.. not both simultaneously. here i have connectd the ftp and use the binary mode to transfer the files $ftp = Net::FTP->new($ftphost, Debug => 0) or... (2 Replies)
Discussion started by: aarts
2 Replies

2. Shell Programming and Scripting

FTP from one directory to another using perl

Hi All I am stuck with a problem and i want your help, I have two directories dir1 and dir2 The files present in dir1 is a1,a2 a3 a4 What i want to is to FTP the files present in the dir1 to dir2 (with .txt extension at the end.) with the help of the Perl. The output expected is The... (12 Replies)
Discussion started by: parthmittal2007
12 Replies

3. HP-UX

[Solved] Unable to rename file in ftp server .Net:FTP perl

Hello All, I am trying to connect to ftp server and get the files. Also i need to rename the file in other ftp dir. rename method is not allowing me to rename the file in other dir. When i tried copy command by using net::FTP:FILE then perl says it is not installed. Can some body help me to... (2 Replies)
Discussion started by: krsnadasa
2 Replies

4. Shell Programming and Scripting

ftp in perl

Hi, I have three files in this /home/mani/ location. I would like to ftp to another server. could you please give perl for that requirement. Thanks, Mani (10 Replies)
Discussion started by: Mani_apr08
10 Replies

5. Shell Programming and Scripting

Perl FTP navigation

Hi Experts, I have this requirement to list dirs and files of an FTP server on regular basis. I was able to do it by following script: $ftpobj = Net::FTP -> new ("$ftpsrv") || die "Cannot connect to FTP $ftpsrv"; $ftpobj -> login("user","passwd"); $ftpobj -> cwd ("/root_dir"); @rootdir... (1 Reply)
Discussion started by: mtomar
1 Replies

6. Shell Programming and Scripting

script FTP in perl

this script should search directories read,,search for file for daily reports. you must provide the beginning day, ending day and month. this script will pull files for days and month specified and Ftp them to another server. Thanks for any help you provide (2 Replies)
Discussion started by: lemseffert
2 Replies

7. Shell Programming and Scripting

Net::Ftp in perl

I am trying to execute a script in another server, i used Net::Ftp module How to execute unix command in another server by using Net::Ftp module.. #!/usr/bin.perl ### Perl script to $ftp->login($user_name,'password') or die "Cannot login ", $ftp->message;... (2 Replies)
Discussion started by: pritish.sas
2 Replies

8. Shell Programming and Scripting

How to automate ftp in perl

My situations is I cannot use NET::ftp. So I need to have a way to automate ftp. I know how to do it in ksh: #!/usr/bin/ksh ftp -i -v -n $host_name <<_FTP >> $log_file 2>&1 user $user_name lcd $local_dir cd $remote_dir put $file_name bye _FTP But how can I do it in perl? Note:... (6 Replies)
Discussion started by: egyfan
6 Replies

9. Shell Programming and Scripting

using perl to ftp

Hi all, I am trying to download a build from an ftp server. My problem is that my build contains sub folders and files within the sub folders. I can ftp a single file at a time, but it will be difficult to specify all the paths and download individula files. My build structure is: ... (2 Replies)
Discussion started by: gurukottur
2 Replies

10. UNIX for Dummies Questions & Answers

Perl, Pipes, and FTP

I am attempting to automate an ftp session in PERL by emulating the user and sending commands to ftp, but I am getting unexpected and unwanted results. Here is a portion code that illustrates the method I am attempting (this was just a shot in the dark): system("( echo open server sleep 1... (2 Replies)
Discussion started by: murdaugh
2 Replies
Login or Register to Ask a Question