• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Urban Penguin

The Urban Penguin - Linux Training

  • Home
  • About
  • Programming
    • Master Editing Text Files Using VIM
    • Learn Shell Scripting with BASH
    • PERL Scripting in Linux
    • Ruby Scripting in Linux
    • Scripting with PowerShell
    • Learn C Programming using Linux and the Raspberry Pi
    • General Java Tutorials
    • Java 7 OCA Exam 1ZO-803
  • OS Tutorials
    • Red Hat and CentOS Training
      • Red Hat Enterprise Linux System Administration 1 – RH124
      • RHCSA – System Admin 2 – RH134
      • RHCE – EX294 – Automation With Ansible
    • Learning Ubuntu
    • LPI Training
      • LPI Linux Essentials
      • LPIC-1 Linux Administrator
      • LPIC-2 Certified Linux Engineer
      • LPIC-3 Senior Level Certification
        • LPIC-3 Exam 300 : Mixed Environments
        • LPIC-3 Exam 303 : Security
        • LPIC-3 Exam 304 : Virtualization and High Availability
    • Linux Technologies
      • Apache HTTPD Server
      • Learning PHP
      • Learning PUPPET
      • Learning SAMBA
      • Linux File-Systems
      • Monitoring with Nagios Core
      • MYSQL
      • openLDAP Directories on Linux
You are here: Home / Scripting / Perl / SSH Connections using PERL and Net::OpenSSH

SSH Connections using PERL and Net::OpenSSH

June 25, 2017 by The Urban Penguin

SSH Connections using PERLIn this module we look at a common system administration task and the need to run commands across many servers. Sure we could do this with Puppet or a similar configuration management tool. Checkout my Puppet videos on Pluralsight. But if our needs are more ill-defined or irregular then making SSH connection with PERL and Net::OpenSSH may be a a great option. Using the Net::OpenSSH module in PERL allows an easy connection to our servers. If we have many servers we can iterate a list of servers with the foreach structure. We use this in the demo. Similarly if we need to run many commands across the SSH Connections using PERL we can iterate through a list of commands.

First, we need to check the module is installed. We are using Ubuntu so we can use the prebuilt package provided:

$ sudo apt-get update; sudo apt-get install -y libnet-openssh-perl

In Red Hat and CentOS this can be obtained from the EPEL repo and the package perl-Net-OpenSSH. Modules can also be installed using cpan if you have it and the rest of the d eloper tools installed.

The code we create in the video is shown below.  We iterate through two demonstration servers alice and bob. On each server, we run the specified command. In out case, just running uname -r.

#!/usr/bin/perl
use strict;
use warnings;
use Net::OpenSSH;
my ($s);
my $username = 'ubuntu';
my $cmd = 'uname -r';
my @servers = ('alice', 'bob');
foreach $s (@servers) {
 my $ssh = Net::OpenSSH->new("$username\@$s",timeout => 30);
 $ssh->error and die "Unable to connect: " + $ssh->error;
 print "Connected to $s\n";
 my $fh = $ssh->pipe_out($cmd) or die "Unable to run command";
 while (<$fh>) {
   print "$s OUT: $_";
 }
 close $fh;
 undef $ssh;
}

 

We don’t provide a password as we use  SSH-Keys for authentication.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to print (Opens in new window)

Filed Under: Perl

Primary Sidebar

Newest Video

The Urban Penguin On Youtube

Categories

Pages

  • About The Urban Penguin
  • Contact Us
  • Linux Technologies
    • Apache HTTPD Server
    • Learning PHP
    • Learning PUPPET
    • Learning SAMBA
    • Linux File-Systems
    • Monitoring with Nagios Core
    • MYSQL
    • openLDAP Directories on Linux
  • LPI Training from The Urban Penguin
    • Complete Linux Essentials
    • Live and Pluralsight hosted courses
    • LPI Linux Essentials
    • LPI Linux Essentials for Raspberry Pi
    • LPIC-1 Linux Administrator
    • LPIC-2 Certified Linux Engineer
    • LPIC-3 Senior Level Certification
      • LPIC-3 Exam 300 : Mixed Environments
      • LPIC-3 Exam 303 : Security
      • LPIC-3 Exam 304 : Virtualization and High Availability
  • Online Instructor-led Courses
    • Bash Scripting Masterclass
    • Nftables Masterclass
    • Red Hat Enterprise Linux System Administration 1 – RH124
    • SELinux Masterclass
  • OpenStack
    • Citrix Videos
    • Pluralsight
    • Raspberry Pi Tutorials
    • Udemy
  • Operating System Tutorials
    • Learning SUSE
    • Learning Ubuntu
    • Linux Foundation Training
    • Red Hat and CentOS Training
      • RHCE – EX294 – Automation With Ansible
      • RHCSA – System Admin 1 – RH124
      • RHCSA – System Admin 2 – RH134
    • Solaris 11 OCA 1ZO-821
  • Scripting – the power of repetition!
    • General Java Tutorials
    • Java 7 OCA Exam 1ZO-803
    • Learn C Programming using Linux and the Raspberry Pi
    • Learn Shell Scripting with BASH
    • Master Editing Text Files Using VIM
    • PERL Scripting in Linux
    • Ruby Scripting in Linux
    • Scripting with PowerShell

© 2022 The Urban Penguin · All Rights Reserved