Skip to content

Scripts and exit codes ​

The same commands run without a prompt:

Shell
nnc router1 -c 'get-config running' > running.xml
nnc router1 -c 'modules' -c 'device cli --command "show version"'
nnc router1 --script checks.nnc
nnc --script session.nnc         # the script does its own connect
echo 'value @systemName' | nnc router1
  • Given a target, nnc connects first. --read-only, --framed and --raw after the target apply to that connection.
  • -c can be repeated. --script can be shortened to -s; --script - reads standard input.
  • Results go to standard output. Errors, warnings, notifications and traces go to standard error. Errors name the script and line: checks.nnc:3: ….
  • A script stops at its first failed command. Add set on-error continue to run the rest; the exit code is then that of the first failure.
  • Colour and paging are off when the output isn't a terminal.

Exit codes ​

CodeMeaning
0every command succeeded
1the device returned an <rpc-error>
2a command couldn't be parsed or was used wrongly
3connection, authentication or host-key failure
4refused by the read-only guard
5a timeout, a protocol error, or the session closed unexpectedly
130cancelled with Ctrl-C

Example: a nightly check ​

Shell
#!/bin/sh
# checks.nnc: fails the job if the device or its profile misbehaves
cat > checks.nnc <<'EOF'
capabilities
value @systemName
read /ietf-interfaces:interfaces --state > interfaces.xml
EOF
nnc router1 --read-only --script checks.nnc

Released under the MIT Licence.