Package: restconf-cli
RESTCONF Command Line Interface (restconf-cli)Introductionrestconf-cli is a command line interface application which interacts with the restconf enabled devices (i.e. iosxe, nxos, nso). This module uses PythonClickmodule for command line interface (CLI) andrichmodule for the colorful output. It is  built on top of Pythonrequestslibrary to make Restconf API calls. Here are the key information for this modules.Base URL used 'https://<hostname/ip>:/restconf/data/'Default port used for the Restconf API 443Default Headers used for Accept and Content-Type are the following.Accept: application/yang-data+json
Content-Type: application/yang-data+jsonSince default headers are using 'application/yang-data+json', therefore, the output will be in following formats for the below type of devices unless specified in the table below.Device TypeIOSXENXOSNSODefault Accept  Headerapplication/yang-data+jsonapplication/yang-data+jsonapplication/yang-data+jsonDefault Content-Type Headerapplication/yang-data+jsonapplication/yang-data+jsonapplication/yang-data+jsonDefault Output FormatJSONXMLJSONSame for the POST, PUT and PATCH operations, if you do not specify the header fields, it assumes that you are sending the data in the formats mentioned above.Currently tested/supported on Python 3.8, 3.9 and 3.10Disclaimer:This module uses Insecure Requests which is not recommended, use
certificates where possible.InstallationYou can download this module from PyPi repository via PIPTo install a module, simply typepipinstallrestconf-cliNote:It is also recommended that use the virtual environment for any package you are testing.(main)expert@expert-cws:~/venvs$python-mvenv.venv(main)expert@expert-cws:~/venvs$source.venv/bin/activate(.venv)expert@expert-cws:~/venvs$pipinstallrestconf-cliUsageOnce you have installed therestconf-clipackage, you can test this against any Cisco IOSXE, NXOS and NSO device. I have not tested for any other devices, but if you come across any device where this is working or not, feel free to raise an issue or send a pull request.Let's first explore the documentationRunrestconf-cli --helpon the terminal for help text(.venv)expert@expert-cws:~$restconf-cli--help
Usage:restconf-cli[OPTIONS]COMMAND[ARGS]...CLItooltointeractwiththerestconfAPIscurrentlysupportedforIOSXE,NXOSandNSO.ThislibraryusesthefollowingrootURLfortherestconfwithport443asdefaultport.https://<hostname/ip>:<port>/restconf/data/DefaultHeadersforAcceptandContent-Typearethefollowing.Accept:application/yang-data+jsonContent-Type:application/yang-data+jsonSincedefaultheadersareusing'application/yang-data+json',therefore,youwillretrievetheoutputinfollowingformatsforthebelowtypeofdevicesunlessspecifiedfortheGEToperation.|DeviceType|IOSXE|NXOS|NSO||:-------------------:|:--------------------------:|:--------------------------:|:-------------------------:||DefaultAccept|application/yang-data+json|application/yang-data+json|application/yang-data+json||DefaultContent-Type|application/yang-data+json|application/yang-data+json|application/yang-data+json||DefaultOutputFormat|JSON|XML|JSON|SameforthePOST,PUTandPATCHoperationifyoudonotspecifytheheaderfields,itassumesyouaresendingthedataintheformatsmentionedabove.Disclaimer:ThismoduleusesInsecureRequestswhichisnotrecommended,usecertificateswherepossible.

Options:--helpShowthismessageandexit.

Commands:DELETEMethodtodeletethetargetresourceExample:$restconf-cli...GETMethodtoretrieveoperationalorconfigdatafromthedevices.PATCHsameasPUT,exceptiftheresourcedoesnotexist,thedevices...POSTSendsdatatothedevicestocreateanewdataresource.PUTSenddatatothedevicestocreateorupdatethedataresource.You can read all the above information, but this is more or less same info which already exist in this readme file. The important key of information here, are the commands, as you can see there are about 5 commands available which are basically CRUD operation for the Restconf API. We can again-hor--helpflag to see the information inside each of these commands. Let's check each of them and see what command options are available.For GET operation command options, simply runrestconf-cli GET --helporrestconf-cli GET -h(.venv)expert@expert-cws:~$restconf-cliGET--help
Usage:restconf-cliGET[OPTIONS]Methodtoretrieveoperationalorconfigdatafromthedevices.Defaultheaderfortherequestsare'application/yang-data+json'Example:# Display output on the terminal$restconf-cliGET-udeveloper-nsandbox-iosxe-latest-1.cisco.com\-pCisco-IOS-XE-native:native/version\-aapplication/yang-data+json\-capplication/yang-data+json# Display output on the terminal and save the output on a file defined with --output or -o flag$restconf-cliGET-udeveloper-nsandbox-iosxe-latest-1.cisco.com\-pCisco-IOS-XE-native:native/interface\-ooutput.json

Options:-o,--outputFILENAMEOutputwillbewrittentoafile-c,--content-typeTEXTContent-Typeheaderforrestconfapi,defaultisapplication/yang-data+json-a,--acceptTEXTAcceptheaderforrestconfapi,defaultisapplication/yang-data+json-pn,--portINTEGERPortnumberforrestconfapi,defaultis443-p,--pathTEXTPathforrestconfapicall[required]--passwordTEXTPasswordforrestconfapi-u,--usernameTEXTUsernameforrestconfapi[required]-n,--hostnameTEXTDevicehostnameorIPaddressfortherestconfAPI[required]-h,--helpShowthismessageandexit.For POST operation command options, runrestconf-cli POST --helporrestconf-cli POST -h(.venv)expert@expert-cws:~$restconf-cliPOST-h
Usage:restconf-cliPOST[OPTIONS]Sendsdatatothedevicestocreateanewdataresource.Example:# Configure via raw data for POST operation$restconf-cliPOST-udeveloper-nsandbox-iosxe-latest-1.cisco.com\-pietf-interfaces:interfaces\-d'{"interface":[{"name":"Loopback999","description":"created by python click - POST","type":"iana-if-type:softwareLoopback","enabled":true,"ietf-ip:ipv4":{"address":[{"ip":"10.0.1.10","netmask":"255.255.255.255"}]}}]}'# Configure from file for POST operation$restconf-cliPOST-udeveloper\-nsandbox-iosxe-latest-1.cisco.com\-pietf-interfaces:interfaces-ffinterface.json

Options:-ff,--from-fileFILENAMEReadtheplayloadfromfileforPOSToperation-d,--dataTEXTPlayloadtobesentforPOST,PUTandPATCHmethods-c,--content-typeTEXTContent-Typeheaderforrestconfapi,defaultisapplication/yang-data+json-a,--acceptTEXTAcceptheaderforrestconfapi,defaultisapplication/yang-data+json-pn,--portINTEGERPortnumberforrestconfapi,defaultis443-p,--pathTEXTPathforrestconfapicall[required]--passwordTEXTPasswordforrestconfapi-u,--usernameTEXTUsernameforrestconfapi[required]-n,--hostnameTEXTDevicehostnameorIPaddressfortherestconfAPI[required]-h,--helpShowthismessageandexit.For PUT operation command options, runrestconf-cli PUT --helporrestconf-cli PUT -h(.venv)expert@expert-cws:~$restconf-cliPUT-h
Usage:restconf-cliPUT[OPTIONS]Senddatatothedevicestocreateorupdatethedataresource.Example:# Configure via raw data for PUT operation$restconf-cliPUT-udeveloper-nsandbox-iosxe-latest-1.cisco.com\-pietf-interfaces:interfaces\-d'{"interface":[{"name":"Loopback999","description":"created by python click - PUT","type":"iana-if-type:softwareLoopback","enabled":true,"ietf-ip:ipv4":{"address":[{"ip":"10.0.1.10","netmask":"255.255.255.255"}]}}]}'# Configure from file for PUT operation$restconf-cliPUT-udeveloper\-nsandbox-iosxe-latest-1.cisco.com\-pietf-interfaces:interfaces/interface=Loopback999-ffinterface.json

Options:-ff,--from-fileFILENAMEReadtheplayloadfromfileforPUToperation-d,--dataTEXTPlayloadtobesentforPOST,PUTandPATCHmethods-c,--content-typeTEXTContent-Typeheaderforrestconfapi,defaultisapplication/yang-data+json-a,--acceptTEXTAcceptheaderforrestconfapi,defaultisapplication/yang-data+json-pn,--portINTEGERPortnumberforrestconfapi,defaultis443-p,--pathTEXTPathforrestconfapicall[required]--passwordTEXTPasswordforrestconfapi-u,--usernameTEXTUsernameforrestconfapi[required]-n,--hostnameTEXTDevicehostnameorIPaddressfortherestconfAPI[required]-h,--helpShowthismessageandexit.For PATCH operation command options, runrestconf-cli PATCH --helporrestconf-cli PATCH -h(.venv)expert@expert-cws:~$restconf-cliPATCH-h
Usage:restconf-cliPATCH[OPTIONS]sameasPUT,exceptiftheresourcedoesnotexist,thedevicesMUSTNOTcreateone.Example:# Configure via raw data for PATCH operation$restconf-cliPATCH-udeveloper-nsandbox-iosxe-latest-1.cisco.com\-pietf-interfaces:interfaces\-d'{"interface":[{"name":"Loopback999","description":"created by python click - PATCH","type":"iana-if-type:softwareLoopback","enabled":true,"ietf-ip:ipv4":{"address":[{"ip":"10.0.1.10","netmask":"255.255.255.255"}]}}]}'# Configure from file for PATCH operation$restconf-cliPATCH-udeveloper\-nsandbox-iosxe-latest-1.cisco.com\-pietf-interfaces:interfaces/interface=Loopback999-ffinterface.json

Options:-ff,--from-fileFILENAMEReadtheplayloadfromfileforPATCHoperation-d,--dataTEXTPlayloadtobesentforPOST,PUTandPATCHmethods-c,--content-typeTEXTContent-Typeheaderforrestconfapi,defaultisapplication/yang-data+json-a,--acceptTEXTAcceptheaderforrestconfapi,defaultisapplication/yang-data+json-pn,--portINTEGERPortnumberforrestconfapi,defaultis443-p,--pathTEXTPathforrestconfapicall[required]--passwordTEXTPasswordforrestconfapi-u,--usernameTEXTUsernameforrestconfapi[required]-n,--hostnameTEXTDevicehostnameorIPaddressfortherestconfAPI[required]-h,--helpShowthismessageandexit.For DELETE operation command options, runrestconf-cli DELETE --helporrestconf-cli DELETE -h(.venv)expert@expert-cws:~$restconf-cliDELETE-h
Usage:restconf-cliDELETE[OPTIONS]MethodtodeletethetargetresourceExample:$restconf-cliDELETE-udeveloper-nsandbox-iosxe-latest-1.cisco.com\-pietf-interfaces:interfaces/interface=Loopback999

Options:-n,--hostnameTEXTDevicehostnameorIPaddressfortherestconfAPI[required]-u,--usernameTEXTUsernameforrestconfapi[required]--passwordTEXTPasswordforrestconfapi-p,--pathTEXTPathforrestconfapicall[required]-pn,--portINTEGERPortnumberforrestconfapi,defaultis443-a,--acceptTEXTAcceptheaderforrestconfapi,defaultisapplication/yang-data+json-c,--content-typeTEXTContent-Typeheaderforrestconfapi,defaultisapplication/yang-data+json-h,--helpShowthismessageandexit.Notice there are some examples mentioned in the above output for every command, we will explore these in the next section.ExamplesFor the sake of testing, I am going to use Cisco Always-on IOSXE devicesandbox-iosxe-latest-1.cisco.comwhich uses the443default port for Restconf and both Accept and Content-type headers areapplication/yang-data+json, which makes our CLI command earsier as these options are default forrestconf-cli.restconf-cli GET -u developer -n sandbox-iosxe-latest-1.cisco.com -p Cisco-IOS-XE-native:native/version(.venv)expert@expert-cws:~$restconf-cliGET-udeveloper-nsandbox-iosxe-latest-1.cisco.com-pCisco-IOS-XE-native:native/version
Password:{"Cisco-IOS-XE-native:version":"17.3"}Status:200OKThe output will be colorful asrestconf-clicli uses the rich module to print colorful output.For more examples, please check theexamplesfolder.IssuesPlease raise an issue or pull request if you find something wrong with this module.AuthorsMuhammad RafiReferenceshttps://click.palletsprojects.com/en/8.1.x/https://click.palletsprojects.com/en/7.x/changelog/#version-7-1-2