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 Execute CLI Commands
- Example: Using the NETCONF Java Toolkit to Load and Commit a Configuration
- Example: Using the NETCONF Java Toolkit to Load Set Configuration Commands
- Troubleshooting Exception Errors in a NETCONF Java Toolkit Program
- Using the NETCONF Java Toolkit to Parse an RPC Reply
- NETCONF Java Toolkit Overview
Example: Using the NETCONF Java Toolkit to Print Component Temperatures
This NETCONF Java toolkit program prints the name and corresponding temperature of components 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 following example executes the Junos XML API get-environment-information RPC, which is the equivalent of the show chassis environment operational mode command on a device running Junos OS. The program parses the RPC reply, and for all components that list a temperature, the program prints the component name and corresponding temperature.
The RPC reply format for the get-environment-information RPC request is:
<rpc-reply> <environment-information> <environment-item> <name>item-name</name> ... <temperature>temperature</temperature> </environment-item> <environment-item> <name>item-name2</name> ... <temperature>temperature</temperature> </environment-item> ... </environment-information> </rpc-reply>
To parse the reply, the program uses the findNodes() method to return a list of org.w3c.dom.Node objects. For each <environment-item> node, the program obtains a list of child nodes. If a temperature element is present in the child node list, the program prints the name and temperature of that environment item.
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
ShowTemps
. - 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
ShowTemps.java
program is presented here.import java.io.IOException; import java.util.Arrays; import java.util.Iterator; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import net.juniper.netconf.CommitException; import net.juniper.netconf.Device; import net.juniper.netconf.LoadException; import net.juniper.netconf.NetconfException; import net.juniper.netconf.XML; import net.juniper.netconf.XMLBuilder; import org.xml.sax.SAXException; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class showTemps { public static void main(String[] args) throws LoadException, IOException, NetconfException, ParserConfigurationException, SAXException {
String name="", temp=""; //Create the device Device device = new Device("10.10.1.1","admin","PaSsWoRd",null); device.connect(); //Call executeRPC(String rpc) to send RPC and receive RPC reply XML rpc_reply = device.executeRPC("get-environment-information"); // Parse reply and only print items that have a temperature element List<String> list = Arrays.asList("environment-information","environment-item"); List itemlist = rpc_reply.findNodes(list); Iterator iter = itemlist.iterator(); while (iter.hasNext()) { Node item_node = (Node) iter.next(); NodeList child_nodes = item_node.getChildNodes(); // child_nodes contains nodes like <name> and <temperature> for (int i = 0; i < child_nodes.getLength(); i++) { Node child = child_nodes.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { if (child.getNodeName().equals("name")) // Capture the text value in <name> node name = child.getTextContent(); if (child.getNodeName().equals("temperature")) { // Capture the text value in <temperature> node temp = child.getTextContent(); System.out.println(name + ": " + temp); } } } } 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
ShowTemps.java
file.>javac ShowTemps.java - Execute the resulting
ShowTemps
program.>java ShowTemps
Verification
Verifying the Results
Purpose
Verify that the ShowTemps
program runs correctly.
Action
If the program executes successfully, it establishes a connection and a creates a NETCONF session with the specified device. The program then executes the Junos XML API get-environment-information RPC, parses the RPC reply, and prints all environment items that contain a child node <temperature>.
The following sample output is from a Juniper Networks m7i router:
Intake: 25 degrees C / 77 degrees F FPC 0: 26 degrees C / 78 degrees F Power Supplies: 28 degrees C / 82 degrees F CFEB Intake: 22 degrees C / 71 degrees F CFEB Exhaust: 30 degrees C / 86 degrees F Routing Engine: 28 degrees C / 82 degrees F Routing Engine CPU: 28 degrees C / 82 degrees F
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 Execute CLI Commands
- Example: Using the NETCONF Java Toolkit to Load and Commit a Configuration
- Example: Using the NETCONF Java Toolkit to Load Set Configuration Commands
- Troubleshooting Exception Errors in a NETCONF Java Toolkit Program
- Using the NETCONF Java Toolkit to Parse an RPC Reply
- NETCONF Java Toolkit Overview
Published: 2013-07-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 Execute CLI Commands
- Example: Using the NETCONF Java Toolkit to Load and Commit a Configuration
- Example: Using the NETCONF Java Toolkit to Load Set Configuration Commands
- Troubleshooting Exception Errors in a NETCONF Java Toolkit Program
- Using the NETCONF Java Toolkit to Parse an RPC Reply
- NETCONF Java Toolkit Overview