Junos OS

 View Only
last person joined: yesterday 

Ask questions and share experiences about Junos OS.
  • 1.  [OPS Script] Extract multiple show commands (e,.g: show interfaces terse | no-more, show version ) and save the output to a file.txt

    Posted 30 days ago

    Hi experts,

    Would like to ask your favors if you already have a simple op script that will execute a series of show commands and save the output to a txt file in the Junos device.

    Instead of doing the manual show commands, I will utlize an op script that will execute the show commands.

    e.g:

    show version

    show interfaces | terse | no-more

    show chassis hardware | no-more

    and so on..

    Thank you.

    Leangf



    ------------------------------
    ANGFE LANDAGAN
    ------------------------------


  • 2.  RE: [OPS Script] Extract multiple show commands (e,.g: show interfaces terse | no-more, show version ) and save the output to a file.txt

    Posted 14 days ago

    Hi

    I created by asking ChatGPT and verified for functionality by myself.

    For automation purposes, it's recommended to work with XML rather than text formats.



    ------------------------------
    RYOTA KOSAKA
    ------------------------------

    Attachment(s)

    txt
    RPC version.txt   523 B 1 version
    txt
    cli version.txt   479 B 1 version


  • 3.  RE: [OPS Script] Extract multiple show commands (e,.g: show interfaces terse | no-more, show version ) and save the output to a file.txt

    Posted 13 days ago

    Hi Ryota, 

    As for someone who never applied Scripts, how are you planning to run those scripts on you network devices? considering the devices have no central management 

    Thank you

    -------------------------------------------



  • 4.  RE: [OPS Script] Extract multiple show commands (e,.g: show interfaces terse | no-more, show version ) and save the output to a file.txt

    Posted 12 days ago
      |   view attached
    Hi
    I don't know if it's the answer you're looking for, but let me introduce you to on-box and off-box concepts(This is a Juniper term).
     
    ■on-box:
    To execute arbitrary commands, you'll need to use an op script as demonstrated here.
    Op scripts can be installed on each device or placed on a web server.
    When you want to put a script on a web server, you have to specify "#set system scripts op file xxx.py source "http://~~"" or you can execute "> op url http://~~~").
     
    Some configuration is required to make them work. Please refer to the official Juniper website for instructions.
     
     
    ■off-box:
    I recommend you to use netconf over SSH.
    It doesn't need to be as feature-rich as "central management", but you need to have a server capable of running netconf.
     
     
     
    Finally, I share a materials I previously prepared.
    I compiled the APIs available in Junos.



    ------------------------------
    RYOTA KOSAKA
    ------------------------------



  • 5.  RE: [OPS Script] Extract multiple show commands (e,.g: show interfaces terse | no-more, show version ) and save the output to a file.txt

    Posted 12 days ago
    Edited by spuluka 6 days ago

    Good question, and lots of options are possible.  Some options although not complete are listed below.

    • The op scripts could reside on each device and be manually triggered by an operator.
    • The op scripts could reside off-box and triggered by say Python for Python scripts leveraging Junos PyEz.
    • The op scripts could reside off-box and be triggered by Juise for SLAX scripts.
    • SLAX scripts can be hosted on Junos Space and they can be either executed locally on the Junos Space server, or again using Junos Space the SLAX scripts could be staged on the managed devices, and executed from Junos Space.
    • Scripts could be triggered as part of a workflow using a variety of tools e.g. Jenkins, Ansible, Git CI/CD, NETCONF, etc.
    • Scripts could be scheduled to trigger using event-options on box etc.

    I did take a look to see if I had any examples to hand, the following are SLAX scripts which outputs results to screen, although it wouldn't require much change to dump the results to a file as well.

    version 1.0;
    
    ns junos = "http://xml.juniper.net/junos/*/junos";
    ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
    ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
    ns func extension = "http://exslt.org/functions";
    ns utils = "http://xml.juniper.net/utils/1.0";
    
    import "../import/junos.xsl";
    
    
    match / {
        <op-script-results> {
            <output> {
                var $local = jcs:open();
    
                var $commands := {
                    <command> "ifconfig fxp0";
                    <command> "netstat | grep LISTEN";
                    <command> "top -d 1 -m cpu";
                    <command> "iostat -c 5";
                }
    
                for-each ( $commands/command ) {
                    var $result = utils:shell-execute($local, .);
                    expr "\n\nCommand " _ . _ "\n";
                    copy-of $result;
                }
    
                expr jcs:close($local);
            }
        }
    }
    
    <func:function name="utils:shell-execute">
    {
        param $conn;
        param $command;
        var $rpc = {
            <request-shell-execute> {
                <command> $command;
            }
        }
        var $result = jcs:execute($conn, $rpc);
        if ($result/..//xnm:error) {
            call rpc_failure($rpc = $result/.., $message = "Error executing shell command.");
        }    
        <func:result select="$result">;
    }


    Although this version is performing shell commands leveraging the request-shell-execute RPC, it would be simple enough to replace it instead with just the <command>show command</command> RPC instead.
    e.g.

    version 1.0;
    
    ns junos = "http://xml.juniper.net/junos/*/junos";
    ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
    ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
    ns func extension = "http://exslt.org/functions";
    ns utils = "http://xml.juniper.net/utils/1.0";
    
    import "../import/junos.xsl";
    
    
    match / {
        <op-script-results> {
            <output> {
                var $local = jcs:open();
    
                var $commands := {
                    <command> "show version";
                    <command> "show chassis hardware";
                    <command> "show interfaces";
                    <command> "show cli authorization";
                }
    
                for-each ( $commands/command ) {
                    var $result = utils:cli-execute($local, .);
                    expr "\n\nCommand " _ . _ "\n";
                    copy-of $result;
                }
    
                expr jcs:close($local);
            }
        }
    }
    
    <func:function name="utils:cli-execute">
    {
        param $conn;
        param $command;
        var $rpc = {
            <command format="text"> $command;
        }
        var $result = jcs:execute($conn, $rpc);
        if ($result/..//xnm:error) {
            call rpc_failure($rpc = $result/.., $message = "Error executing CLI command.");
        }    
        <func:result select="$result">;
    }

    Finally, if you required the output of the commands to be stored to disk rather than output to the screen, you can implement one of the document instructions, e.g. <exsl:document>.  For example:

    version 1.0;
    
    ns junos = "http://xml.juniper.net/junos/*/junos";
    ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
    ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
    ns func extension = "http://exslt.org/functions";
    ns utils = "http://xml.juniper.net/utils/1.0";
    ns exsl extension = "http://exslt.org/common";
    
    import "../import/junos.xsl";
    
    match / {
        <op-script-results> {
            <output> {
                var $local = jcs:open();
    
                var $commands := {
                    <command> "show version";
                    <command> "show chassis hardware";
                    <command> "show interfaces";
                    <command> "show cli authorization";
                }
    
                <exsl:document href="/var/tmp/example-file.txt" method="text" indent="yes"> {
                    for-each ( $commands/command ) {
                        var $result = utils:cli-execute($local, .);
                        expr "\n\nCommand " _ . _ "\n";
                        copy-of $result;
                    }
                }
                expr "\nCommand results saved to /var/tmp/example-file.txt\n";
                expr jcs:close($local);
            }
        }
    }
    
    <func:function name="utils:cli-execute">
    {
        param $conn;
        param $command;
        var $rpc = {
            <command format="text"> $command;
        }
        var $result = jcs:execute($conn, $rpc);
        if ($result/..//xnm:error) {
            call rpc_failure($rpc = $result/.., $message = "Error executing CLI command.");
        }    
        <func:result select="$result">;
    }

    Regards



  • 6.  RE: [OPS Script] Extract multiple show commands (e,.g: show interfaces terse | no-more, show version ) and save the output to a file.txt

    Posted 9 days ago

    Hi,

    There are multiple ways to do it. if you wnat to extract information from multiple devices from a single linux host, following is the url where i have created a script which runs from a linux host to junos devices 1 by 1 and collects what ever show commands you want it to collect.

    https://github.com/shabbir1282/junos_expect

    if you need any other help, this can be achieved from on-box agent as well but hten you have to collect the data from inidvidual devices which is a daunting task. I would prefer to run a script from a central location which collects the data and saves it to a single zip file.

    thank you,



    ------------------------------
    Shabbir Ahmed
    ------------------------------



  • 7.  RE: [OPS Script] Extract multiple show commands (e,.g: show interfaces terse | no-more, show version ) and save the output to a file.txt

    Posted 6 days ago

    The following is a SLAX op-script that can execute some defined CLI commands and store the results to disk on-box.

    Only caveat that comes to mind with this is that since it is using the command RPC to execute the command on the device, and return the data in text format, it won't support the pipe | command, bit you won't need to use | no-more anyway with this script.

    version 1.0;
    
    ns junos = "http://xml.juniper.net/junos/*/junos";
    ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
    ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
    ns func extension = "http://exslt.org/functions";
    ns utils = "http://xml.juniper.net/utils/1.0";
    ns exsl extension = "http://exslt.org/common";
    
    import "../import/junos.xsl";
    
    
    match / {
        <op-script-results> {
            <output> {
                var $local = jcs:open();
    
                var $commands := {
                    <command> "show version";
                    <command> "show chassis hardware";
                    <command> "show interfaces";
                    <command> "show cli authorization";
                }
    
                <exsl:document href="/var/tmp/example-file.txt" method="text" indent="yes"> {
                    for-each ( $commands/command ) {
                        var $result = utils:cli-execute($local, .);
                        expr "\n\nCommand " _ . _ "\n";
                        copy-of $result;
                    }
                }
                expr "\nCommand results saved to /var/tmp/example-file.txt\n";
                expr jcs:close($local);
            }
        }
    }
    
    <func:function name="utils:cli-execute">
    {
        param $conn;
        param $command;
        var $rpc = {
            <command format="text"> $command;
        }
        var $result = jcs:execute($conn, $rpc);
        if ($result/..//xnm:error) {
            call rpc_failure($rpc = $result/.., $message = "Error executing CLI command.");
        }    
        <func:result select="$result">;
    }

    Regards

    -------------------------------------------