Okay, so choose any of the options available.
All of these approaches perform the given command and will respond with a NETCONF <reply>.
NETCONF can also be called via numerous different scripting languages too, Python, Ruby, Perl, SLAX etc. They all return the data in a similar manner, e.g the contents of the NETCONF reply in XML.
I also tested with Postman, and again had no issue with making a POST to the box to obtain data.
Original Message:
Sent: 07-26-2024 10:28
From: JOHN WILLIAMSON
Subject: How can a switch get trigger events from an external device like a UPS?
Andy,
I'm not so much concerned about the format it returns, but that I can send a rpc command and the switch does respond. So, I would test my alerts with a boring command before changing them to power off commands.
------------------------------
JOHN WILLIAMSON
Original Message:
Sent: 07-25-2024 20:31
From: asharp
Subject: How can a switch get trigger events from an external device like a UPS?
Okay, so the "| display xml rpc" only returns the RPC information for that particular command, it doesn't actually execute the command.
If you want to see what the data returned is like within the CLI, then just omit the "rpc" from the command. e.g.
show version | display xml | no-more<rpc-reply xmlns:junos="http://xml.juniper.net/junos/22.1R0/junos"> <software-information> <host-name>test960</host-name> <product-model>mx960</product-model> <product-name>mx960</product-name> <junos-version>22.1R1.10</junos-version> .... </software-information> <cli> <banner></banner> </cli></rpc-reply>
That will execute the given command (show version) in this example, and return the data in the format specified, XML in this instance.
Please note that the command is being executed, just that using the "| display xml" instructs MGD to return the data in its native XML format, rather than the rendered CLI version.
You could also substitute XML for JSON and get the data returned in that format. e.g. show version | display json
show version | display json | no-more{ "software-information" : [ { "host-name" : [ { "data" : "test960" } ], "product-model" : [ { "data" : "mx960" } ], "product-name" : [ { "data" : "mx960" } ], "junos-version" : [ { "data" : "22.1R1.10" }etc.
If you wanted to make an RPC and return the data in a particular format (set, text, json, xml) taking into consideration that set format is only valid for configuration rather than show/request commands then it would look something like the following: In this example, I am using SSH to connect to the device, but using the -s netconf argument to SSH so that I can access the netconf subsytem on the router. Then I just enter the RPC directly and hit Enter to see the rpc reply from the device.
$ ssh test960 -s netconf....<rpc><get-system-information format="text"/></rpc><rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/22.1R0/junos"><output>Model: mx960Family: junosJunos: 22.1R1.10Hostname: test960</output></rpc-reply><rpc><get-system-information format="json"/></rpc><rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/22.1R0/junos">{ "system-information" : [ { "hardware-model" : [ { "data" : "mx960" } ], "os-name" : [ { "data" : "junos" } ], "os-version" : [ { "data" : "22.1R1.10" } ], "host-name" : [ { "data" : "test960" } ] } ]}</rpc-reply><rpc><close-session/></rpc><rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/22.1R0/junos"><ok/></rpc-reply>]]>]]><!-- session end at 2024-07-25 17:26:54 PDT -->
Or of course continue to use curl to perform the same.
curl -u "username:password" http://xxxxx:xxxx/rpc/get-system-information@format=jsoncurl -u "username:password" http://xxxx:xxxx/rpc/get-system-information -H "Accept: application/json"
Regards,
------------------------------
Andy Sharp
Original Message:
Sent: 07-25-2024 12:45
From: JOHN WILLIAMSON
Subject: How can a switch get trigger events from an external device like a UPS?
I'm using the Rest API Explorer that I have enabled on a test switch.
I was able to test the power off command, and that worked. Very good. Now, I want to test a command that won't requirement to wait for this EX 2300-12 to reboot each time. I would like to send a simple command that would do something simple, like show version which = get-software-information in rpc. I'd like to see it happen in the switch CLI so I know it worked. I thought that would be a post and then I would but the rpc code in , like this:
<rpc>
<get-software-information>
</get-software-information>
</rpc>
But that does not show any action on the switch in the CLI as a confirmation. I've tried both a simple get and a post and have the same result.
------------------------------
JOHN WILLIAMSON
Original Message:
Sent: 07-25-2024 10:33
From: JOHN WILLIAMSON
Subject: How can a switch get trigger events from an external device like a UPS?
I found the reference to that "display XML rpc" but I expected a doc rather than just trying each command and hoping it was supported.
------------------------------
JOHN WILLIAMSON
Original Message:
Sent: 07-24-2024 19:20
From: asharp
Subject: How can a switch get trigger events from an external device like a UPS?
Hi,
Within Junos itself, any CLI command can also provide you with what the RPC is associated with the command that you are trying to find.
Just add, "| display xml rpc" to the end of the command that you want to check. If there is an RPC available specific to that command, it will be displayed.
> request system power-off in 5 | display xml rpc<rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.1X46/junos"> <rpc> <request-power-off> <in>5</in> </request-power-off> </rpc> <cli> <banner>{primary:node1}</banner> </cli></rpc-reply>
Another example.
> show chassis hardware extensive | display xml rpc<rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.1X46/junos"> <rpc> <get-chassis-inventory> <extensive/> </get-chassis-inventory> </rpc> <cli> <banner>{primary:node1}</banner> </cli></rpc-reply>
There are some commands that do not have any RPC associated with them, for example "show chassis fan | display xml rpc" will probably return the response "xml rpc equivalent of this command is not available.", if you are running an old Junos release, newer releases support the RPC <get-fan-information> as far as I can recall. However, you can still make an RPC to perform that task, you just have to use the RPC command, which allows you to execute normal cli commands via an RPC. e.g.
<rpc><command>show chassis fan</command></rpc>
Some reference information on the subject can be found here: https://www.juniper.net/documentation/us/en/software/junos/netconf/junos-xml-protocol/topics/task/junos-xml-protocol-rpcs-and-xml-mapping.html#mapping-commands-to-junos-xml-request-tag-elements
XML API Explorer: https://apps.juniper.net/xmlapi/operational/
With the XML API Explorer, you can enter the cli command in the filter field and that will also return the RPC equivalent where available for the particular Junos version you are interested in.
I rarely use it, most times I'm just running the "| display xml rpc" command via the CLI to find out the available RPC, but it is there if you need to reference it.
Regards,
------------------------------
Andy Sharp
Original Message:
Sent: 07-24-2024 17:32
From: JOHN WILLIAMSON
Subject: How can a switch get trigger events from an external device like a UPS?
Is there a list of EX switch rpc commands somewhere? I can't find it. I'd like to know what I can get and post to help figure out what I can tell the switch to do.
------------------------------
JOHN WILLIAMSON
Original Message:
Sent: 07-24-2024 12:38
From: asharp
Subject: How can a switch get trigger events from an external device like a UPS?
I'm not familiar with Postman, but with curl you would expect to do something like the following:
I haven't verified it as I don't have access to a device right now that supports the REST API. But I would hope something like the following would work. Either using a POST with text/plain or with xml data.
curl -u "username:password" http://device-name:port/rpc/request-power-off --header "Content-Type: text/plain" –d "in=3"curl -u "username:password" http://device-name:port/rpc/request-power-off --header "Content-Type: application/xml" –d "<in>3</in>"
Of course it depends on the version of Junos deployed and if REST is enabled in the configuration etc.
Regards,
------------------------------
Andy Sharp
Original Message:
Sent: 07-24-2024 10:09
From: JOHN WILLIAMSON
Subject: How can a switch get trigger events from an external device like a UPS?
I'll be sending the alerts from Solarwinds which we use to monitor the UPSes and other devices. Is there a way to use Postman and REST API to talk to the switches?
------------------------------
JOHN WILLIAMSON
Original Message:
Sent: 07-24-2024 09:24
From: asharp
Subject: How can a switch get trigger events from an external device like a UPS?
Hi,
I'm not sure that Junos provides the capabilities to receive an event in that manner. Or maybe it does and I'm not familiar with that functionality...?
I guess other options would depend on what the capabilities are for triggering events from the UPS. For example, perhaps it is possible to trigger a command to execute using SSH that in conjunction with NETCONF and using ssh certificates you could instruct the device to shutdown.
e.g.
ssh -i ~/.ssh/id_rsa auto@192.168.0.212 -s netconf < shutdown_rpc.txtmore shutdown_rpc.txt<rpc><request-power-off><in>3</in></request-power-off></rpc>This will shutdown the box in 3 minutes as an example.
Regards,
------------------------------
Andy Sharp
Original Message:
Sent: 07-22-2024 13:28
From: JOHN WILLIAMSON
Subject: How can a switch get trigger events from an external device like a UPS?
We would like to be able to have our switches react to events on our UPSes. Example:" If UPS has less than 5 min of battery left, power down the switch. " We would have other commands for other UPS events. I just don't know how to pass an event into the switch to evaluate and run a CLI command based on it.
------------------------------
JOHN WILLIAMSON
------------------------------