![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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.
|
|
||||
|
Quote:
http://search.cpan.org/~gbarr/libnet-1.20/Net/FTP.pm |
|
||||
|
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;
|
|
||||
|
|
|
||||
|
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; |
![]() |
| Bookmarks |
| Tags |
| perl, perl shift, shift, shift perl |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|