remote file copy using Net::Telnet
am 03.02.2006 08:02:08 von AnuHi ,
I am tryin to write a perl script which
" copies file from the local machine to the remote machine "
I found a code snippet for file copy which used Net::SSH and is working
like a breeze.I am tryin to implement the same logic for file copy
using Net::Telnet module,but then its gives an error.
my code:
#!/usr/bin/perl -w
#including the necessary packages
use Net::Telnet;
use strict;
#use warnings;
#use Net::FTP;
use File::Basename;
use Getopt::Std;
#use Getopt::Std;
#calling the necessary subroutine
# To copy file from remote machine onto local machine.
#&ConnectTelnet("XXXXX:idt.tar",".","XXXX","XXXX");
#To copy file from local machine to remote machine.
&ConnectTelnet("aout.txt","XXXX:.","XXXX","");
sub ConnectTelnet{
#assigning the passed arguments to local variables
my($strfnArg1, $strfnArg2, $strfnUid, $strfnPass) = @_;
my $ifnTimeout=10;
my $strfnErrmode='return';
#checking validation conditions
if ($strfnArg1 eq ""||$strfnArg2 eq ""){
print("You have not enterred proper host and file name
arguments\n");
print("Your programme will be terminated\n");
exit;
}
if ($strfnUid eq ""){
print("You did not enter your userid\n");
print("Your programme will be terminated\n");
exit;
}
if ($strfnPass eq ""){
print("You did not enter your password\n");
print("Your programme will be terminated\n");
exit;
}
#based on argument string patterns assigning values to variables
my($strfnFromHost, $strfnFromFile, $strfnToHost, $strfnToFile);
if ($strfnArg1 =~ /:/) {
($strfnFromHost, $strfnFromFile) = split /:/,$strfnArg1, 2;
print("$strfnFromHost\n");
print("$strfnFromFile\n");
}
elsif ($strfnArg2 =~ /:/) {
($strfnToHost, $strfnToFile) = split /:/, $strfnArg2, 2;
}
#opening connection with the remote host by creating object and
providing
#credentials
my $objfnTelnet=new
Net::Telnet(Timeout=>$ifnTimeout,Errmode=>$strfnErrmode);
$objfnTelnet->binmode(1);
$objfnTelnet->open(Host=>$strfnFromHost||$strfnToHost,Timeou t=>$ifnTimeout,Errmode=>$strfnErrmode);
#provide username and password
$objfnTelnet->login(Name => $strfnUid,Password =>
$strfnPass,Timeout=>$ifnTimeout,Errmode=>$strfnErrmode);
# fetching file from remote host to local machine
if ($strfnFromHost && $strfnFromFile) {
$objfnTelnet->cmd("cat $strfnFromFile 1> success 2> failure");
my($abc) = $objfnTelnet->cmd("cat failure");
if($abc eq ""){
my(@strfnOut) = $objfnTelnet->cmd("cat -v $strfnFromFile");
$strfnArg2 = basename $strfnFromFile if $strfnArg2 eq '.';
open (FH, ">$strfnArg2") or die "Can't open $strfnArg2 for
writing: $!";
print FH @strfnOut;
#closing local file
close FH or die "Can't close $strfnArg2 : $!";
print ("File fetched from remote host successfully");
}
else{
die "can't open file";
}
}
#copying file from local machine to remote host
elsif ($strfnToHost) {
open FH, $strfnArg1 or die "Can't open $strfnArg1: $!";
my $c = do { local $/;
print $c;
#closing local file
close FH or die "Can't close $strfnArg1: $!";
$strfnToFile = basename $strfnArg1 if !$strfnToFile ||
$strfnToFile eq '.';
my $ert = $objfnTelnet->cmd("String=>cat -
>>$strfnToFile",$c,Timeout=>$ifnTimeout,Errmode=>$strfnErrmo de);
print("$ert\n");
print ("File copied to remote host succeccfully");
}
}
the error it gives is as follows :
bad named parameter "String=>cat - >>aout.txt" given to
Net::Telnet::cmd() at ./telnetcopy.pl
I am quite to network programming please do clarify this doubt of mine
too.
"My understanding is that the commands that we pass to the SSH or
Telnet session through cmd() get executed at the shell prompt so their
output should be the same irrespective of whether the session is opened
up using Telnet or SSH."
In which case this program should be running fine which isnt happen.
Can anyone please help me.
thanx
Anu.