Junos Automation (Scripting)
Highlighted
Junos Automation (Scripting)

Edit config through REST API

‎12-31-2019 01:15 PM

Is it possible to edit the config of a junos device using the REST API? I see that it has both GET and POST capabilities, but I can't find any information online about how to edit a device's config. I've gotten it to work for reading data from the device, but I would like to update the config this way as well.

3 REPLIES 3
Highlighted
Junos Automation (Scripting)

Re: Edit config through REST API

‎12-31-2019 06:34 PM

Hi Ian,

 

Find here a working example of REST API using Curl command and corresponding MX config here:

 

root@MX> show version | grep Junos:
Junos: 16.1

 

root@MX> show interfaces terse lo0
Interface Admin Link Proto Local Remote
lo0 up up
lo0.0 up up inet 10.0.6.25 --> 0/0
lo0.16384 up up inet 127.0.0.1 --> 0/0
lo0.16385 up up inet

 

root@MX> show configuration system services
rest {
http {
port 1234;
}
enable-explorer;
}


From Server:
==============
C:\>curl http://labroot:lab123@10.1.1.1:1234/rpc?stop-on-error=1 -d "<lock-configuration/><load-configuration><configuration><interfaces><interface><name>lo0</name><unit><name>0</name><description>Somethinghere</description><family><inet><address><name>2.0.0.6/32</name></address></inet></family></unit></interface></interfaces></configuration></load-configuration><commit/><unlock-configuration/>"

 

RESULT:
-------------
--nwlrbbmqbhcdarz

--nwlrbbmqbhcdarz
Content-Type: application/xml; charset=utf-8

<load-configuration-results>
<load-success/>
</load-configuration-results>
--nwlrbbmqbhcdarz
Content-Type: application/xml; charset=utf-8

<commit-results xmlns:junos="http://xml.juniper.net/junos/*/junos">
<routing-engine junos:style="normal">
<name>re0</name>
<commit-success/>
<commit-revision-information>
<new-db-revision>re0-1551864399-284</new-db-revision>
<old-db-revision>re0-1551761092-283</old-db-revision>
</commit-revision-information>
</routing-engine>
</commit-results>
--nwlrbbmqbhcdarz

--nwlrbbmqbhcdarz--


AFTER the commit via Curl:
============================

root@MX> show interfaces terse lo0
Interface Admin Link Proto Local Remote
lo0 up up
lo0.0 up up inet 2.0.0.6 --> 0/0
10.0.6.25 --> 0/0
lo0.16384 up up inet 127.0.0.1 --> 0/0
lo0.16385 up up inet

 

root@MX> show interfaces descriptions lo0.0
Interface Admin Link Description
lo0.0 up up Somethinghere

 

Here are some posts that are relevant here which you might find interesting!

 

https://forums.juniper.net/t5/Junos-Automation-Scripting/Configure-MX-router-using-REST-API/td-p/459...

 

https://forums.juniper.net/t5/Junos-Space-Developer/Announcing-SpaceEZ-a-new-Python-module-for-easy-...

 

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

Accept as Solution = cool ! (Help fellow community members with similar query be redirected here instead of them reposting again)
Accept as Solution+Kudo = You are a Star !

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

//Nex

Highlighted
Junos Automation (Scripting)

Re: Edit config through REST API

‎01-01-2020 07:28 AM

Great, thank you! That's exactly what I was looking for.

 

One more thing though, is there a JSON counterpart to the <lock-configuration>, <load-configuration>, and <commit> elements? I'm storing my variables in YAML, so it would be great to be able to just feed the variable dict to to_json in ansible but I'm not sure what to do about those elements.

Junos Automation (Scripting)

Re: Edit config through REST API

‎01-02-2020 06:20 AM

Alright, so according to the whitepaper PDF "JSON Support for Junos OS", the data control parameters will still be in XML, so things like commit, load-configuration, etc will still be held within XML tags. In theory you can supply the configuration in JSON within the <configuration-json> XML tags, but this appears to only be supported on some devices. The device I'm working on does not support this, so I'm left generating XML from the YAML variables, which isn't ideal but gets the job done.

Feedback