Connecting to linux server using Net::Telnet module

I am using the below code to connect to the Linux server. I am getting error.

bad match operator: Search pattern not terminated at C:/Subha/eclipse/Perl Automation/ServerConnect.pl line 12

Below is the code.

#!/usr/bin/perl
use warnings;
use strict;
use Net::Telnet;
my $host = "lmmk.com";
my $user = "user";
my $passwd = "pwd";
my $telnet_con = Net::Telnet->new(Host    => $host,
                                  Port    => 22,
                                  Timeout => 10,
                                  Prompt  => '/\$ $');
my $output=$telnet_con->cmd('ls');
print "$output";
$telnet_con->close;

Please let me know what is wrong with the above code.

Thank you.
sduser1Asked:
Who is Participating?
 
FishMongerCommented:
Since Windows doesn't come with an ssh command, you'll need 2 additional modules.
Net:SSH2 which provides the ssh functionality.
Net::SFTP::Foreign::Backend::Net_SSH2 to link it to Net::SFTP::Foreign

#!/usr/bin/perl

use warnings;
use strict;
use Net::SFTP::Foreign;
use Data::Dumper;

my $host = '10.100.0.118';
my $sftp = Net::SFTP::Foreign->new($host,
                                   backend => 'Net_SSH2',
                                   user => 'me',
                                   password => 'mypass');
$sftp->die_on_error("Unable to establish SFTP connection");
$sftp->setcwd('/home') or die "unable to change cwd: " . $sftp->error;

my @files = $sftp->ls('.');
print Dumper \@files;

Open in new window


c:\test>Perl-1.pl
$VAR1 = [
          [
            {
              'a' => bless( {
                              'mtime' => 1361376188,
                              'atime' => 1431030184,
                              'perm' => 16877,
                              'flags' => 15,
                              'size' => 4096,
                              'uid' => 0,
                              'gid' => 0
                            }, 'Net::SFTP::Foreign::Attributes' ),
              'filename' => '.',
              'longname' => 'drwxr-xr-x    3 root     root         4096 Feb 20  2013 .'
            },
            {
              'filename' => '..',
              'longname' => 'drwxr-xr-x   24 root     root         4096 May  6 08:59 ..',
              'a' => bless( {
                              'gid' => 0,
                              'uid' => 0,
                              'flags' => 15,
                              'size' => 4096,
                              'atime' => 1431024163,
                              'perm' => 16877,
                              'mtime' => 1430927972
                            }, 'Net::SFTP::Foreign::Attributes' )
            },
            {
              'filename' => 'rkb',
              'longname' => 'drwx--x--x   63 rkb      rkb         12288 May  7 08:54 rkb',
              'a' => bless( {
                              'mtime' => 1431014091,
                              'perm' => 16841,
                              'atime' => 1431017837,
                              'flags' => 15,
                              'size' => 12288,
                              'uid' => 500,
                              'gid' => 500
                            }, 'Net::SFTP::Foreign::Attributes' )
            }
          ]
        ];

Open in new window

0
 
FishMongerCommented:
Enable debug logging.

Start with enabling input_log and if needed, you can enable output_log and/or dump_log
0
 
sduser1Author Commented:
I have enabled input_log.  The file came out empty but I am attaching the dump_log file.
C--Subha-HistoryNotes-dump-log.txt
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
FishMongerCommented:
I don't see anything obvious in the script that would account for the error message.

Our company has removed telnet from our servers for PCI compliance, but If I can find one which still has it installed, I'll run a test to see if I can duplicate the problem.
0
 
sduser1Author Commented:
Just to let you know, the server I am connecting to is SFTP server and also Linux server.
0
 
FishMongerCommented:
If you're connecting to an SFTP server, why are you using Net::Telnet?

If you're needing to do any file transfers, then you'd be better off using Net::SFTP::Foreign or possibly Net::SSH::Any
0
 
sduser1Author Commented:
Thank you. Let me try Net::SFTP and get back to you if I see any issues.
0
 
FishMongerCommented:
If you're connecting from Windows, as you stated in your perlguru thread, and want to try Net::SFTP::Foreign, you'll need to do that under the cygwin environment.

Another opion, which doesn't require cygwin, would be to use the older Net::SFTP module.
0
 
sduser1Author Commented:
Thank you. I will try Net::SFTP module
0
 
sduser1Author Commented:
I am having issues installing NET::SFTP Module.

Can't locate Net/SSH/Perl/Buffer.pm in @INC (you may need to install the Net::SSH::Perl::Buffer module) C:/Perl/site/lib C:/Perl/lib .) at C:/Perl/lib/Net/SFTP/Buffer.pm line 6.
BEGIN failed--compilation aborted at C:/Perl/lib/Net/SFTP/Buffer.pm line 6.
Compilation failed in require at C:/Perl/lib/Net/SFTP/Attributes.pm line 7.
BEGIN failed--compilation aborted at C:/Perl/lib/Net/SFTP/Attributes.pm line 7.
Compilation failed in require at C:/Perl/lib/Net/SFTP.pm line 8.
BEGIN failed--compilation aborted at C:/Perl/lib/Net/SFTP.pm line 8.
Compilation failed in require at C:/Subha/eclipse/Perl Automation/SFTPServerConnect.pl line 5.
BEGIN failed--compilation aborted at C:/Subha/eclipse/Perl Automation/SFTPServerConnect.pl line 5.
0
 
FishMongerCommented:
This is a common type of error when installing this module.  One of the dependencies Net::SFTP requires is Net::SSH::Perl which has a lot of its own dependencies.  Some of those dependencies are a pain to install on Windows, but not impossible.

Lookup the Net::SSH::Perl dependencies and build status for your version of perl and install each dependency individually.

Once those are installed, install Net::SSH::Perl and assuming that all goes well, then install Net::SFTP.

The last time I installed Net::SSH::Perl, which was about 5 years ago, I had to install a couple additional modules which were not required dependencies, but were needed to speed up the authentication process.
0
 
sduser1Author Commented:
I was in the process of installing the modules and I am stuck here.

Can't locate loadable object for module Math::Pari in @INC.

Can't locate loadable object for module Math::Pari in @INC (@INC contains: C:/Subha/eclipse/.metadata/.plugins/org.epic.debug C:/Subha/eclipse/Perl Automation C:/Perl/site/lib C:/Perl/lib .) at C:/Perl/lib/Net/SSH/Perl/Util/SSH2MP.pm line 6.
Compilation failed in require at C:/Perl/lib/Net/SSH/Perl/Util/SSH2MP.pm line 6.
BEGIN failed--compilation aborted at C:/Perl/lib/Net/SSH/Perl/Util/SSH2MP.pm line 6.
Compilation failed in require at C:/Perl/lib/Net/SSH/Perl/Util.pm line 56.
BEGIN failed--compilation aborted at C:/Perl/lib/Net/SFTP/Buffer.pm line 9.
Compilation failed in require at C:/Perl/lib/Net/SFTP/Attributes.pm line 7.
BEGIN failed--compilation aborted at C:/Perl/lib/Net/SFTP/Attributes.pm line 7.
Compilation failed in require at C:/Perl/lib/Net/SFTP.pm line 8.
BEGIN failed--compilation aborted at C:/Perl/lib/Net/SFTP.pm line 8.
0
 
FishMongerCommented:
What version of perl are you using and is it 32bit or 64bit?
0
 
sduser1Author Commented:
32bit
0
 
FishMongerCommented:
You should be able to install it via ppm.

Example:
c:\test>ppm install http://www.bribes.org/perl/ppm/Math-Pari.ppd
Installing package 'http://www.bribes.org/perl/ppm/Math-Pari.ppd'...
Bytes transferred: 1303124
Use of chdir('') or chdir(undef) as chdir() is deprecated at C:/Strawberry/perl/vendor/lib/PPM.pm line 393.
Files found in blib\arch: installing files in blib\lib into architecture dependent library tree
Installing C:\STRAWB~1\perl\site\lib\auto\Math\Pari\Pari.dll
Installing C:\STRAWB~1\perl\site\lib\Math\libPARI.dumb.pod
Installing C:\STRAWB~1\perl\site\lib\Math\libPARI.pod
Installing C:\STRAWB~1\perl\site\lib\Math\Pari.pm
Installing C:\STRAWB~1\perl\site\lib\Math\PariInit.pm
0
 
FishMongerCommented:
What's your status?  Were you able to install the modules, or are you still having problems?

Since these modules are a pain to install on Windows using the normal method (i.e., cpan), you should probably use ppm to install pre-compiled binaries from the Bribes repository.

My prior post showed how to use ppm to install the Math::Pari module.  You can use the same procedure to install Net::SSH::Perl and Net::SFTP

c:\test>ppm install http://www.bribes.org/perl/ppm/Net-SSH-Perl.ppd
Installing package 'http://www.bribes.org/perl/ppm/Net-SSH-Perl.ppd'...
Bytes transferred: 3188
Use of chdir('') or chdir(undef) as chdir() is deprecated at C:/Strawberry/perl/vendor/lib/PPM.pm line 393.
Installing C:\STRAWB~1\perl\site\lib\Crypt\DH.pm
Bytes transferred: 8177
Use of chdir('') or chdir(undef) as chdir() is deprecated at C:/Strawberry/perl/vendor/lib/PPM.pm line 393.
Files found in blib\arch: installing files in blib\lib into architecture dependent library tree
Installing C:\STRAWB~1\perl\site\lib\auto\String\CRC32\CRC32.dll
Installing C:\STRAWB~1\perl\site\lib\String\CRC32.pm
Installing C:\STRAWB~1\perl\site\lib\String\CRC32.pod
Bytes transferred: 71869
Use of chdir('') or chdir(undef) as chdir() is deprecated at C:/Strawberry/perl/vendor/lib/PPM.pm line 393.
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Agent.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Auth.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\AuthMgr.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Buffer.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Channel.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\ChannelMgr.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Cipher.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Comp.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Config.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Constants.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Handle.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Kex.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Key.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Mac.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Packet.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\SSH1.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\SSH2.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Util.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Auth\ChallengeResponse.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Auth\KeyboardInt.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Auth\KeyboardInteractive.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Auth\Password.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Auth\PublicKey.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Auth\Rhosts.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Auth\Rhosts_RSA.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Auth\RSA.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Cipher\Blowfish.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Cipher\CBC.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Cipher\CFB.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Cipher\DES.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Cipher\DES3.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Cipher\IDEA.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Cipher\RC4.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Comp\Zlib.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Handle\SSH1.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Handle\SSH2.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Kex\DH1.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Key\DSA.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Key\RSA.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Key\RSA1.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Subsystem\Client.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Subsystem\Server.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Util\Authfile.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Util\Hosts.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Util\RSA.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Util\SSH1Misc.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Util\SSH1MP.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Util\SSH2MP.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SSH\Perl\Util\Term.pm

c:\test> ppm install http://www.bribes.org/perl/ppm/Net-SFTP.ppd
Installing package 'http://www.bribes.org/perl/ppm/Net-SFTP.ppd'...
Bytes transferred: 11960
Use of chdir('') or chdir(undef) as chdir() is deprecated at C:/Strawberry/perl/vendor/lib/PPM.pm line 393.
Installing C:\STRAWB~1\perl\site\lib\Net\SFTP.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SFTP\Attributes.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SFTP\Buffer.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SFTP\Constants.pm
Installing C:\STRAWB~1\perl\site\lib\Net\SFTP\Util.pm
0
 
sduser1Author Commented:
Thanks for all your help.

I am able to install all the modules.  

I am using the below code to connect to SFTP server.

Please correct if anything is wrong.

my $sftp = Net::SFTP->new($host,$user,$passwd);
$sftp->status;
$sftp->get('/fimsdevapp/sample.txt','c:\sample.txt' ])

I get the error 'Use of uninitialized value $ENV{"HOME"} in concatenation (.) or string at C:/Perl/site/lib/Net/SSH/Perl.pm line 111.
The getpwuid function is unimplemented at C:/Perl/site/lib/Net/SSH/Perl/SSH2.pm line 39.
0
 
FishMongerCommented:
First step is to set the "HOME" environment variable to fix the "getpwuid function is unimplemented" issue.
This needs to be prior to creating the $sftp object.
$ENV{HOME} = 'C:/path/to/your/homedir';

Open in new window

This should also work.
$ENV[HOME] = $ENV{USERPROFILE};

Open in new window


When creating the $sftp object, the second parameter should be a hash.
Change:
my $sftp = Net::SFTP->new($host,$user,$passwd);

Open in new window

To:
my $sftp = Net::SFTP->new($host, user => $user, password => $passwd );

Open in new window

or
my %args = ( user => $user, password => $password);
my $sftp = Net::SFTP->new($host, %args);

Open in new window

0
 
sduser1Author Commented:
Thank you. I get no errors when I added your code.

Not sure if it is connected to the server as I don't see any errors. I added the print statement but it doesn't print the message 'Connected'

Below is the code.

#!/usr/bin/perl
use warnings;
use strict;
use Net::SFTP;

$ENV{HOME} = $ENV{USERPROFILE};

my $host = 'lmmk.fmr.com';
my $user = "user1";
my $passwd = "pwd";

my $sftp = Net::SFTP->new($host,user => $user, password => $passwd);
print "connected\n";
$sftp->status;
$sftp->get("/fimsdev/SIT/files/test/CLARCH_WI_2015-04-29-08-50-05_O.csv","c:/CLARCH_WI_2015-04-29-08-50-05_O.csv");
0
 
FishMongerCommented:
Do you have root access on the box you're connecting to?  If so, ssh into it and tail the secure log file while you run the script to confirm that the server is accepting your login.

You probably should enable debug to see if that provides anything helpful.
0
 
sduser1Author Commented:
Enabled debug and here is the debug log.

LA973353: Reading configuration data C:\Users\a414475/.ssh/config
LA973353: Reading configuration data /etc/ssh_config
LA973353: Connecting to lmmk120.fmr.com, port 22.
LA973353: Remote version string: SSH-2.0-OpenSSH_5.3


LA973353: Remote protocol version 2.0, remote software version OpenSSH_5.3

LA973353: Net::SSH::Perl Version 1.38, protocol version 2.0.
LA973353: No compat match: OpenSSH_5.3
.
LA973353: Connection established.
LA973353: Sent key-exchange init (KEXINIT), wait response.
LA973353: Algorithms, c->s: 3des-cbc hmac-sha1 none
LA973353: Algorithms, s->c: 3des-cbc hmac-sha1 none
LA973353: Entering Diffie-Hellman Group 1 key exchange.
LA973353: Sent DH public key, waiting for reply.
LA973353: Received host key, type 'ssh-dss'.
LA973353: Host 'lmmk120.fmr.com' is known and matches the host key.
LA973353: Computing shared secret key.
LA973353: Verifying server signature.
LA973353: Waiting for NEWKEYS message.
LA973353: Send NEWKEYS.
LA973353: Enabling encryption/MAC/compression.
LA973353: Sending request for user-authentication service.
LA973353: Service accepted: ssh-userauth.
LA973353: Trying empty user-authentication request.
LA973353: Authentication methods that can continue: publickey,password.
LA973353: Next method to try is publickey.
LA973353: Next method to try is password.
LA973353: Trying password authentication.
LA973353: Login completed, opening dummy shell channel.
LA973353: channel 0: new [client-session]
LA973353: Requesting channel_open for channel 0.
LA973353: channel 0: open confirm rwindow 0 rmax 32768
LA973353: channel 1: new [client-session]
LA973353: Requesting channel_open for channel 1.
LA973353: Sending subsystem: sftp
LA973353: Requesting service subsystem on channel 1.
LA973353: channel 1: open confirm rwindow 0 rmax 32768
LA973353: sftp: Sending SSH2_FXP_INIT
0
 
sduser1Author Commented:
Looks like the script is hanging at the line sftp:  Sending SSH2_FXP_INIT
0
 
FishMongerCommented:
I've been running some tests and I'm hanging at that point as well.  I have not found the cause/solution yet, but will do some more testing.

So far, my research indicates that the best solution is switch to Net::SFTP::Foreign.  However, if you do that, you should use ssh keys instead of the password authentication.  The password auth will require the use of IO::Pty and on Windows that module only works under the cygwin environment.
0
 
sduser1Author Commented:
Thanks for your help. I installed NET::SFTP::Foreign and I used the below to connect

use Net::SFTP::Foreign;
    my $sftp = Net::SFTP::Foreign->new($host);
   $sftp->die_on_error("Unable to establish SFTP connection");

    $sftp->setcwd('/fimsdev/clientWorkitemArchive/test') or die "unable to change cwd: " . $sftp->error;

I am getting  ''ssh' is not recognized as an internal or external command,'

Please let me know how to install ssh.
0
 
sduser1Author Commented:
Hi,

Thank you. I am able to use the above code and connect to the server. I  got the list of files.

I was on vacation on Friday and could not respond to this email.. Thanks for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.