How can I debug my Perl Net::Telnet program? - perl

I am trying to automate telnet to a Linux box which doesn't require any authentication through Net::Telnet Perl module. Here is a test script:
use strict;
use Net::Telnet;
my $telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die',
host=> '10.104.20.200', port => 2004,
);
$telnet->print('ls');
my $output = $telnet->waitfor('/\$ $/i');
print $output;
When I execute this script, I get the below error,
pattern match timed-out at telnetPerl.pl line 7.
As you could see, I am matching for any characters in waitfor. But I doubt whether the telnet connectivity happened in the first place.

The Net::Telnet docs say under the entry for waitfor():
Use dump_log() to debug when this method keeps timing-out and you don't think it should.
What happened when you followed the doc's instructions? :)

Related

net-sftp-foreign error handling for nagios integration

I am rather new in Perl. I have written a very simple script that copies and removes a file from sftp.
but the script should return some kind of output in order to integrate it with nagios.. using nsca or something.
the script is running on solaris 10.
here is the script:
#!/usr/bin/perl -w
use strict;
use warnings;
use Net::SFTP::Foreign;
use POSIX qw(strftime);
my $datestring =strftime "%d-%m-%Y", localtime;
my $host="sftp.mariog.com";
my $username="user";
my $local="/mnt/mariog";
my $file="BookingReport_Daily_$datestring.xls";
print "$file \n";
my $sftp = Net::SFTP::Foreign->new($host,
user => $username,
stderr_discard => 1,
autodie => 1,
);
$sftp->die_on_error("unable to establish SFTP Connection");
$sftp->get("$file", "$local/$file");
$sftp->remove($file);
$sftp->disconnect();
how to handle the fact that the file does not exist on the sftp? maybe it has not reached yet. there a daily file uploaded but at different times. the script is run by cron every 4 hours so most of the times it will not find files to transfer...
where can I get the output code of the transfer successful or not? so that i can pass it to a nagios passive check with nsca...
thank you for your help..
kind regards.
Mario
It looks like one way to check for a successful get in the documentation would be something like this:
$sftp->get("$file", "$local/$file") or die "get failed:" $sftp->error;

Weird issue with Net::SSH::Expect in Perl script

I am working on putting together a perl script. I have captured it below:
#!/usr/bin/perl
use Tie::File;
use Net::SSH::Expect;
use utf8;
use warnings;
use diagnostics;
# Grab password from hidden file
$pw=`cat .password`;
chomp $pw;
#Read list of 9200's from hosts.list file into an array
tie #hosts, 'Tie::File', "hosts.list" or die;
#Loop through hosts, connect via ssh, run commands, and write out log files.
foreach (#hosts) {
#Create ssh session handle
my $ssh = Net::SSH::Expect->new (
host => $_,
password => $pw,
user => 'user',
raw_pty => 1
);
my $login_output = $ssh->login();
if ($login_output !~ /.*sbc.*>/) {
die "Login failed. Login output was $login_output";
}
$ssh->send("show sip errors");
my $line;
while ( defined ($line = $ssh->read_line()) ){
print $line . "\n";
}
$ssh->close();
}
First, I'm not a programmer, so style is probably very ugly. Sorry about that :) The goal is to run several commands on a remote appliance, capture the results in separate files, which will then be consumed by a 3rd party parsing engine (splunk).
The current implemented functionality is able to log in to remote hosts, run the command, and then print out to stdout. Not quite there, but still shows a good proof of concept.
The script runs fine for the first 3 hosts in the hosts.list file. However as soon as it gets to the fourth host, I receive this exception:
Uncaught exception from user code:
SSHAuthenticationError Login timed out. The input stream currently has the contents bellow: user#myhost.mydomain's password: at /System/Library/Perl/Extras/5.12/Expect.pm line 828
at /Library/Perl/5.12/Net/SSH/Expect.pm line 209
Net::SSH::Expect::__ANON__('ARRAY(0x7fd718a03008)') called at /System/Library/Perl/Extras/5.12/Expect.pm line 828
Expect::_multi_expect(1, 'ARRAY(0x7fd7189fbce8)', 'ARRAY(0x7fd7189f7460)') called at /System/Library/Perl/Extras/5.12/Expect.pm line 565
Expect::expect('Expect=GLOB(0x7fd7189f1878)', 1, 'ARRAY(0x7fd718a01530)', 'ARRAY(0x7fd7189f15a8)', 'ARRAY(0x7fd71890a3d0)', 'ARRAY(0x7fd718a07470)', 'ARRAY(0x7fd7189d8b18)') called at /Library/Perl/5.12/Net/SSH/Expect.pm line 580
Net::SSH::Expect::_sec_expect('Net::SSH::Expect=HASH(0x7fd718a29828)', 1, 'ARRAY(0x7fd718a01530)', 'ARRAY(0x7fd7189f15a8)', 'ARRAY(0x7fd71890a3d0)', 'ARRAY(0x7fd718a07470)', 'ARRAY(0x7fd7189d8b18)') called at /Library/Perl/5.12/Net/SSH/Expect.pm line 213
Net::SSH::Expect::login('Net::SSH::Expect=HASH(0x7fd718a29828)') called at ./pcscfFetch.pl line 26
Any ideas on what the problem could be? I am able to log in to the host with no issue manually via ssh. The script works fine for our other hosts, it's just this one outlier that I can't seem to figure out. Any advice would be appreciated. Thanks!
I did end up resolving this. In the constructor for $ssh I set the timeout to 10 seconds, instead of the default 1. The script runs significantly slower, but I don't appear to have the issues I was running into before. Appreciate the feedback!
Net::SSH::Expect is not reliable.
Use Net::OpenSSH instead, or if you want to run the same set of commands in several hosts Net::OpenSSH::Parallel.

Perl LWP does not work

I'm using Padre as my IDE with Strawberry Perl on Windows 7 Pro.
I'm trying to create a perl script that goes to a text file on a website, and then reads/copies the text file.
But I can't get LWP to work even for the simplest LWP command ever.
#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
getprint('http://www.perlmeme.org') or die 'Unable to get page';
exit 0;
I keep getting this error message:
500 can't connect to proxy.sn.no:8001 (Bad hostname)
eg 500 can't connect to (Bad hostname) http://www.perlmeme.org
I've been googling around, used Microsoft Fixit to reset ports, etc but I still can't make it work. http//www.justskins.com/forums/lwp-connect-failing-bad-119421.html
Can anyone help me out here? Been stuck for many hours :(
Edit:
--1 foreach my $key (keys %ENV) { print "$key: $ENV{$key}\n" if $key =~ m/proxy/i; }
Yes it prints out FTP_PROXY and HTTP_PROXY both followed by this: http://proxy.sn.no:8001/
That's the proxy that I got from this helpthread How do I install a module? Strawberry Perl issues
I had the proxy problem, then I tried the config from that thread, then the proxy problem was still there.
--2 I'm not expecting any proxy to be used on my end or anything. Just wanna connect the perl script to the website to retrieve a text document.
--3 ping had 0% loss. (I can only post two hyperlinks in this post)
--4 I'm using Windows.
LWP will honor the http_proxy environment variable and try to use it as an HTTP proxy. Check with env | grep http_proxy on Unix.

telnet inline with perl?

Is there a way to allow perl to initiate a telnet session and programmatically issue commands to that telnet session?
I initially tried a stupid method:
commands.pl:
sleep(1);
print $command1;
sleep(1);
print $command2;
and then
> perl commands.pl | telnet www.host.com port
This does not work.
There is a Net::Telnet module.
use Net::Telnet ();
$t = new Net::Telnet (Timeout => 10,
Prompt => '/bash\$ $/');
$t->open("sparky");
$t->login($username, $passwd);
#lines = $t->cmd("who");
print #lines;
(Example taken from that page.)
One option would be through Perl's interface to Expect.

Why does my Net::Telnet program timeout?

I'm written small code to connect to remote server using Perl but observing error messages
#!/usr/bin/perl -w
use Net::Telnet;
$telnet = new Net::Telnet ( Timeout=>60, Errmode=>'die');
$telnet->open('192.168.50.40');
$telnet->waitfor('/login:/');
$telnet->print('queen');
$telnet->waitfor('/password:/');
$telnet->print('kinG!');
$telnet->waitfor('/:/');
$telnet->print('vol >> C:\result.txt');
$telnet->waitfor('/:/');
$telnet->cmd("mkdir vol");
$telnet->print('mkdir vol234');
$telnet->cmd("mkdir vol1");
$telnet->waitfor('/\$ $/i');
$telnet->print('whoamI');
print $output;
But while running i'm getting following errors
C:\>perl -c E:\test\net.pl
E:\test\net.pl syntax OK
C:\>perl E:\test\net.pl
command timed-out at E:\test\net.pl line 13
C:\>
Help me in this regard. I'm new to Perl.
I'm not sure about that Net::Telnet, but using '/:/' (with quotes) I guess is the problem. I.e. /:/ (within slashes) - that's regular expression, but with quotes that's simply string which should appear on terminal (i.e. it waits for string '/:/' - slash, two dots, slash).
To debug such programs (if Net::Telnet doesn't echo interaction with remote system) you can simply put: print "I'm waiting for login...\n" at lines before waitfor()
Why aren't you using Net::Telnet's login method to log-in? When you're at such a low level, you have to handle all of the details yourself. If you look in the source for that method, you'll see it doing quite a bit of work, including a kludge to get around a login bug on Linux.