Configuring Juniper routers with PHP NETCONF

1.5k Views Asked by At

I'm using the PHP library for NETCONF: https://github.com/Juniper/netconf-php . So far I have managed to get the configuration parts I need for my scripts, but the recent challenge remains loading new configurations into the router. The logs on my router:

Nov 27 14:34:48  router.nl sshd[78164]: subsystem request for netconf by user user
Nov 27 14:34:48  router.nl mgd[78168]: UI_CMDLINE_READ_LINE: User 'x', command 'xml-mode netconf need-trailer '
Nov 27 14:34:48  router.nl file[78167]: UI_LOGIN_EVENT: User 'x' login, class 'j-super-user' [78167], ssh-connection 'x.x.x.x 46796 x.x.x.x 22', client-mode 'netconf'
Nov 27 14:34:48  router.nl file[78167]: UI_NETCONF_CMD: User 'x' used NETCONF client to run command 'lock cannot reconstruct arguments'
Nov 27 14:34:48  router.nl file[78167]: UI_NETCONF_CMD: User 'x' used NETCONF client to run command 'edit-config cannot reconstruct arguments default-operation=merge cannot reconstruct arguments'
Nov 27 14:34:48  router.nl file[78167]: UI_NETCONF_CMD: User 'x' used NETCONF client to run command 'unlock cannot reconstruct arguments'
Nov 27 14:34:48  router.nl file[78167]: UI_NETCONF_CMD: User 'x' used NETCONF client to run command 'close-session'

It keeps complaining about not being able to reconstruct arguments. This is the script I'm trying to get working:

require_once '../app/include/netconf/Device.php';

$deviceParams = [
    'hostname'  => 'x.x.x.x',
    'username'  => 'x',
    'password'  => 'password',
    'port'      => 22
    ];

$this->device = new Device($deviceParams);
$this->connectRouter();

$islocked = $this->device->lock_config();

$command = '<system><services><ftp/></services></system>';

if($islocked) {              
    $this->device->load_xml_configuration($command, 'merge');
}

$this->device->unlock_config();

$this->device->close();

I've set up traceoqptions under the netconf { ssh { tree and this is one of the outputs (everything seems ok):

Nov 30 10:41:57 [86546] Incoming:
<rpc><edit-config><target><candidate/></target><default-operation>merge</default-operation><config><configuration><system><services><ftp></ftp></services></system></configuration></config></edit-config></rpc>]]>]]>


Nov 30 10:41:57 [86546] Outgoing: <rpc-reply     xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/12.3R8/junos">
Nov 30 10:41:57 [86546] Outgoing: <ok/>
Nov 30 10:41:57 [86546] Outgoing: </rpc-reply>
Nov 30 10:41:57 [86546] Outgoing: ]]>]]>
Nov 30 10:41:57 [86546] Incoming: <rpc><close-session/></rpc>]]>]]>
2

There are 2 best solutions below

0
On BEST ANSWER

The first logs I have shown in my OP do not say anything but telling me that something went wrong. The traceoptions however, did tell me important information after I tried locking/unlocking the configuration:

Nov 30 12:06:43 [86767] Outgoing: <rpc-error>
Nov 30 12:06:43 [86767] Outgoing: <error-type>protocol</error-type>
Nov 30 12:06:43 [86767] Outgoing: <error-tag>operation-failed</error-tag>
Nov 30 12:06:43 [86767] Outgoing: <error-severity>error</error-severity>
Nov 30 12:06:43 [86767] Outgoing: <error-message>
Nov 30 12:06:43 [86767] Outgoing: configuration database modified
Nov 30 12:06:43 [86767] Outgoing: </error-message>
Nov 30 12:06:43 [86767] Outgoing: </rpc-error>
Nov 30 12:06:43 [86767] Outgoing: </rpc-reply>
Nov 30 12:06:43 [86767] Outgoing: ]]>]]>

A quick search on 'configuration database modified' implies that there were still some uncommitted changes ( http://www.juniper.net/documentation/en_US/junos13.3/topics/topic-map/junos-script-automation-service-template-automation.html , bottom page ):

Problem

You see the following message when creating, updating, or deleting a service on a device through a NETCONF session:

<output>
    configuration database modified
</output>

The configuration has previously uncommitted changes, and the service script cannot commit the service configuration changes.

Simply typing 'commit' in the configuration mode made the error go away. I hope I have helped / will help others in the future.

1
On

Not sure about netconf in PHP (still diving in) but in Python you should always preferably use 'configure private':

junos_dev.open()
with Config(junos_dev, mode='private') as cu:
  cu.load(some_string, format='set')
  #print cu.diff()
  cu.commit()
junos_dev.close()

And now I also found relevant function here: /** *This method should be called for load operations to happen in 'private' mode. *@param mode * Mode in which to open the configuration. * Permissible mode(s) : "private" */ public function open_configuration($mode)