File: example-1.pl

package info (click to toggle)
libnet-appliance-session-perl 4.142720-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 224 kB
  • ctags: 29
  • sloc: perl: 1,034; makefile: 8
file content (62 lines) | stat: -rw-r--r-- 1,670 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/perl -n

# This (fictional) example shows how you can save time if there are many
# devices that require manual login reconfiguration (e.g. no SNMP).
# 
# The program is a filter, so wants a list of hosts on standard input or a
# filename containing hosts as an argument, and will go through each one,
# connecting to and reconfiguring the device.

BEGIN {
    use strict;
    use warnings FATAL => 'all';

    use Net::Appliance::Session;
}

my $host = $_; chomp $host;
die "one and only param is a device FQDN or IP!\n"
    if ! defined $host;

my $s = Net::Appliance::Session->new({
    transport => 'SSH', # or 'Telnet' or 'Serial'
    personality => 'ios', # or many others, see docs
    host => $host,
});
$s->set_global_log_at('notice'); # maximum debugging is 'debug'

try {
    $s->connect({
        name     => $username,
        password => $password,
    });
    $s->begin_privileged; # use same pass as login

    # is this a device with FastEthernet or GigabitEthernet ports?
    # let's do a test and find out, for use in the later commands.

    my $type = $s->cmd('show interfaces status | incl 1/0/24');
    $type = ($type =~ m/^Gi/ ? 'GigabitEthernet' : 'FastEthernet');

    # now actually do some work...
    # (lines which make changes are commented in this example!)

    $s->begin_configure;

    $s->cmd("interface ${type}1/0/13");
    # $s->cmd('no shutdown');
    $s->cmd("interface ${type}1/0/14");
    # $s->cmd('no shutdown');
    $s->cmd("interface ${type}1/0/15");
    # $s->cmd('no shutdown');

    $s->end_configure;
    # $s->cmd('write memory');
    $s->end_privileged;
}
catch {
    warn $_;
}
finally {
    $s->close;
};