Click here to get back home

remote file copy using Net::Telnet

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
remote file copy using Net::Telnet anu 02-03-2006
Posted by anu on February 3, 2006, 2:02 am
Please log in for more thread options


Hi ,

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,Timeout=>$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 $/; <FH> };
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=>$strfnErrmode);

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.


Posted by Paul Lalli on February 3, 2006, 10:11 am
Please log in for more thread options


anu wrote:
> my $ert = $objfnTelnet->cmd("String=>cat -
> >>$strfnToFile",$c,Timeout=>$ifnTimeout,Errmode=>$strfnErrmode);

This does not at all match what the documenation says should be the
arguments to cmd:
(From:
http://search.cpan.org/~jrogers/Net-Telnet-3.03/lib/Net/Telnet.pm )
$ok = $obj->cmd(String => $string,
[Output => $ref,]
[Cmd_remove_mode => $mode,]
[Errmode => $mode,]
[Input_record_separator => $chars,]
[Ors => $chars,]
[Output_record_separator => $chars,]
[Prompt => $match,]
[Rs => $chars,]
[Timeout => $secs,]);

You should be passing a list of key value pairs, the first (required)
one of which has a key of String and a value of the command you want to
run. Instead, your first argument is a single string: "String=>cat -
>>$strfnToFile",

Basically, your quotes are in the wrong position. It should be:
String => "cat - >> $strfnToFile",

> bad named parameter "String=>cat - >>aout.txt" given to Net::Telnet::cmd() at
./telnetcopy.pl

I agree.

Paul Lalli


Similar ThreadsPosted
Remote.pm (File::Remote) error handling question January 29, 2007, 12:57 pm
File::Copy::syscopy August 25, 2004, 8:02 am
Remote.pm (File::Remote) problem February 28, 2006, 9:50 pm
Devel::Cover failing with 'bizarre copy of hash in leave' error October 21, 2004, 2:09 pm
How do you telnet from 1 host to another using Telnet Module December 21, 2005, 4:15 pm
net::telnet to ms telnet server May 19, 2005, 12:28 am
remote keyboard and mouse IO February 21, 2005, 3:22 am
how to execute command on remote machine September 21, 2005, 12:40 am
Remote install of Perl Module April 12, 2005, 10:47 am
Read user log from a remote host July 26, 2006, 5:14 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap