Sunday, October 6, 2013

Junos OP script ping a range of IP addresses


I wanted to ping some devices in my lab to see which ones were reachable, but there wasn't a range command in Junos. So I decided to build one myself. I took a similar ping-sweep script and modified it for my purposes.

{master:0}
user@router# set system scripts op file ping.slax

{master:0}
user@router> op ping ?
Possible completions:
  <[Enter]>            Execute this command
  <name>               Argument name
  detail               Display detailed output
  first                starting ip of last octet - default 1
  last                 last ip of last octet - default 20
  remote-host          first 3 octests of an ip-address to ping eg 10.1.1.
  step                 step size - default 1
  |                    Pipe through a command
{master:0}
user@router> op ping remote-host 192.168.32. first 20 last 30        
Pinging 192.168.32.20
Ping Success!!!
Pinging 192.168.32.21
Ping Failed!!!
Pinging 192.168.32.22
Ping Success!!!
Pinging 192.168.32.23
Ping Success!!!
Pinging 192.168.32.24
Ping Success!!!
Pinging 192.168.32.25
Ping Success!!!
Pinging 192.168.32.26
Ping Success!!!
Pinging 192.168.32.27
Ping Failed!!!
Pinging 192.168.32.28
Ping Failed!!!
Pinging 192.168.32.29
Ping Failed!!!
Pinging 192.168.32.30
Ping Failed!!!

{master:0}
user@router> show arp no-resolve
MAC Address       Address         Interface     Flags
00:a0:a5:7b:ba:1a 192.168.32.20    ge-0/0/0.0                none
40:b4:f0:7a:bc:3d 192.168.32.22    ge-0/0/0.0                none
40:b4:f0:79:50:fd 192.168.32.23    ge-0/0/0.0                none
40:b4:f0:79:59:3d 192.168.32.24    ge-0/0/0.0                none
40:b4:f0:79:46:bd 192.168.32.25    ge-0/0/0.0                none
84:18:88:d1:9d:7d 192.168.32.26    ge-0/0/0.0                none
Total entries: 6

Here's the source code:
------------------

version 1.0;

/*  This OP script will ping a range of ips up to a class C subnet
*/


ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns ext = "http://xmlsoft.org/XSLT/namespace";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";

import "../import/junos.xsl";


var $arguments = {
    <argument> {
        <name> "remote-host";
        <description> "first 3 octests of an ip-address to ping eg 10.1.1.";
    }
    <argument> {
        <name> "first";
        <description> "starting ip of last octet - default 1";
    }
    <argument> {
        <name> "last";
        <description> "last ip of last octet - default 20";
    }
    <argument> {
        <name> "step";
        <description> "step size - default 1";
    }
}


param $remote-host = "192.168.32.";
param $first = '1';
param $last = '20';
param $step = '1';

match / {

    call ping($remote-host, $cur-size = $first, $last, $step);

}

/*
 * Ping the remote-host with different packet size and display the result
 */
template ping ($remote-host, $cur-size, $last, $step) {

        var $ip = $remote-host _ $cur-size;

    if ($cur-size <= $last) {
        var $ping-rpc = {
            <ping> {
                <host> $ip;
                <count> 1;
                <rapid>;
            }
        }

        expr jcs:output("Pinging ", $ip);

        /*
         * Invoking ping RPC and getting the result into $ping-out
         */
        var $ping-out = jcs:invoke($ping-rpc);

        if ($ping-out/ping-success) {
            expr jcs:output("Ping Success!!!");
        } else {
            expr jcs:output("Ping Failed!!!");
        }

        call ping($remote-host, $cur-size = $cur-size + $step, $last,
                  $step);
    }
}


No comments:

Post a Comment