Link to home
Start Free TrialLog in
Avatar of sduser1
sduser1Flag for United States of America

asked on

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.
Avatar of FishMonger
FishMonger
Flag of United States of America image

Enable debug logging.

Start with enabling input_log and if needed, you can enable output_log and/or dump_log
Avatar of sduser1

ASKER

I have enabled input_log.  The file came out empty but I am attaching the dump_log file.
C--Subha-HistoryNotes-dump-log.txt
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.
Avatar of sduser1

ASKER

Just to let you know, the server I am connecting to is SFTP server and also Linux server.
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
Avatar of sduser1

ASKER

Thank you. Let me try Net::SFTP and get back to you if I see any issues.
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.
Avatar of sduser1

ASKER

Thank you. I will try Net::SFTP module
Avatar of sduser1

ASKER

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.
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.
Avatar of sduser1

ASKER

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.
What version of perl are you using and is it 32bit or 64bit?
Avatar of sduser1

ASKER

32bit
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
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
Avatar of sduser1

ASKER

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

Avatar of sduser1

ASKER

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");
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.
Avatar of sduser1

ASKER

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
Avatar of sduser1

ASKER

Looks like the script is hanging at the line sftp:  Sending SSH2_FXP_INIT
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.
Avatar of sduser1

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sduser1

ASKER

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.