Juniper/net-netconf

Name: net-netconf

Owner: Juniper Networks

Description: A Ruby gem for NETCONF

Created: 2013-01-06 23:59:45.0

Updated: 2018-02-11 23:31:04.0

Pushed: 2017-04-10 08:54:52.0

Homepage: null

Size: 287

Language: Ruby

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Netconf

Gem Version Dependency Status Build Status Code Climate Test Coverage

Description

Device management using the NETCONF protocol as specified in RFC4741 and RFC6241.

Features
Synopsis
ire 'net/netconf'

eate the options hash for the new NETCONF session. If you are
ing ssh-agent, then omit the :password

n = { target: 'vsrx', username: 'root', password: 'Amnesiac' }

ovide a block and the session will open, execute, and close

onf::SSH.new( login ){ |dev|

perform the RPC command:
<rpc>
   <get-chassis-inventory/>
</rpc>

v = dev.rpc.get_chassis_inventory

The response is in Nokogiri XML format for easy processing ...

ts 'Chassis: ' + inv.xpath('chassis/description').text
ts 'Chassis Serial-Number: ' + inv.xpath('chassis/serial-number').text

Alternative explicity open, execute RPCs, and close

ire 'net/netconf'

n = { target: 'vsrx', username: 'root', password: 'Amnesiac' }

= Netconf::SSH.new(login)
open

= dev.rpc.get_chassis_inventory

 'Chassis: ' + inv.xpath('chassis/description').text
 'Chassis Serial-Number: ' + inv.xpath('chassis/serial-number').text

close
Using Netconf
Remote Procedure Calls (RPCs)

Each Netconf session provides a readable instance variable - rpc. This is used to execute Remote Procedure Calls (RPCs). The @rpc will include the NETCONF standard RPCs, any vendor specific extension, as well as the ability to metaprogram new onces via method_missing.

Here are some examples to illustrate the metaprogamming:

Without any parameters, the RPC is created by swapping underscores (_) to hyphens (-):

ire 'net/netconf'

rpc.get_chassis_inventory

pc>
 <get-chassis-inventory/>
rpc>

You can optionally provide RPC parameters as a hash:

rpc.get_interface_information(interface_name: 'ge-0/0/0', terse: true )

pc>
 <get-interface-information>
    <interface-name>ge-0/0/0</interface-name>
    <terse/>
</get-interface-information>
rpc>

You can additionally supply attributes that get assigned to the toplevel element. In this case You must enclose the parameters hash to disambiquate it from the attributes hash, or declare a variable for the parameters hash.

rpc.get_interface_information({interface_name: 'ge-0/0/0', terse: true }, { format: 'text'})

pc>
 <get-interface-information format='text'>
    <interface-name>ge-0/0/0</interface-name>
    <terse/>
</get-interface-information>
rpc>

If you want to provide attributes, but no parameters, then:

rpc.get_chassis_inventory(nil, format: 'text')

pc>
 <get-chassis-inventory format='text'/>
rpc>
Retrieving Configuration

To retrieve configuration from a device, use the get-config RPC. Here is an example, but you can find others in the examples directory:

ire 'net/netconf'

n = { target: 'vsrx', username: 'root', password: 'Amnesia' }

 "Connecting to device: #{login[:target]}"

onf::SSH.new(login) do |dev|
ts 'Connected.'

----------------------------------------------------------------------
retrieve the full config.  Default source is 'running'
Alternatively you can pass the source name as a string parameter
to #get_config

ts 'Retrieving full config, please wait ... '
gall = dev.rpc.get_config
ts "Showing 'system' hierarchy ..."
ts cfgall.xpath('configuration/system')     # JUNOS toplevel config element is <configuration>

----------------------------------------------------------------------
specifying a filter as a block to get_config

gsvc1 = dev.rpc.get_config do |x|
.configuration { x.system { x.services } }
d

ts 'Retrieved services as BLOCK:'
gsvc1.xpath('//services/*').each { |s| puts s.name }

----------------------------------------------------------------------
specifying a filter as a parameter to get_config

lter = Nokogiri::XML::Builder.new do |x|
.configuration { x.system { x.services } }
d

gsvc2 = dev.rpc.get_config(filter)
ts 'Retrieved services as PARAM:'
gsvc2.xpath('//services/*').each { |s| puts s.name }

gsvc3 = dev.rpc.get_config(filter)
ts 'Retrieved services as PARAM, re-used filter'
gsvc3.xpath('//services/*').each { |s| puts s.name }

NOTE: There is a JUNOS RPC, get-configuration, that provides Juniper specific extensions as well.

Changing Configuration

To retrieve configuration from a device, use the edit-config RPC. Here is an example, but you can find others in the examples directory:

ire 'net/netconf'

n = { target: 'vsrx', username: 'root', password: 'Amnesia' }

host_name = 'vsrx-abc'

 "Connecting to device: #{login[:target]}"

onf::SSH.new(login) do |dev|
ts 'Connected!'

rget = 'candidate'

JUNOS toplevel element is 'configuration'

cation = Nokogiri::XML::Builder.new do |x|
x.configuration {
  x.system {
    x.location {
      x.building 'Main Campus, A'
      x.floor 5
      x.rack 27
    }
  }
}
d

gin
rsp = dev.rpc.lock target

# --------------------------------------------------------------------
# configuration as BLOCK

rsp = dev.rpc.edit_config do |x|
  x.configuration {
    x.system {
      x.send(:'host-name', new_host_name )
    }
  }
end

# --------------------------------------------------------------------
# configuration as PARAM

rsp = dev.rpc.edit_config(location)

rsp = dev.rpc.validate target
rpc = dev.rpc.commit
rpc = dev.rpc.unlock target

scue Netconf::LockError => e
puts 'Lock error'
scue Netconf::EditError => e
puts 'Edit error'
scue Netconf::ValidateError => e
puts 'Validate error'
scue Netconf::CommitError => e
puts 'Commit error'
scue Netconf::RpcError => e
puts 'General RPC error'
se
puts 'Configuration Committed.'
d

NOTE: There is a JUNOS RPC, load-configuration, that provides Juniper specific extensions as well.

Authors and Contributors

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.