perl script working in vmware server but fails in vmware ESXi - perl

This problem is really puzzling to me: I have the following script working on vmware server 2.0:
#!/usr/local/bin/perl
# server (transmitter)
use strict;
use IO::Socket::Multicast6;
use IO::Interface;
use constant GROUP => "235.1.1.2";
use constant PORT => "3000";
my $sock = IO::Socket::Multicast6->new(
Proto=>"udp",
Domain=>AF_INET,
PeerAddr=>GROUP,
PeerPort=>PORT);
$sock->mcast_if("eth1");
$sock->mcast_ttl(10);
while (1) {
my $message = localtime();
$sock->send($message) || die "Could not send: $!";
} continue {
sleep 4;
}
It works great on vmware server. I have cloned this VM to an EXSi server but running the same exact copy of the virtual machine running the script, and I get the following error:
Can't call method "mcast_if" on an undefined value
Im really puzzled by this as I am not sure what the problem could be.
there is really nothing different except for the CPU running on both machines, but I don't see how something so low level can be causing an issue but I could be wrong. perl -d wasn't very helpful.
Thanks.

It's failing to create the socket, use some error checking to try to find out why. Eg:
my $sock = IO::Socket::Multicast6->new(
Proto=>"udp",
Domain=>AF_INET,
PeerAddr=>GROUP,
PeerPort=>PORT)
or die "Socket failed: $!";

The new() constructor is failing, but not raising an exception. I don’t know its API: is there some way to get it to tell you why?
Otherwise you might try errno (that is, $!).

Related

Unable to get output from remote Device using Net::SSH2

I am trying to connect to a remote device through Net::SSH2. If I am using $channel->blocking(0) method, then I am not getting any output from the device even if the connection and authentication is successful. If I dont use $channel->blocking(0), then I am getting the output but the prompt keeps on blinking, in the sense the the program gets stuck.
Code
use Net::SSH2;
use strict;
$ssh = Net::SSH2->new()
$ssh->connect('xx.xx.xx.xx'))
$ssh->auth(username => 'xxxx', password => 'xxxx'))
my $channel = $ssh->channel() or do { print" [LOG ERROR]: Failed to create channel. Exiting ...\n"};
$channel->blocking(0);
$channel->shell() ;
print $channel "show status \n";
while(<$channel>)
{
print $_;
}
$channel->close();
$ssh->disconnect();
can somebody please help.
Maybe debug could help you by troubleshooting. If there is no particular reason why you are using shell try to replace it by exec. You could adopt this example.
use warnings is also always useful.

[Perl][net::ssh2] How to keep the ssh connection while executing remote command

I'm working on a perl script using net::ssh2 to make a SSH connection to a remote server.
(I'm working on windows)
I chose Net::SSH2 because i had to make some SFTP connections in the same script.
For now, my sftp connections work perfectly. The problem is when i try to execute a "long-duration" command. I mean a command which execution can take more than 30sec.
$ssh2 = Net::SSH2->new();
$ssh2->connect('HOST') or die;
if($ssh2->auth(username=>'USER', password=>'PSWD'))
{
$sftp = Net::SFTP::Foreign->new(ssh2=>$ssh2, backend=>'Net_SSH2');
$sftp->put('local_path', 'remote_path');
$channel=$ssh2->channel();
##
$channel->shell('BCP_COMMAND_OR_OTHER_PERL_SCRIPT');
# OR (I tried both, both failed :( )
$channel->exec('BCP_COMMAND_OR_OTHER_PERL_SCRIPT');
##
$channel->wait_closed();
$channel->close();
print "End of command";
$sftp_disconnect();
}
$ssh2->disconnect();
When i execute this script, the connection is successfull, the file is correctly sent but the execution is not (completely) performed. I mean, I think the command is sent for execution but terminated immediatly or not sent at all, i'm not sure.
What i want is the script waits until the command is completly finished before disconnect everything (just because sometimes, i need to get the result of the command execution)
Does anyone know how to solve this? :( The cpan documentation is not very explicit for this
Thanks!
PS: I'm open to any remarks or suggestion :)
Edit: After some test, i can say that the command is sent but is interrupted. My test was to start another perl script on the remote server. This script writes in a flat file. In this test, the script is started, the file is half-filled. I mean, the file is brutaly stopped in the middle.
In the other hand, when i performed a "sleep(10)" just after the "$channel->exec()", the script goes to the end successfully.
Problem is, that I can't write a "sleep(10)" (i don't know if it will take 9 or 11 seconds (or more, you see my point)
You can try using Net::SSH::Any instead.
It provides a higher level and easier to use API and can use Net::SSH2 or Net::OpenSSH to handle the SSH connection.
For instance:
use Net::SSH::Any;
my $ssh = Net::SSH::Any->new($host, user => $user, password => $password);
$ssh->error and die $ssh->error;
my $sftp = $ssh->sftp;
$sftp->put('local_path', 'remote_path');
my $output = $ssh->capture($cmd);
print "command $cmd output:\n$output\n\n";
$sftp->put('local_path1', 'remote_path1');
# no need to explicitly disconnect, connections will be closed when
# both $sftp and $ssh go out of scope.
Note that SFTP support (via Net::SFTP::Foreign) has been added on version 0.03 that I have just uploaded to CPAN.

PERL SFTP using System, Cannot install SFTP module

I'm a bit new to perl and stackoverflow. If I could use a more familiar language I would, unfortunately I cannot due to certain circumstances. Thanks in advance for the help.
Modules Not Installed: Net::SFTP, WWW::CURL, Net::SSH2, Net::SFTP::Foriegn
Modules Installed: Net::FTP
I am unable to install modules.
Unable to use Net::FTP Tried Default port and port 22, with a username and password. All I get back from the other box's log when trying to connect is "Did not receive identification string from xx.xx.xx.xx" Also unable to use FTP in command line, times out.
$ftp = Net::FTP->new($box,Port=>22, Debug => 0)
or die print "Error: Cannot connect";
$ftp->login($userBox,$passBox)
or die print "Error: Cannot login";
$ftp->cwd()
or die print "Error: Cannot change to Root";
$ftp->cwd($dir)
or die print "Error: Cannot change to selected directory";
if($copyfile ne "" && $dir ne "")
{
$ftp->put($copyfile, $copyfile);
}
$ftp->quit();
I can manually use SFTP through the linux command line, not FTP, so I have been trying to use the system command to SFTP into the other box. The other box's logs just say "Connection closed by xx.xx.xx.xx"
system('sftp '.$userBox.'#'.$box.' ENDOFINPUT'
.$passBox.'ENDOFINPUT
cd ../../../
put '.$filename.' '.$dir.'
exit
ENDOFINPUT');
If anyone knows how to help me with my problem that'd be great :)
Let's approach this from another direction... when you say you're "unable to install modules", is that just because you don't have root permission? If that's the case, you can install them locally under a user account instead.
If the machine doesn't have an internet connection to even install them locally, you can use the same technique to install them on a different box, then gzip the entire local directory where you have them installed and copy them to the target machine, and add a "use lib" statement to get at them from your script.
Are you sure that the $passBox equivalent worked on the shell?
You should be able to set-up passwordless connectivity using key-pairs, which would make THAT problem go away quite quickly.

Perl Net::FTP object expected re-use behavior?

I can't seem to re-use a Net::FTP object after using quit.
Is this expected? I was unable to tell from the documentation (cpan).
As a workaround, I'm creating a new Net::FTP object each time I need to perform a batch of ftp operations. This seems wasteful.
The following example shows: successful initial login, printing of root directory ls, quit (socket close), login failing with ftp message "Connection closed".
#!/usr/bin/env perl
use strict;
use warnings;
use Net::FTP;
my $hostname = 'foo';
my $username = 'bar';
my $password = 'baz';
# successful first pass
my $ftp = Net::FTP->new( $hostname ) or die "cannot connect to $hostname: $#";
$ftp->login( $username, $password ) or die "cannot login: ", $ftp->message;
map { print "ls_output: $_\n" } $ftp->ls; # success
$ftp->quit or die "cannot close connection: ", $ftp->message;
# re-use attempt
$ftp->login( $username, $password ) or die "cannot login: ", $ftp->message;
# never gets here since re-use attempt fails
print "done!\n";
quit causes the remote end to close the connection and there's no way to logout without closing the connection. If you're trying to avoid reconnecting, you can't.
On the other hand, maybe you're expecting login to connect you to the server. The connection is created in new, not in login, and Net::FTP does not provide a means of reconnecting.
Net::FTP subclasses IO::Socket::INET, so you could reconnect by using IO::Socket::INET's connect, but you'd also have to reinitialise a field or two the constructor normally initialises. Nothing complicated though.
But is there even a problem that needs fixing? You talk of inefficiencies, but the time it takes to create and initialise an object pales in comparison it takes to make an FTP connection.
It's not a Perl issue. It's the FTP protocol. After the quit is issued... that's it, the FTP session is over. There's nothing to issue the login command to -- no-one's listening anymore.
Try it yourself on the command line with the FTP client.
From this very documentation you linked:
quit ()
Send the QUIT command to the remote FTP server and close the socket connection.
Closing the socket connection terminates the connection to the server, not just the session on this server.
Creating a new object for each connection has two drawbacks:
Perl may or may not keep the data from the old object around in memory—but garbage collection should not bother you.
Creating a new connection involves some overhead. But unless you are creating dozens of new connections per second, this should not bother you.
So just create new objects every time; you can reuse the same variable if you like. Thinking too low level in Perl, and optimizing too early is only going to hurt.

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.