Skip to content
Guides & References

Write an SIE script in Perl

How to write an SIE script in Perl with Net::OpenSSH: installing the modules from OS packages, the command line and a minimal collection script.

Script Integration EngineUpdated
On this page

Write an SIE script in Perl

Perl is still the language of a lot of network tooling written over the years. If your team has Perl scripts that already collect configs, SIE can run them with small changes. For new scripts, Python is usually the better long-term choice.


Good for

  • Bringing existing Perl collection scripts into rConfig.
  • Heavy text handling with regular expressions.

Set it up

Install modules from your OS packages rather than CPAN, so OS updates keep them current. Net::OpenSSH runs the system’s own ssh; IO::Pty is needed for password logins.

Terminal window
sudo dnf install perl-Net-OpenSSH perl-IO-Tty # RHEL family (EPEL)
sudo apt install libnet-openssh-perl libio-pty-perl # Ubuntu

The command

/usr/bin/perl {script_path}my-script.pl {device_ip} {device_username} {device_password}

A minimal script

perl-skeleton.pl
#!/usr/bin/perl
# Minimal SIE script in Perl: run one command over SSH with Net::OpenSSH.
#
# Usage: perl-skeleton.pl <ip> <username> <password>
use strict;
use warnings;
use Net::OpenSSH;
my $KEYS = '/var/www/html/rconfig8/current/storage/app/rconfig/scripts/keys';
my $CONFIG_COMMAND = 'show running-config';
sub note { print STDERR "$_[0]\n" }
sub fail { print STDERR "ERROR: $_[0]\n"; exit 1 }
my ($host, $user, $password) = @ARGV;
fail('usage: perl-skeleton.pl <ip> <username> <password>') unless defined $password;
note("connecting to $host as $user");
my $ssh = Net::OpenSSH->new(
$host,
user => $user,
password => $password,
timeout => 30,
master_opts => [
-o => 'StrictHostKeyChecking=accept-new',
-o => "UserKnownHostsFile=$KEYS/known_hosts",
],
);
fail("cannot connect to $host: " . $ssh->error) if $ssh->error;
my $config = $ssh->capture($CONFIG_COMMAND);
fail("'$CONFIG_COMMAND' failed on $host: " . $ssh->error) if $ssh->error;
$config =~ s/\r//g;
fail("no output from $host") unless $config =~ /\S/;
print $config;
print "\n" unless $config =~ /\n\z/;
my $lines = () = $config =~ /\n/g;
note("SIE-OK: perl $lines lines from $host");
exit 0;

Habits that matter in Perl

  • print STDERR for everything except the config. Plain print goes to stdout and is stored.
  • use strict; use warnings;. Warnings go to stderr, so they don’t spoil the stored config, and they catch mistakes early.
  • Check $ssh->error after every call. Net::OpenSSH doesn’t die on failure; it records the error and carries on.

Type to search.