Running Shell Commands or Scripts Using REST API


Netvisor ONE version 5.1.0 provides the ability to run shell commands or scripts using REST API or  through CLI commands.  As a network administrator or as an admin user, you can run the scripts from the directories   /opt/nvOS/bin/pn-scripts (directory and all files are delivered as part of  pn-upgrade-agent package)  and /usr/bin/pn-scripts  (backup directory for running custom scripts).  


The commands introduced to enable this feature are:  pn-script-show  (to view all the available scripts) and pn-script-run name <script-name>  (to run a specified script).


Usage Guidelines: To run a custom script,

    • You should have permission to run the script.
    • You should not have any duplicate scripts in the directories,  /opt/nvOS/bin/pn-scripts and /usr/bin/pn-scripts. In case of duplicate scripts, the script from the directory,  /opt/nvOS/bin/pn-scripts takes precedence.
    • It is not recommended to execute any scripts that are manually copied to the directory.


 You can use the CLI commands or the  vREST API to run the scripts. To run the scripts using the CLI commands, for example:


To display the available scripts, use the command:


CLI (network-admin@pn-lab1) > pn-script-show


switch: pn-lab1

pn-lab1: /opt/nvOS/bin/pn-scripts/:

testscript.sh

pn-testscript.sh


To run the script, use the command:


CLI (network-admin@pn-lab1) > pn-script-run name testscript.sh


Executing /opt/nvOS/bin/pn-scripts/testscript.sh:

Executing Test PN script!


To run the scripts using vREST API, use the following API call:


$ curl -u network-admin:test123  http://pn-lab1/vRest/api-docs/run-pn


       {"apiVersion":"1.0.0","swaggerVersion":"1.2","basePath":"/vRest","resourcePath":"/run-pn","produces":["application/json","application/x-ndjson"],"consumes":["application/json"],"apis":[{"path":"/run-pn/script","operations":[{"method":"POST","summary":"","notes":"","type":"result-list","nickname":"scriptRun","consumes":["application/json"],"parameters":[{"name":"body","required":false,"type":"run-pn-script","paramType":"body","allowMultiple":false}]}]}],"models":{"result":{"id":"result","required":["api.switch-name","scope","status","code"],"properties":{"api.switch-name":{"type":"string"},"scope":{"type":"string","enum":["local","fabric"]},"status":{"type":"string","enum":["Success","Failure"]},"code":{"type":"integer","format":"int32"},"message":{"type":"string"}}},"result-list":{"id":"result-list","required":["status","result"],"properties":{"status":{"type":"string","enum":["Success","Failure"]},"result":{"type":"array","items":{"$ref":"result"}}}},"run-pn-script":{"id":"run-pn-script","description":"Run PN script","required":["name"],"properties":{"name":{"type":"string","description":"desc=Script to execute:pattern=^[a-zA-Z0-9_.:-]+$:pattern-help=letters, numbers, _, ., :, and -"}}}}}


$ curl -u network-admin:test123  http://pn-lab1/vRest/pn-script


       {"data":[{}],"result":{"status":"Success","result":[{"api.switch-name":"local","scope":"local","status":"Success","code":0,"message":"/opt/nvOS/bin/pn-scripts/:\ntestscript.sh\n\n/usr/bin/pn-scripts/:\ntestscript.sh\ntest-usr.sh\n"}]}}


$ curl -u network-admin:test123 -X POST http://pn-lab1/vRest/pn-script/run -d '{"name":"testscript.sh"}' -H "Content-Type: application/json"


       {"result":{"status":"Success","result":[{"api.switch-name":"local","scope":"local","status":"Success","code":0,"message":"Executing /opt/nvOS/bin/pn-scripts/testscript.sh:\nExecuting Test PN script!\n"}]}}