Sunday 4 December 2016

Installing & using the openssh module in perl


I will start this article by saying that I'm failry new to perl & have recently begun my quest to become proficient in perl scripting for automating some of my system administration related tasks. Anyone whose familiar with perl would be aware that perls' flexibility stems from it's enormous commnuity base & modules with CPAN comprising of over 10,000 packages. That's a lot. In this article, I dwelve into one such module which is extremely useful for us system administrators & that's the Net::OpenSSH module.

Perl is installed in most UNIX/Linux based operating systems. I'll be using Ubuntu 16.04 for the tasks being performed.

Let's start by confirming that we in fact have perl & perldoc installed.

root@buntu:~# dpkg-query -l perl*
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                        Version            Architecture       Description
+++-===========================-==================-==================-============================================================
ii  perl                        5.22.1-9           amd64              Larry Wall's Practical Extraction and Report Language
ii  perl-base                   5.22.1-9           amd64              minimal Perl system
un  perl-cross-config           <none>             <none>             (no description available)
ii  perl-doc                    5.22.1-9           all                Perl documentation
un  perl-modules                <none>             <none>             (no description available)
ii  perl-modules-5.22           5.22.1-9           all                Core Perl modules
un  perlapi-5.22.1              <none>             <none>             (no description available)

Now let's install cpanminus, which is basically a script that allows us to communicate with CPAN & install packages/modules from there.

curl -L http://cpanmin.us | perl - --sudo App::cpanminus

Let's confirm that it has been installed correctly.

root@buntu:~# dpkg-query -l cpan*
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                        Version            Architecture       Description
+++-===========================-==================-==================-============================================================
ii  cpanminus                   1.7040-1           all                script to get, unpack, build and install modules from CPAN
root@buntu:~#

I installed the openssh module via apt-get from the default internet repositories made available post installtion.

root@buntu:~# apt-get install libnet-openssh-perl -y
We can check list of installed module via the cpan -l command.

root@buntu:~# cpan -l | grep -i openssh
Net::OpenSSH    0.70
Net::OpenSSH::Constants 0.51_07
Net::OpenSSH::OSTracer  0.65_06
Net::OpenSSH::ShellQuoter       undef
Net::OpenSSH::ConnectionCache   undef
Net::OpenSSH::ModuleLoader      undef
Net::OpenSSH::ObjectRemote      undef
Net::OpenSSH::SSH       undef
Net::OpenSSH::ShellQuoter::Chain        undef
Net::OpenSSH::ShellQuoter::POSIX        undef
Net::OpenSSH::ShellQuoter::MSWin        undef
Net::OpenSSH::ShellQuoter::MSCmd        undef
Net::OpenSSH::ShellQuoter::csh  undef
Net::OpenSSH::ShellQuoter::fish undef


I used the cpan shell to download & install one of the dependent modules IO::Pty

root@buntu:~# cpan
Terminal does not support AddHistory.
cpan shell -- CPAN exploration and modules installation (v2.11)
Enter 'h' for help.
cpan[1]> install IO::Pty
Reading '/root/.cpan/Metadata'
  Database was generated on Sun, 04 Dec 2016 05:29:02 GMT
Running install for module 'IO::Pty'
Checksum for /root/.cpan/sources/authors/id/T/TO/TODDR/IO-Tty-1.12.tar.gz ok
Scanning cache /root/.cpan/build for sizes
............................................................................DONE

The cpan shell will install any dependent modules for the module being installed. Note that we do need make & gcc to be installed because under the hood module installation via CPAN downloads the tar.gz files for the modules & compiles & builds them to make them available for use.

Given below is the first program I wrote using the Net::Openssh modules:

root@buntu:~# cat test7.pl
#!/usr/bin/perl -w

use Net::OpenSSH;

my $cmd1 = " uptime ";
my $cmd2 = "uptime";

my $hostname = "buntu";
my $username = "sa";
my $password = "123";
my $timeout  = 20;

my $ssh = Net::OpenSSH->new(
    $hostname,
    user        => $username,
    password    => $password,
    timeout     => $timeout,
    master_opts => [ -o => "StrictHostKeyChecking=no" ]
);
$ssh->error and die "Unable to connect to remote host: " . $ssh->error;

my ( $fh, $pid ) = $ssh->open2pty( { stderr_to_stdout => 1 } );

 $ssh->system("ls /tmp") or
    die "remote command failed: " . $ssh->error;


It's failry simple. It logs in to host name buntu with the supplied credentials & uses the system function to run the "ls /tmp" command & display its output.

As I progress in my learning, I'll definitely try to post more articles on perl scripting for system administrration.

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...