IO/Pty error when compiling PERL script - perl

It has been awhile since I have done any PERL programming and I am working on a project where I need to connect to a remote server and get a listing of files. The script is being developed on a Windows 2012 server using ActivePerl.
This is the script that I have put together, from other examples -
#! /usr/bin/env perl
use strict;
use warnings;
use feature qw(say);
use autodie;
use Net::SFTP::Foreign;
# US Server Configuration
use constant {
HOST => "samplehost",
REMOTE_DIR => "sample/remote",
LOCAL_DIR => "sample/local",
PORT => "3235",
USER_NAME => "username",
PASSWORD => "D%password",
DEBUG => "0",
};
my $stfp = Net::SFTP::Foreign->new (
HOST,
timeout => 240,
user => USER_NAME,
password => PASSWORD,
port => PORT,
autodie => 1,
);
#
# List remote directory contents
#
my $remotefiles;
$remotefiles = $stfp->ls(REMOTE_DIR);
#
# Loop through remote files and print each filename
#
foreach ($remotefiles){
my $file = $_;
my $filename = $file->{filename};
if($filename ne "." && $filename ne ".."){
print"the filename is $filename";
}
}
I am receiving an error when I compile it -
C:\development>perl ConvertPDFs.pl
password authentication not available, IO::Pty is not installed or failed to loa
d: Can't locate IO/Pty.pm in #INC (#INC contains: C:/Perl64/site/lib C:/Perl64/l
ib .) at C:/Perl64/site/lib/Net/SFTP/Foreign/Backend/Unix.pm line 256.
at ConvertPDFs.pl line 20.
Not sure what the problem is.

Pseudo-ttys are a unix construct. If a module requires IO::Pty, it won't run on Windows (though maybe on cygwin). On the plus side, the error message makes it sound like Net::SFTP::Foreign only uses IO::Pty for password-based authentication, so you could avoid the problem by switching to key-based authentication.

Related

Perl and Selenium::Remote::Driver

EDITED AGAIN
I have a server on AWS somewhere in Northern Virginia and this is my monitoring server. I ssh into this Ubuntu server from another State to do system administration. I want to do web automation tests on this server which will test a web application on the Internet hitting a URL and verify that I can selenium test a login and authenticate successfully. This server is on an AWS cloud I'm not quite sure which Perl module to use since I'm accessing it remotely.
There are two CPAN modules: Selenium::Remote::Driver and WWW::Selenium. I have tried both and they are giving me issues. And I really don't know which is appropriate for my scenario. When I use Selenium::Remote::Driver, I get the following error:
Selenium server did not return proper status at /usr/local/share/perl/5.18.2/Selenium/Remote/Driver.pm line 401.
When I use WWW::Selenium, I get this error:
Failed to start new browser session: org.openqa.selenium.server.RemoteCommandException: Error while launching browser
I was able to launch firefox manually from the AWS monitoring server by exporting the DISPLAY but it was really slow. I have heard that I can use a headless browser but I would have to export the DISPLAY by:
export DISPLAY=:5
But remember, I'm sshing into this AWS/Selenium server from my desktop so I'm assuming I use the above command on the AWS/Selenium Server while I'm ssh into it from my desktop? Actually, at this point, I'm not sure I'm doing here. Can somebody help?
The problem in this type of questions is that the variety of configurations and binaries in your setup might be so broad that the it is hard to actually provide a straight and correct answer for YOUR SETUP.
This answer has the following assumptions:
you have downloaded the selenium-server-standalone.jar into /usr/lib/
you have jdk 1.8 ( run the java -version in the shell
you have installed and configured the xvfb-run ( it is a fight on it's own )
So :
```
# ssh to your server , obs the -X !
ssh -X user-name#server-name
# start the selenium-server-standalone on the server
xvfb-run -e /dev/stdout java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar /usr/lib/selenium-server-standalone.jar &
# one liner test - this is one veery long one
perl -e 'use strict ; use warnings ; use Data::Printer ; my $host="127.0.0.1"; use Selenium::Remote::Driver;my $driver = Selenium::Remote::Driver->new( "browser_name" =>"chrome", "error_handler" => sub { print $_[1]; croak 'goodbye'; }, "remote_server_addr" => "$host","port"=> "4444");$driver->debug_on();$driver->get("http://www.google.com"); print $driver->get_title();$driver->quit();' &
```
Here is the code in the one-liner as a perl script
#!/usr/bin/env perl
use strict ;
use warnings ;
use Carp ;
use Data::Printer ;
use Selenium::Remote::Driver;
my $host="127.0.0.1";
my $driver = Selenium::Remote::Driver->new(
"browser_name" =>"chrome"
, "error_handler" => sub { print $_[1]; croak 'goodbye' ; }
, "remote_server_addr" => "$host"
, "port"=> "4444") ;
$driver->debug_on() ;
$driver->get("http://www.google.com");
print $driver->get_title();
$driver->quit();
The output should look something like:
```
Prepping get
Executing get
REQ: POST, http://127.0.0.1:4444/wd/hub/session/ddb9c2575ab026cdb8c640bdc554181b/url, {"url":"http://www.google.com"}
RES: {"sessionId":"ddb9c2575ab026cdb8c640bdc554181b","status":0,"value":null}
Prepping getTitle
Executing getTitle
REQ: GET, http://127.0.0.1:4444/wd/hub/session/ddb9c2575ab026cdb8c640bdc554181b/title, {}
RES: {"sessionId":"ddb9c2575ab026cdb8c640bdc554181b","status":0,"value":"Google"}
GooglePrepping quit
Executing quit
REQ: DELETE, http://127.0.0.1:4444/wd/hub/session/ddb9c2575ab026cdb8c640bdc554181b, {}
RES: {"sessionId":"ddb9c2575ab026cdb8c640bdc554181b","status":0,"value":null}
```
Try running the below code:
#!/usr/bin/perl
use warnings;
use strict;
use Selenium::Remote::Driver;
my $host = "10.10.1.1"; //Enter your server IP in this place
my $driver = new Selenium::Remote::Driver('remote_server_addr' => $host,
'port' => '4444',
'auto_close' => 0);
$driver->get('http://www.google.com');

How to Retrive multiple sequences from a database in Bioperl?

I have installed Bioperl1.6 via ppm and have placed .pl file in cgi-bin folder of my localhost. When I run this via url
http://localhost/cgi-bin/bio2.pl
it says "Internal Server Error"
While if run any of the bioperl file either in CGI or .pl it complain same as Internal server error.
#!C:/Perl64/bin/perl.exe
use Bio::SeqIO;
use Bio::DB::GenBank;
use Bio::DB::Query::GenBank;
$query = "Arabidopsis[ORGN] AND topoisomerase[TITL] and 0:3000[SLEN]";
$query_obj = Bio::DB::Query::GenBank->new(-db => 'nucleotide', -query => $query );
$gb_obj = Bio::DB::GenBank->new;
$stream_obj = $gb_obj->get_Stream_by_query($query_obj);
while ($seq_obj = $stream_obj->next_seq) {
# do something with the sequence object
print $seq_obj->display_id, "\t", $seq_obj->length, "\n";
}
if I run this code via cmd then it says genebank cant be found excptn.
Please guide me how to run bioperl in perl cgi.

can't ssh:unable to establish master SSH connection: bad password or master process exited unexpectedly

I have problem with perl modules, i 'm trying to connect to the remote server using the OpenSSH module from CPAN and i have given the username and password correctly but when i run my CGI from browser i see the following error message "
can't ssh:unable to establish master SSH connection: bad password or
master process exited unexpectedly
in need of immediate reply's and awaiting,
Thanks in Advance.
Here is my code
#!/usr/bin/perl -w
use CGI;
use Data::Dumper;
use strict;
use CGI::Carp qw(fatalsToBrowser);
use Net::OpenSSH;
my $query = CGI->new();
#my $mach_name=$query->param('mach_name');
my $mach_name= '****.cce.***.com';
my $user='root';
my $passwd='*******';
my $ssh = Net::OpenSSH->new("$mach_name",user => "$user" , passwd => "$passwd
", master_opts => [-o => "strictHostKeyChecking=no"]);
$ssh->error and die "can't ssh:" . $ssh->error;
my $mem_info =$ssh->capture("ioscan -m lun");
print "$mem_info";
print "Content-type: text/html\n\n";
print "<html><head><title>test page</title></head>\n";
print "<body><p>Dicovering Machine Please wait....</p>\n";
print "<p>$mach_name</p>";
print "<p><b>Swap Memory :$mem_info </b> </p>";
print "</body></html>";
The same program if i run through the command line it is getting the o/p but through the browser i'm seeing the above error i have given the passwd and username correctly
My problem was solved when I've increased the timeout from 10 seconds to 15 seconds, because the device is using an older SSH daemon version, but my linux server that made the client connection is upgraded and of course it probes first the newer ciphers.
my $ssh = Net::OpenSSH->new(
$host,
port => $port,
user => $user,
password => $pass,
timeout => 15, # <= this was 10 and now is 15
kill_ssh_on_timeout => 1,
strict_mode => 0,
master_opts => [-o => "StrictHostKeyChecking=no", '-vvv'], #<= -vvv helped much
ctl_dir =>'/tmp/libnet-openssh-perl',
Looks like some permissions issue. Run ssh in verbose mode:
my $ssh = Net::OpenSSH->new("$mach_name",
user => "$user",
passwd => "$passwd",
master_opts => '-vvv',
master_stderr_fh => \*LOG);
If that doesn't give you enough information about the cause of the problem, then, you can use truss to see what's happening at the OS level.
Also check .ssh/known_hosts, make sure .ssh directory is writeable by Apache. .libnet-openssh-perl directory should be writeable by Apache.
For example, my Apache user is apache with a home directory of /var/www
I have /var/www/.ssh owned by apache, and /var/www/.libnet-openssh-perl owned by apache. I ssh to devices using my own account, then copy my known_hosts file to /var/www/.ssh/known_hosts

SNMP v3 with NET::SNMP working, but snmpwalk/snmpget not?

I have the following (working) perl script:
use Net::SNMP;
# create session to the host
my ($session, $error) = Net::SNMP->session(
-hostname => $hostname,
-version => 'snmpv3',
-username => 'my_user_name',
-authkey => 'my_authkey',#actually, here stands the real authkey as configured on the switch
-privkey => 'my_privkey',#same as on switch
-authprotocol => 'sha',
-privProtocol => 'des'
);
if (!defined($session)) {
print $error . "\n";
last;
}
# retrieve a table from the remote agent
my $result = $session->get_table(
-baseoid => $MAC_OID
);
if (!defined($result)) {
print $session->error . "\n";
$session->close;
last;
}
#print out the result of the snmp query
#....
Now I wanted to use snmpwalk or snmpget with the same keys. For that, I created a snmp.conf file in .snmp of my home directory with the following content:
defSecurityName my_user_name
defContext ""
defAuthType SHA
defSecurityLevel authPriv
defAuthPassphrase my_auth_key here
defVersion 3
defPrivPassphrase my_privkey here
defPrivType DES
As I see it, I use the same credentials in the script and for snmpget. Why do I get snmpget: Authentication failure (incorrect password, community or key) ?
That depends on the version of snmpget and snmpset you use. When I tested an older version of net-snmp against my C# based SNMP agent http://sharpsnmplib.codeplex.com I noticed that for SHA authen mode + DES privacy mode a bug prevented the net-snmp command line tools from generating the correct message bytes (the encryption is wrong so that no agent can decrypt it).
My suggestion is that you try to use Net::SNMP instead, as like you found out, it is not affected by the same bug.
Your problem is that you're using an authentication key for Net::SNMP and a password for the command-line net-snmp tools. Based on your Net::SNMP usage you're actually using 'localized' keys. Which means the right tokens for your snmp.conf file are:
defAuthLocalizedKey 0xHEXSTRING
defPrivLocalizedKey 0xHEXSTRING
See the snmp.conf manual page for further details.

Why does Perl's Net::SFTP->new complain about "Not an ARRAY reference"?

I am trying to use Net::SFTP to get connected to remote server.
My script is:
my %args = (
ssh_args => {
user => 'canneu_scp',
identity_files => [ '/home/home1/cgrshah/responsys/capgemini.private' ],
debug => 1,
} );
my $targetserver='files.responsys.net';
my $sftp = Net::SFTP->new($targetserver, %args)
or die "could not open connection to $targetserver\n";
But when I run this, I get an error stating:
Not an ARRAY reference at /usr/lib/perl5/site_perl/5.8.1/Net/SFTP.pm line 36.
Can anyone help me with this?
This is just a wild shot in the dark, but the user option should not be in the hash handed to ssh_args, it is at the same level. Try using this code instead:
my $sftp = Net::SFTP->new(
$targetserver,
user => 'canneu_scp',
ssh_args => {
identity_files => [ '/home/home1/cgrshah/responsys/capgemini.private' ],
debug => 1,
}
) or die "could not open connection to $targetserver\n";
It sounds like the code above got you further along, and now you are having problems because your version of Math::BigInt is too old. I see three ways to move forward:
switch to an RSA key instead of a DSA key
find an RPM of Math::BigInt version 1.78 or later
manually install a copy of Math::BigInt
The third option has many pitfalls, and if you decide to go with it I would suggest the following steps:
install App::cpanminus
make sure you have a gcc installed
run wget -O- http://cpanmin.us | perl - --local-lib=~/perl5 App::cpanminus
add ~/perl5/bin to your path
install Math::BigInt into your home directory with cpanm --local-lib=~/perl5 Math::BigInt
add use lib "$ENV{HOME}/perl5"; to the start of your script so it can find the new modules