Perl Openssh



  1. Perl Openssh Capture2
  2. Perl Openssh Cisco
  3. Ssh Terminal Commands
mod_perl and mod_perl2
mod_perl and mod_perl2 tie STDIN and STDOUT to objects that are not backed up by real file descriptors at the operative system level. Net::OpenSSH will fail if any of these handles is used explicetly or implicitly when calling some remote command.
The workaround is to redirect them to /dev/null or to some file:
open my $def_in, '<', '/dev/null' or die 'unable to open /dev/null';
my $ssh = Net::OpenSSH->new($host,
default_stdin_fh => $def_in);

my $out = $ssh->capture($cmd1);
$ssh->system({stdout_discard => 1}, $cmd2);
$ssh->system({stdout_to_file => '/tmp/output'}, $cmd3);
Also, note that from a security stand point, running ssh from inside the webserver process is not a great idea. An attacker exploiting some Apache bug would be able to access the ssh keys and passwords and gain unlimited access to the remote systems.
If you can, use a queue (as TheSchwartz) or any other mechanism to execute the ssh commands from another process running under a different user account.
At a minimum, ensure that ~www-data/.ssh (or similar) is not accessible through the web server!

What is the method to install Perl Net::OpenSSH Module? Need Perl Net SSH module; Environment. Red Hat Enterprise Linux(RHEL) 5; Perl; Subscriber exclusive content. A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles. Provided by: libnet-openssh-perl0.74-1all NAME Net::OpenSSH - Perl SSH client package implemented on top of OpenSSH SYNOPSIS use Net::OpenSSH; my $ssh = Net. You have searched for packages that names contain openssh in all suites, all sections, and all architectures. Found 20 matching packages. Package libconfig-model-openssh-perl. Xenial (16.04LTS) (perl): configuration editor for OpenSsh universe 1.237-1: all bionic (18.04LTS) (perl): configuration editor for OpenSsh universe. Questions: Can anyone suggest a a very simple way to use Net::SSH::Perl in Windows without all those cygwin hacks and all. Whenever I am trying to install the modules it’s taking me to some other dependent modules and the process seemed never ending. Thanks beforehand Answers: Here is.

mlebel has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have been having problems with NET::SSH::Expect lately and I have always found it generally slow and buggy.

Upon doing some research, I found Net::OpenSSH. It appears to be working really fast and be reliable.

Perl Openssh

The only thing though is that I am having a problem running multiple commands within the same session(logging onto the device only once and run all the commands). Unless I mis-understood the documentation provided by Salvador Fandiño, I have the following code which log's on (into a Cisco router by the way) and displays the login banner and then returns to my linux prompt.

Perl Openssh

Perl Openssh Capture2

I hope there is a good solution. I really want to move away from Net::SSH::Expect...

Perl Openssh Cisco

  • Comment on Net::OpenSSH multiple commands
  • Download Code
Replies are listed 'Best First'.
Re: Net::OpenSSH multiple commands
by salva (Canon) on Apr 19, 2012 at 21:06 UTC
    Ideally, you should be able to run commands sequentially:

    Unfortunately, some devices do not support this mode of operation and force you to talk to some kind of custom shell.

    In that case, in order to start a remote shell from Net::OpenSSH, call open2pty without passing any command:

    ...and then, use Expect to talk to the shell.

    Sometimes the following approach also works:

    See also the FAQ section from the module docs.

[reply]
[d/l]
[select]
Re: Net::OpenSSH multiple commands
by zentara (Archbishop) on Apr 19, 2012 at 19:27 UTC
    I hope there is a good solution. I really want to move away from Net::SSH::Expect...

    It's not Net::OpenSSH, but Net::SSH2 can do multiple commands thru it's channel's shell() method. Try it, if you can't find a Net::OpenSSH solution. Keys Auth logins is also supported.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
[reply]
[d/l]
Re: Net::OpenSSH multiple commands
by mlebel (Hermit) on Apr 20, 2012 at 19:21 UTC

    Thank you zentara and salva for your answers.

    Unfortunately, after messing with this all day, I wasn't able to get anything working from either one your posts. My thought now is to post the Net::SSH:Expect code that I've been using and I am trying to convert to Net::OpenSSH. Maybe this will help identify what I am trying to achieve, and the reasons behind it. So, here it is...

    Note that this code is using a username and password. If possible, I would rather use public key authentication.

    Is this possible using the Net::OpenSSH Module (with maybe expect)? If so, how could I write it?

    Thanks

[reply]
[d/l]
Re: Net::OpenSSH multiple commands
by mlebel (Hermit) on Apr 23, 2012 at 17:09 UTC

    After a few more days of messing with this, I have gotten the following code to work.

    This code, however, gives me 2 errors...

    I have tried searching for a fix for them but I haven't found anything.

    Would anyone know how I can fix these errors?

[reply]
[d/l]
[select]
    Re^2: Net::OpenSSH multiple commands
    by salva (Canon) on Apr 24, 2012 at 09:31 UTC
    The line ... ... does mostly nothing, delete it.
    This code, however, gives me 2 errors

    Those are probably harmless, generated by OpenSSH because you are requesting a remote pty but attaching it to a non-pty stream.

    BTW, when writing follow ups to previous comments, do not reply to main question but to those, otherwise the person you are responding (i.e. me) will probably miss the reply.

[reply]
[d/l]
    Re^3: Net::OpenSSH multiple commands
    by mlebel (Hermit) on Apr 24, 2012 at 11:32 UTC

    I removed the line:(for which I just realized that I had forgot to change 'show ip arp')

    and the results were that the 'channel 4: open failed: resource shortage:' error went away. As for the remaining error, I guess i'll just filter it out since it's probably harmless as you said.

    I appreciate the tip on writing follow ups, I never realized that it could cause that. Now I have a much more solid solution compared to using the Net::SSH::Expect module. For those interested, I cleaned up my code:...

    Thank you Salva

[reply]
[d/l]
[select]
    Re^3: Net::OpenSSH multiple commands
    by mlebel (Hermit) on Apr 24, 2012 at 19:10 UTC

    Hi Salva, I believe you are probably the best person to answer this:... I changed the line:

    to this:

    and I get an error (I think might be caused by openssh) that says 'Bad packet length 688328622.' (The length numbers change every time I run the code). If i remove some parts of the commands such as 'terminal length 0' and run it again, the error goes away. The same goes if I leave 'terminal length 0' in there and replace say 'show ip accounting' by 'show ip route'.(no error)

    So it seems that a combination of commands will trigger this error. I do have to say though that the output is ok regardless of the error. Would this be another error which I should Ignore? or could this be a bug?

    Thanks

[reply]
[d/l]
[select]
    Re^4: Net::OpenSSH multiple commands
    by salva (Canon) on Apr 25, 2012 at 09:22 UTC
    Re^5: Net::OpenSSH multiple commands
    by mlebel (Hermit) on Apr 25, 2012 at 11:36 UTC
    Some notes below your chosen depth have not been shown here
Re: Net::OpenSSH multiple commands
by mlebel (Hermit) on May 24, 2012 at 15:27 UTC

    The solution that ended up working for me was to go back to using Net::SSH::Expect and remove the username/password login and make it use public-key authentication. So far (knock on wood) this seems to work..it's no longer slow and it seems to login everytime..I can also run multiple commands and get the results expected.. Here is a sample of code I used to make this work. (taken from it's documentation)

    I hope this helps someone.

    Also, if there is a way to do it using the Net::OpenSSH module, a sample code would be appreciated. Otherwise, enjoy this working solution

    Marc

[reply]
[d/l]
Re: Net::OpenSSH multiple commands
by Anonymous Monk on Apr 07, 2013 at 19:04 UTC
    I don't know who this might help, but here is a simple way to join multiple commands and run them at once on a remote server. It just connects to the server, finds the directory you want, and prints its total contents. --Ben.
[reply]
[d/l]
    Re^2: Net::OpenSSH multiple commands
    by salva (Canon) on Apr 07, 2013 at 20:06 UTC
    It is recommendable to let Net::OpenSSH quote the command arguments. Double references can be used to let some fragment pass unquoted: Otherwise, in case $path_to_file contained shell metacharacters it could produce undesired results or even be a security hole.

    Note that currently, argument quoting expect some derivative of the Bourne shell on the remote side. Support for quoting arguments for other shells is in the works though.

[reply]
[d/l]
[select]

Ssh Terminal Commands