remote file copy using Net::Telnet

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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.

#To copy file from local machine to remote machine.

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
    print("Your programme will be terminated\n");

if ($strfnUid eq ""){
    print("You did not enter your userid\n");
    print("Your programme will be terminated\n");


if ($strfnPass eq ""){
    print("You did not enter your password\n");
    print("Your programme will be terminated\n");

#based on argument string patterns assigning values to variables

my($strfnFromHost, $strfnFromFile, $strfnToHost, $strfnToFile);
if ($strfnArg1 =~ /:/) {
     ($strfnFromHost, $strfnFromFile) = split /:/,$strfnArg1, 2;
elsif ($strfnArg2 =~ /:/) {
     ($strfnToHost, $strfnToFile) = split /:/, $strfnArg2, 2;

#opening connection with the remote host by creating object and

my $objfnTelnet=new



    #provide username and password

    $objfnTelnet->login(Name => $strfnUid,Password =>

# 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");

        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 -
Quoted text here. Click to load it

    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 ./

I am quite to network programming please do clarify this doubt of mine

"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.


Re: remote file copy using Net::Telnet

anu wrote:
Quoted text here. Click to load it

This does not at all match what the documenation says should be the
arguments to cmd:
(From: )
    $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 -
Quoted text here. Click to load it

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

Quoted text here. Click to load it

I agree.

Paul Lalli

Site Timeline