Supported Platforms
Related Documentation
- EX, M, MX, QFX, SRX, T Series
- Example: Using the NETCONF Java Toolkit to Execute an Operational Request RPC
- Example: Using the NETCONF Java Toolkit to Load and Commit a Configuration
- Troubleshooting Exception Errors in a NETCONF Java Toolkit Program
- NETCONF Java Toolkit Class: Device
- NETCONF Java Toolkit Overview
Using the NETCONF Java Toolkit to Perform Operational Tasks
Using Device Object Methods to Execute RPCs and Operational Commands
The NETCONF Java toolkit Device object has methods to request information from and perform operational tasks on remote devices. When appropriate, the methods are overloaded to take a number of different formats.
Executing RPCs
To execute a remote procedure call (RPC), call the executeRPC() method on the Device object. The executeRPC() method is overloaded to accept a String object, a net.juniper.netconf.XML object, or an org.w3c.dom.Document object as the argument. The RPC is processed by the NETCONF server, which returns the RPC reply as an XML object.
The method syntax is:
public XML executeRPC (String rpcContent)
public XML executeRPC (net.juniper.netconf.XML rpc)
public XML executeRPC (org.w3c.dom.Document rpcDoc)
The following code snippet executes the Junos XML API get-chassis-inventory RPC using a string argument. The get-chassis-inventory RPC is equivalent to the show chassis hardware operational mode command in the Junos OS command-line interface (CLI).
Device device = new Device("10.10.1.1","admin","PaSsWoRd",null); device.connect(); try { XML rpc_reply = device.executeRPC("get-chassis-inventory"); System.out.println(rpc_reply.toString()); } catch (Exception e) { System.out.println("exception: " + e.getMessage()); // additional processing for exception } device.close();
Executing Operational Mode Commands
To execute an operational mode command to request information from or perform operational tasks on a device running Junos OS, call the runCliCommand() method on the Device object. The runCliCommand() method sends a Junos OS operational mode command to the NETCONF server on the remote device. The argument is a string representing the operational mode command that you would enter in the Junos OS CLI. The RPC is processed by the NETCONF server, which returns the RPC reply. 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.
The method syntax is:
public String runCLICommand (String command)
The following code snippet sends the CLI operational mode command show chassis hardware to the NETCONF server on a device running Junos OS:
Device device = new Device("10.10.1.1","admin","PaSsWoRd",null); device.connect(); try { cli_reply = device.runCliCommand("show chassis hardware"); System.out.println(cli_reply); } catch (Exception e) { System.out.println("exception: " + e.getMessage()); // additional processing for exception } device.close();
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:
- 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
. - 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:
- Compile the
ExecuteCLICommand.java
file.>javac ExecuteCLICommand.java - 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
- EX, M, MX, QFX, SRX, T Series
- Example: Using the NETCONF Java Toolkit to Execute an Operational Request RPC
- Example: Using the NETCONF Java Toolkit to Load and Commit a Configuration
- Troubleshooting Exception Errors in a NETCONF Java Toolkit Program
- NETCONF Java Toolkit Class: Device
- NETCONF Java Toolkit Overview
Published: 2012-11-26
Supported Platforms
Related Documentation
- EX, M, MX, QFX, SRX, T Series
- Example: Using the NETCONF Java Toolkit to Execute an Operational Request RPC
- Example: Using the NETCONF Java Toolkit to Load and Commit a Configuration
- Troubleshooting Exception Errors in a NETCONF Java Toolkit Program
- NETCONF Java Toolkit Class: Device
- NETCONF Java Toolkit Overview