How do I define a NETCONF RPC operation that executes a shell script?

Similar to the $(CONFD_DIR)/examples.confd/intro/7-action example that show you how to define an action in your YANG model that executes a pearl script, you can define a NETCONF RPC operation in your YANG model that use the tailf:exec YANG extension to execute a shell script.

YANG model example that implements an RPC which executes a simple ping shell script:

module config {
  namespace "http://tail-f.com/ns/example/ping";
  prefix config;

  import tailf-common {
    prefix tailf;
  }
  rpc ping {
    tailf:exec "./ping.sh" {
      tailf:args "-c $(context)";
      tailf:wd ".";
    }
    input {
      leaf count {
	type uint32;
	default "3";
      }
      leaf host {
	type string;
	default "localhost";
      }
    } 
    output {
      leaf ping-result {
	type string;
      }
    }
  }
}

Now you place a shell script called ping.sh in the same working directory:

#!/bin/sh

context=$2
i=0
for a in "$@" ; do
  	if [ $i = 5 ]; then
		host=$a
  	fi
    if [ $i = 3 ]; then
    	count=$a
    fi
    i=`expr $i + 1`
done

if ping -q -c $count $host >/dev/null
then
    result_ping="was successful"
else
    result_ping="failed"
fi
mesg="Ping to host ${host} count ${count} ${result_ping}."

echo "ping-result 'Invoked from $2. $mesg'"

We use the netconf-console tool that ships with ConfD to test the RPC:

$ netconf-console cmd-invoke-ping.xml

The cmd-invoke-ping.xml content where we implement our RPC:

<?xml version="1.0" encoding="UTF-8"?>
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <capabilities>
    <capability>urn:ietf:params:netconf:base:1.0</capability>
  </capabilities>
</hello>
]]>]]>
<?xml version="1.0" encoding="UTF-8"?>
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1">
  <ping xmlns="http://tail-f.com/ns/example/ping">
    <count>5</count>
  </ping>
</rpc>
]]>]]>
<?xml version="1.0" encoding="UTF-8"?>
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="2">
  <close-session/>
</rpc>
]]>]]>

The resulting NETCONF RPC that we sent will look like this:

<rpc message-id="1" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <ping xmlns="http://tail-f.com/ns/example/ping">
    <count>5</count>
  </ping>
</rpc>

And the reply:

<rpc-reply message-id="1" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <ping-result xmlns="http://tail-f.com/ns/example/ping">Invoked from netconf. Ping to host localhost count 5 was successful.</ping-result>
</rpc-reply>
1 Like