Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

help to deal with ssh connection

by mitchreward (Acolyte)
on May 18, 2014 at 14:44 UTC ( [id://1086509]=perlquestion: print w/replies, xml ) Need Help??

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

hi guys,

I'm new in perl language, and my boss the senior sys-admin asked me to write a perl script. This script have to ssh a specific host, run a bash script, and get the ouptut. to print it in a CGI page (but that's another thing)

The Net::SSH module is not installed on the host, and I'd like to deal with file handle as much as possible. If it's too complicated, I'lle install the module.

right now am on my own pc (debian) and getting the following error, even with the libnet-ssh-perl installed.

Can't locate Net/SSH/Perl.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at ssh.pl line 3.

this is the script :

#!/usr/bin/perl use Net::SSH::Perl; use strict; use Net::OpenSSH; my $host = "192.168.0.24"; my $ssh = Net::OpenSSH->new($host); $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error; #$ssh->system("ls /tmp") or # die "remote command failed: " . $ssh->error;

Could you guys give me some help to start ? I'm reading a perl book, but am really short in time..

Replies are listed 'Best First'.
Re: help to deal with ssh connection
by vinoth.ree (Monsignor) on May 18, 2014 at 17:21 UTC

    You can execute commands on remote host using a Perl script using the Net::SSH::Perl module.

    This module allows you to execute a command remotely and receive the STDOUT, STDERR, and exit status of that remote host command.

    One big advantage of Net::SSH::Perl over other methods is that you can automate the login process, that way you can write fully automated perl scripts, no console interaction is required in order to authenticate in the remote machine.

    #!/usr/bin/perl use strict; use warnings; use Net::SSH::Perl; use Data::Dumper; my $Host = "xxxxxx"; my $User = "xxxxxx"; my $Secret = "xxxxxx"; my $Path="/home/ree"; #Connect my $ssh = Net::SSH::Perl->new($Host); $ssh->login($User, $Secret); # Execute the command my($stdout, $stderr, $exit) = $ssh->cmd("ls -l $Path");

    But if you use Net::OpenSSH module, check useful info from this node Problems with Net::OpenSSH


    All is well

      thanks for your answer, but I'm using net::ssh (not net::ssh::perl neither net::openssh). Is it possible to authenticate with public key and the net::ssh module ?

        Before we go to ssh with public key authentication, correct the typo in your post Re^2: help to deal with ssh connection, its Net::SSH not set:ssh

        You can get the doc for Net::SSH here Net::SSH

        In that doc its been explained how to generate public key and copy that into your remote machine. If you have done those steps, post us what error your script report.


        All is well
Re: help to deal with ssh connection
by Anonymous Monk on May 18, 2014 at 15:05 UTC

    I don't think you need use Net::SSH::Perl; since you don't seem to be using it in your script, what happens when you remove that line?

    The Debian package libnet-ssh-perl actually contains only the Perl module Net::SSH. I haven't yet found the Debian package for Net::SSH::Perl, you may need to install it via cpan if you want it, but I'd recommend Net::OpenSSH (and you're already using that module in your script anyway).

    On Ubuntu 12.10 or Debian wheezy and upwards you can get Net::OpenSSH via the package libnet-openssh-perl (and libio-pty-perl for password auth).

    If your sysadmin boss wants (you) to use Perl, then he'll have to deal with installing a few modules along the way.

      thanks guys, that helped me. I finnaly installed Net::SSH and I did something that is not far for working at the moment. I just got a problem with the auth with public key:

      #!/usr/bin/perl use Net::SSH qw(sshopen2); use CGI qw(:standard); use strict; use utf8; print header(); if (param("email")) { my $email = param("email"); my $date = param("date"); print <<ENDHTML; <html> <body> <h1> my script </h1> <br><br> <font color="red"> please type the date in English format, i.e : 1 +2-Apr-2014 </font><br><br> <form action="index.cgi" method="post"> <INPUT type="text" name="date" id="date"><BR><BR> <INPUT type="hidden" name="email" value="$email"> <INPUT type="submit" value="Send"> </form> ENDHTML my $user = "**user**"; my $host = "**host**"; my $cmd = "maili $email del $date"; sshopen2("$user\@$host", *READER, *WRITER, "$cmd") || die "ssh: $!"; while (<READER>) { chomp(); print "$_\n"; } close(READER); close(WRITER); }
      How can I specify to use a public key for ssh authentication? I can't get the doc, because http://search.cpan.org seems to be down at the moment.
        You can read the documentation for any module installed on your machine using the command perldoc. E.g.: perldoc Net::SSH.

        Even if perldoc is not installed (on some distribution it is packed as a separate tool), you can read the raw pod just looking at the .pm file.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1086509]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2025-07-19 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.