Perl - Run script remotely through SSH that includes a require directive - perl

Consider the following 2 scripts :
main.pl
#!/usr/bin/env perl
require 'lib.pl';
testSub('Hello World');
lib.pl
sub testSub
{
my ($input) = #_;
print $input, "\n";
}
1;
I need to run main.pl script remotely through SSH using sudo while including lib.pl as a dependency.
I know how to run a single Perl script through SSH using sudo :
$ ssh user#host 'sudo perl' < /path/to/local/perl/script
But, in this case, the following error is immediately raised :
Can't locate libs.pl in #INC (#INC contains: /usr/lib/perl5/5.10.0/x86_64-linux-thread-multi /usr/lib/perl5/5.10.0 /usr/lib/perl5/site_perl/5.10.0/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.10.0 /usr/lib/perl5/vendor_perl/5.10.0/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.10.0 /usr/lib/perl5/vendor_perl .) at - line XX.
But how could I do the same while including another script as a dependency ?
Thanks all in advance.

Perl can only look at the local file system for includes and can not call back through a ssh channel to get data. So you either need to transfer the libs.pl to the other side before executing the program or just include its content directly in your program.
If you don't like to do this maybe Object::Remote can help you. But don't ask me how to use it, I've only heard a great talk about it.

Related

Can't locate URI/Escape.pm in #INC on AWS lambda

I have an AWS Lamba function in python that generates a url using a shell script.
The input parameters to the shell script is provided by the python code.
The shell script uses perl to URL encode the required URL.
This script runs fine when I run in locally;however on AWS lamba looks like the perl MURI is not installed. Would appreciate if there is alternative way to do this or any other suggestions to go about it.I am doing URL encode by the following commands. The error I see in lambda logs is:
Can't locate URI/Escape.pm in #INC (#INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .).
BEGIN failed--compilation aborted.
Code in the script is
cred=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$PARM1/$DATE/$PUBLIC_REGION/$SERVICE_NAME/request")
FULLDT=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$FULLDT")
SESSION_TOKEN=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$SESSION_TOKEN")
however on AWS lamba looks like the perl MURI is not installed
You're almost right. the -M is a command-line option telling Perl to load a module. The complete module name is "URI::Escape".
And, yes, that module isn't part of a standard Perl installation, so you'll need to install it separately as part of whatever process you use to set up the environment that your lambda runs in. The simplest approach is probably to run:
cpan URI::Escape
But I'm not sure how that would translate to the lambda environment.

.exe crated using Par::Packer with script containing Net::SSH2 is not working

Am using Net::SSH2 module in my Perl script and trying to make an executable (.exe for windows) using Par::Packer. The script need to ssh to a linux box from a windows box using public keys and don't want to input password.
Am facing the below error while running the .exe
Can't load
'C:\Users...\AppData\Local\Temp\par-73656172756d7567616d\cache-a6d57d5a234829aae84d08c4a78a09ad307ea8d3\a9057da2.xs.dll'
for module Net::SSH2: load_file:The specified module could not be
found at C:/Strawberry/perl/lib/DynaLoader.pm line 193. at
C:/Strawberry/perl/vendor/lib/PAR/Heavy.pm line 123.
Tried to workaround with the below methods but none of them worked
Tried to pack from a portable version of Strawberry Perl
Tried to link a9057da2.xs.dll while packing using Par::Packer
Is there any workaround or fix for packing the perl scrip to .exe with Net:SSH2.
Or are there alternatives to Net::SSH2 for ssh from windows to Linux using public keys (not passing passwords)?
If you get something like:
Can't load D:\TEMP\par-76696b616d7768\cache-48ca417d0c47dd7f7245a1218d8d6614470afa93\7c34139c.xs.dll' for module XML::LibXML: load_file:The specified module could not be found at <embedded>/DynaLoader.pm line 193.
at <embedded>/PAR/Heavy.pm line 140.
BEGIN failed--compilation aborted at D:\TEMP\par-76696b616d7768\cache-48ca417d0c47dd7f7245a1218d8d6614470afa93\inc\lib/XML/LibXML.pm line 156.
Compilation failed in require at script/test.pl line 3.
BEGIN failed--compilation aborted at script/test.pl line 3.
then add the following test code
(check your path to Listdlls.exe and fix the below as needed)
#This is the code causing the issue
use XML::LibXML;
my $x = XML::LibXML->new();
ListPerlDLLs();
sub ListPerlDLLs
{
my #ret = `C:\\"Program Files"\\Sysinternals\\Listdlls.exe $$`; # $$ : current PID
foreach my $line (#ret)
{
$line =~ s/\s$//;
next if $line !~ /^0x\w+\s+\w+\s+(.+)/;
my $dll = $1;
next if $dll =~ /^c:\\windows\\/i;
print "$dll\n";
}
}
The Listdlls.exe is from Microsoft - see SysInternals \
First, double-click Listdlls.exe to accept the disclaimer
(will ask for approval on first launch).
When you run the Perl script, you will get something like this:
D:/strawberry-perl/perl/bin/perl.exe
D:/strawberry-perl/perl/bin/perl532.dll
D:/strawberry-perl/perl/bin/libgcc_s_seh-1.dll
D:/strawberry-perl/perl/bin/libwinpthread-1.dll
D:/strawberry-perl/perl/bin/libstdc++-6.dll
D:/strawberry-perl/perl/lib/auto/Encode/Encode.xs.dll
D:/strawberry-perl/perl/lib/auto/Fcntl/Fcntl.xs.dll
D:/strawberry-perl/perl/lib/auto/Storable/Storable.xs.dll
D:/strawberry-perl/perl/lib/auto/Data/Dumper/Dumper.xs.dll
D:/strawberry-perl/perl/lib/auto/IO/IO.xs.dll
D:/strawberry-perl/perl/vendor/lib/auto/XML/LibXML/LibXML.xs.dll
D:/strawberry-perl/c/bin/libxml2-2__.dll
D:/strawberry-perl/c/bin/libiconv-2__.dll
D:/strawberry-perl/c/bin/liblzma-5__.dll
D:/strawberry-perl/c/bin/zlib1__.dll
D:/strawberry-perl/perl/lib/auto/List/Util/Util.xs.dll
if you build your EXE and run it, you will get something like:
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/test.exe
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/perl532.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/libgcc_s_seh-1.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/libwinpthread-1.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/libstdc++-6.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/5461bd7b.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/1ed3ae5b.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/89b9c18b.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/18f23f26.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/0e796a00.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/5d0ea4ea.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/d3d27bc6.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/d8bcc032.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/64916f0f.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/76c6a0cc.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/6b1ff21f.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/4eceebd6.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/ebaed210.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/831f407e.xs.dll
D:/TEMP/par-76696b616d7768/cache-48ca417d0c47dd7f7245a1218d8d6614470afa93/7c34139c.xs.dll
D:/strawberry-perl/c/bin/libxml2-2__.dll
D:/strawberry-perl/c/bin/liblzma-5__.dll
D:/strawberry-perl/c/bin/zlib1__.dll
D:/strawberry-perl/c/bin/libiconv-2__.dll
The last four are the missing DLLs -
add them with --link parameters:
call pp test.pl -o test.exe --link D:/strawberry-perl/c/bin/libxml2-2__.dll --link D:/strawberry-perl/c/bin/liblzma-5__.dll --link D:/strawberry-perl/c/bin/zlib1__.dll --link D:/strawberry-perl/c/bin/libiconv-2__.dll
Probably you are not packing the libssh2 and openssl DLLs inside the executable.
Par::Packer does not detect DLL dependencies automatically, you need to tell it explicitly which ones your program needs (a simple way to discover which DLLs your program needs is to use Process Monitor, an utility which allows one to monitor process activity).
Another option is to use my module Win32::Packer to pack the application which does find DLL dependencies automatically.

Problems with ROUGE on Windows

I am trying to use ROUGE on Windows:
So the first few lines of the file:
use XML::DOM;
use DB_File;
use Getopt::Std;
#-------------------------------------------------------------------------------------
use vars qw($opt_a $opt_b $opt_c $opt_d $opt_e $opt_f $opt_h $opt_H $opt_m $opt_n $opt_p $opt_s $opt_t $opt_l $opt_v $opt_w $opt_2 $opt_u $opt_x $opt_U $opt_3 $opt_M $opt_z);
And it gives me this error:
Can't locate DB_File.pm in #INC (#INC contains: C:/Perl64/site/lib C:/Perl64/lib
.) at C:\RELEASE-1.5.5\ROUGE-1.5.5.pl line 174.
BEGIN failed--compilation aborted at C:\RELEASE-1.5.5\ROUGE-1.5.5.pl line 174.
I am new to perl. All i need is ROUGE to work. What does this mean? How can i fix it?
The author of the perl script probably did not test his script
on windows.
You could probably hack the script to make it work, but
since you're new to perl and seem to be in a hurry,
I suggest you use linux/unix for ROGUE.
The README for ROGUE mentions linuxy things like bash etc,
which is a clue.
If you insist, here's a way to get the perl script running
on windows, but be warned, further errors can pop up
as you get deeper in to the features.
remove activeperl
install strawberryperl 32bit
cpan install XML:DOM
cpan install DB_File
try:
close existing windows
open a new command prompt
run this command
ppm install DB_File
close window
try running rogue again
hope it helps
---
if you have a copy of the rogue application please share it so we can check it out.

Apache doesn't see pm file

I have installed Apache 2.2 and strawberry perl. I have textxx.pm. And I prepare simple script
#!/usr/bin/perl
use textxx;
print("HelloWorld!");
When I run it via cmd it works. When I run it via web browser I get and error:
Can't locate textxx.pm in #INC (#INC contains: C:/strawberry/perl/site/lib C:/strawberry/perl/vendor/lib C:/strawberry/perl/lib .)
It's weird because script without "use" works.
My module is located at the same directory as script
You are trying to use the textxx.pm perl module.. but it is not found while running through apache, if you remove the use line it will obviously work since it's not looking for the module.
a quick fix, if your textxx.pm is in let's say C:/strawberry/perl/site/lib then just add this at the top of your script:
#!/usr/bin/perl
use lib "C:/strawberry/perl/site/lib";
use textxx;
print("HelloWorld!");
Lack of permission could be the issue, but I'm betting the problem is that you're expecting Perl to look in the directory containing the script for the module. Perl doesn't do that, at least not by default.
It worked from the console because you set the current work directory happened to the be the same as the script, but that's not the case when you're running it through apache.
To tell Perl to look in the same directory as the script, add the following because use textxx;:
use Cwd qw( realpath );
use File::Basename qw( dirname );
use lib dirname(realpath($0));

installing Net/SSH/Perl.pm

I am fairly new to scripting with perl. I am trying to ssh into a server and perform some commands. I have to provide a username and password.
When trying to run the script that i have so far (code listed below). I get the following error.
I am assuming this means that i need to install/make Net/SSH/Perl.pm, however, when i follow the instructions and tutorials i have found online, none have worked.
Can someone please assist me. I have tried CPAN and ppm, CPAN says it cannot find net::ssh::perl or other variations of that string. And ppm will not even run, i do not see it in my perl directory. Any help is greatly appreciated!
// The code
#!/usr/bin/perl
use Net::SSH::Perl;
$uName = "username";
$pWord = "password";
$unitIp = $ARGV[0];
my $ssh = Net::SSH::Perl->new($unitIp, 35903);
$ssh->login($uName, $pWord);
my $out = $ssh->cmd("java -version");
print $out;
// the error that is returned
Can't locate Net/SSH/Perl.pm in #INC (#INC contains: /usr/lib/perl5/5.8.5/i386-linux-thread-multi /usr/lib/perl5/5.8.5 /usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.4/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.3/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.2/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl/5.8.4 /usr/lib/perl5/site_perl/5.8.3 /usr/lib/perl5/site_perl/5.8.2 /usr/lib/perl5/site_perl/5.8.1 /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.4/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.3/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.2/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl/5.8.4 /usr/lib/perl5/vendor_perl/5.8.3 /usr/lib/perl5/vendor_perl/5.8.2 /usr/lib/perl5/vendor_perl/5.8.1 /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl .) at needsAName.pl line 31.
BEGIN failed--compilation aborted at needsAName.pl line 31.
You can try this:
curl -L http://cpanmin.us | perl - --sudo App::cpanminus
cpanm Net::SSH::Perl
case sensitive.
Explanation:
1st line will install "cpanm" - what is IMHO the easiest way installing modules
2nd line will install the module Net::SSH::Perl
Instead of Net::SSH::Perl try using Net::SSH2 or if you are in an Unix/Linux environment Net::OpenSSH. They are far easier to install!
Why should you use Net::OpenSSH instead of other perl ssh? This is what I found after install it via cpanm:
Net::OpenSSH Vs Net::SSH::.* modules
Why should you use Net::OpenSSH instead of any of the other Perl SSH
clients available?
Well, this is the perldoc writters (biased) opinion:
Net::SSH::Perl is not well maintained nowadays (update: a new
maintainer has stepped in so this situation could change!!!), requires
a bunch of modules (some of them very difficult to install) to be
acceptably efficient and has an API that is limited in some ways.
Net::SSH2 is much better than Net::SSH::Perl, but not completely stable
yet. It can be very difficult to install on some specific operative
systems and its API is also limited, in the same way as Net::SSH::Perl.
Using Net::SSH::Expect, in general, is a bad idea. Handling interaction
with a shell via Expect in a generic way just can not be reliably done.
Net::SSH is just a wrapper around any SSH binary commands available on
the machine. It can be very slow as they establish a new SSH connection
for every operation performed.
In comparison, Net::OpenSSH is a pure perl module that doesn't have any
mandatory dependencies (obviously, besides requiring OpenSSH binaries).
just type cpan on terminal and then type: force install Net::SSH::Perl
perl -MCPAN -e 'install Net::SSH::Perl'