Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

Navigation
 

Related Documentation

 

Example: Using the NETCONF Java Toolkit to Execute CLI Commands

This NETCONF Java toolkit program demonstrates the runCLICommand() method, which sends the specified Junos OS operational mode command to the NETCONF server to request information from or perform operational tasks on a device running Junos OS.

Requirements

  • Routing, switching, or security device running Junos OS.
  • NETCONF Java toolkit is installed on the configuration management server.
  • Client application can log in to the device where the NETCONF server resides.
  • NETCONF service over SSH is enabled on the device where the NETCONF server resides.

Overview

The NETCONF Java toolkit Device class contains the runCliCommand() method, which takes a Junos OS CLI operational mode command and converts it to an equivalent RPC in XML that can be processed by the NETCONF server. The runCLICommand() method takes as an argument the string representing an operational mode command that you enter in the Junos OS CLI.

The following example executes the show chassis hardware command on a device running Junos OS. The return value for the method is a string. Starting with Junos OS Release 11.4, the return string is the same ASCII-formatted output that you see in the Junos OS CLI. For devices running earlier versions of Junos OS, the return string contains Junos XML tag elements.

Configuration

Creating the Java program

Step-by-Step Procedure

To construct the Java program file:

  1. Give the file a descriptive name.

    The filename must be the same as the class name. For this example, the file and class are named ExecuteCLICommand.

  2. Add the code to the file and update the environment-specific variables such as the remote host IP address, username, password, and <rpc-reply> tag elements.

    The complete Java code for the ExecuteCLICommand.java program is presented here.

    /*ExecuteCLICommand*/
    import java.io.IOException;
    import javax.xml.parsers.ParserConfigurationException;
    import net.juniper.netconf.Device;
    import net.juniper.netconf.NetconfException;
    import net.juniper.netconf.XML;
    import org.xml.sax.SAXException;
    
    public class ExecuteCLICommand {
        public static void main(String args[]) throws NetconfException, 
                  ParserConfigurationException, SAXException, IOException {
            
            String cli = "show chassis hardware";
    
            Device device = new Device("10.10.1.1","admin","PaSsWoRd",null);
            device.connect();
            try {
                String cli_reply = device.runCliCommand(cli);        
                System.out.println(cli_reply);
            }
            catch (Exception e) {
                System.out.println("exception: " + e.getMessage());
                // additional processing for exception
            }
            device.close();
        }
    }

Compiling and Running the Java Program

Step-by-Step Procedure

You need a Java compiler to compile the source code and to create an executable program.

To compile the code and run the program on the configuration management server:

  1. Compile the ExecuteCLICommand.java file.
    >javac ExecuteCLICommand.java
  2. Execute the resulting ExecuteCLICommand program.
    >java ExecuteCLICommand

Verification

Verifying Program Execution

Purpose

Verify that the ExecuteCLICommand program runs correctly.

Action

If the program executes successfully, it establishes a connection and creates a NETCONF session with the specified device. The program converts the Junos OS CLI operational mode command show chassis hardware to an RPC and sends the RPC to the NETCONF server. The server responds with the requested operational information enclosed in the <rpc-reply> tag element The program parses the RPC reply and prints the resulting chassis inventory. The following sample output is from a Juniper Networks m7i router.

On a device running Junos OS Release 11.4 or later release, the output is in ASCII-formatted text, which is identical to the output in the CLI.

Hardware inventory:
Item             Version  Part number  Serial number     Description
Chassis                                30010             M7I
Midplane         REV 03   710-008761   CB3874            M7i Midplane
Power Supply 0   Rev 04   740-008537   PG10715           AC Power Supply
Routing Engine   REV 07   740-009459   1000445584        RE-5.0
CFEB             REV 07   750-010464   CM4612            Internet Processor II
FPC 0                                                    E-FPC
  PIC 0          REV 06   750-002971   CB0032            4x OC-3 SONET, MM
  PIC 1          REV 02   750-002982   HS2878            1x Tunnel
  PIC 2          REV 08   750-005724   CL9084            2x OC-3 ATM-II IQ, MM
  PIC 3          REV 12   750-012838   DJ1107            4x 1GE(LAN), IQ2
    Xcvr 0       REV 01   740-013111   7303405           SFP-T
    Xcvr 1       REV 01   740-013111   7303391           SFP-T
    Xcvr 2       REV 01   740-013111   7303350           SFP-T
    Xcvr 3       REV 01   740-013111   7303420           SFP-T
FPC 1                                                    E-FPC
  PIC 2          REV 07   750-009487   CL5745            ASP - Integrated (Layer-2-3)
  PIC 3          REV 07   750-009098   CB7256            2x F/E, 100 BASE-TX
Fan Tray                                                 Rear Fan Tray

On a device running Junos OS Release 11.3 or earlier release, the output contains Junos XML tag elements.

<rpc-reply xmlns:junos="http://xml.juniper.net/junos/11.2R1/junos">
    <chassis-inventory xmlns="http://xml.juniper.net/junos/11.2R1/junos-chassis">
        <chassis junos:style="inventory">
            <name>Chassis</name>
            <serial-number>30010</serial-number>
            <description>M7I</description>
            <chassis-module>
                <name>Midplane</name>
                <version>REV 03</version>
                <part-number>710-008761</part-number>
                <serial-number>CB3874</serial-number>
                <description>M7i Midplane</description>
                <model-number>CHAS-MP-M7i-1GE-S</model-number>
            </chassis-module>

            /* Output omitted for brevity */

        </chassis>
    </chassis-inventory>
</rpc-reply>
 

Related Documentation

 

Published: 2013-07-26

 

Related Documentation

 

Published: 2013-07-26