Skip to content

Commit

Permalink
done various jnpr examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dgjnpr committed Apr 4, 2017
1 parent 04dad47 commit 5dc0d40
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 152 deletions.
84 changes: 41 additions & 43 deletions examples/jnpr/edit_config_text_std.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

puts "NETCONF v.#{Netconf::VERSION}"

login = { :target => 'vsrx', :username => "jeremy", :password => "jeremy1" }

new_host_name = "vsrx"
login = {
target: 'vsrx',
username: 'jeremy',
password: 'jeremy1'
}

new_host_name = 'vsrx'

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

puts "Connecting to device: #{login[:target]}"
Netconf::SSH.new(login) do |dev|
puts 'Connected!'

Netconf::SSH.new( login ){ |dev|
puts "Connected!"

target = 'candidate'

location = Nokogiri::XML::Builder.new{ |x| x.send(:'configuration-text', <<-EOCONF

location = Nokogiri::XML::Builder.new do |x|
x.send(:'configuration-text', <<-EOCONF
system {
location {
building "Main Campus, ABC123"
Expand All @@ -22,47 +27,40 @@
}
}
EOCONF
)}

)
end

begin

rsp = dev.rpc.lock target
dev.rpc.lock target

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

rsp = dev.rpc.edit_config(:toplevel => 'config-text'){
|x| x.send(:'configuration-text', <<EOCONF
dev.rpc.edit_config(toplevel: 'config-text') do |x|
x.send(:'configuration-text', <<EOCONF
system {
host-name #{new_host_name};
}
EOCONF
)}

)
end
# --------------------------------------------------------------------
# configuration as PARAM

rsp = dev.rpc.edit_config( location, :toplevel => 'config-text' )

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

rescue Netconf::LockError => e
puts "Lock error"
rescue Netconf::EditError => e
puts "Edit error"
rescue Netconf::ValidateError => e
puts "Validate error"
rescue Netconf::CommitError => e
puts "Commit error"
rescue Netconf::RpcError => e
puts "General RPC error"
else
puts "Configuration Committed."
end
}


dev.rpc.edit_config(location, toplevel: 'config-text')

dev.rpc.validate target
dev.rpc.commit
dev.rpc.unlock target
rescue Netconf::LockError
puts 'Lock error'
rescue Netconf::EditError
puts 'Edit error'
rescue Netconf::ValidateError
puts 'Validate error'
rescue Netconf::CommitError
puts 'Commit error'
rescue Netconf::RpcError
puts 'General RPC error'
else
puts 'Configuration Committed.'
end
end
85 changes: 45 additions & 40 deletions examples/jnpr/get_config_jnpr.rb
Original file line number Diff line number Diff line change
@@ -1,62 +1,67 @@
require 'net/netconf/jnpr' # note: including Juniper specific extension
require 'net/netconf/jnpr'

puts "NETCONF v.#{Netconf::VERSION}"

login = { :target => 'ex4', :username => "jeremy", :password => "jeremy1" }

puts "Connecting to device: #{login[:target]}"
login = {
target: 'ex4',
username: 'jeremy',
password: 'jeremy1'
}

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

Netconf::SSH.new(login) do |dev|
puts 'Connected.'

Netconf::SSH.new( login ){ |dev|
puts "Connected."

puts "Retrieving full config, please wait ... "
cfgall = dev.rpc.get_configuration # Junos specific RPC
puts 'Retrieving full config, please wait ... '
# Junos specific RPC
cfgall = dev.rpc.get_configuration
puts "Showing 'system' hierarchy ..."
puts cfgall.xpath('system') # Root is <configuration>, so don't need to include it in XPath
# Root is <configuration>, so don't need to include it in XPath
puts cfgall.xpath('system')

# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# specifying a filter as a block to get_configuration
# Junos extension does the proper toplevel wrapping

puts "Retrieved services from BLOCK, as XML:"
cfgsvc1_1 = dev.rpc.get_configuration{ |x|
puts 'Retrieved services from BLOCK, as XML:'

cfgsvc1 = dev.rpc.get_configuration do |x|
x.system { x.services }
x.system { x.login }
}

cfgsvc1_1.xpath('system/services/*').each{|s| puts s.name }
end

puts "Retrieved services from BLOCK, as TEXT:"

cfgsvc1_2 = dev.rpc.get_configuration( :format => 'text' ){ |x|
cfgsvc1.xpath('system/services/*').each { |s| puts s.name }

puts 'Retrieved services from BLOCK, as TEXT:'

cfgsvc2 = dev.rpc.get_configuration(format: 'text') do |x|
x.system { x.services }
}
puts cfgsvc1_2.text
end

puts cfgsvc2.text

# ----------------------------------------------------------------------
# specifying a filter as a parameter to get_configuration
# you must wrap the config in a toplevel <configuration> element

filter = Nokogiri::XML::Builder.new{ |x| x.configuration {
x.system { x.services }
x.system { x.login }
}}

puts "Retrieved services by PARAM, as XML"

cfgsvc2 = dev.rpc.get_configuration( filter )
cfgsvc2.xpath('system/services/*').each{|s| puts s.name }
filter = Nokogiri::XML::Builder.new do |x|
x.configuration do
x.system { x.services }
x.system { x.login }
end
end

puts 'Retrieved services by PARAM, as XML'

cfgsvc3 = dev.rpc.get_configuration(filter)
cfgsvc3.xpath('system/services/*').each { |s| puts s.name }

# ----------------------------------------------------------------------
# specifying a filter as a parameter to get_configuration,
# get response back in "text" format

puts "Retrieved services by PARAM, as TEXT:"
cfgsvc3 = dev.rpc.get_configuration( filter, :format => 'text' )
puts cfgsvc3.text
}



puts 'Retrieved services by PARAM, as TEXT:'
cfgsvc4 = dev.rpc.get_configuration(filter, format: 'text')
puts cfgsvc4.text
end
29 changes: 16 additions & 13 deletions examples/jnpr/get_config_matching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

puts "NETCONF v.#{Netconf::VERSION}"

login = { :target => 'vsrx', :username => "jeremy", :password => "jeremy1" }
login = {
target: 'vsrx',
username: 'jeremy',
password: 'jeremy1'
}

Netconf::SSH.new(login) do |dev|
configs = dev.rpc.get_configuration do |x|
x.interfaces(matching: 'interface[name="ge-*"]')
end

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

configs = dev.rpc.get_configuration{ |x|
x.interfaces( :matching => 'interface[name="ge-*"]' )
}

ge_cfgs = configs.xpath('interfaces/interface')

puts "There are #{ge_cfgs.count} GE interfaces:"
ge_cfgs.each{|ifd|
units = ifd.xpath('unit').count
puts " " + ifd.xpath('name')[0].text + " with #{units} unit" + ((units>1) ? "s" : '')
}
}
ge_cfgs.each do |ifd|
units = ifd.xpath('unit').count
puts ' ' + ifd.xpath('name')[0].text + " with #{units} unit" + (units > 1) ? 's' : ''
end
end
68 changes: 35 additions & 33 deletions examples/jnpr/get_config_std.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,50 @@

puts "NETCONF v.#{Netconf::VERSION}"

login = { :target => 'vsrx', :username => "jeremy", :password => "jeremy1" }

puts "Connecting to device: #{login[:target]}"
login = {
target: 'vsrx',
username: 'jeremy',
password: 'jeremy1'
}

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

Netconf::SSH.new( login ){ |dev|
puts "Connected."
Netconf::SSH.new(login) do |dev|
puts 'Connected.'

# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# retrieve the full config. Default source is 'running'
# Alternatively you can pass the source name as a string parameter
# Alternatively you can pass the source name as a string parameter
# to #get_config
puts "Retrieving full config, please wait ... "
cfgall = dev.rpc.get_config

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

# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# specifying a filter as a block to get_config
cfgsvc1 = dev.rpc.get_config{ |x|
x.configuration { x.system { x.services }}
}
puts "Retrieved services as BLOCK:"
cfgsvc1.xpath('//services/*').each{|s| puts s.name }

cfgsvc1 = dev.rpc.get_config do |x|
x.configuration { x.system { x.services } }
end

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

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

filter = Nokogiri::XML::Builder.new{ |x|
x.configuration { x.system { x.services }}
}

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

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

filter = Nokogiri::XML::Builder.new do |x|
x.configuration { x.system { x.services } }
end

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

cfgsvc3 = dev.rpc.get_config(filter)
puts 'Retrieved services as PARAM, re-used filter'
cfgsvc3.xpath('//services/*').each { |s| puts s.name }
end
18 changes: 9 additions & 9 deletions examples/jnpr/get_inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

puts "NETCONF v#{Netconf::VERSION}"

login = { :target => 'vsrx', :username => "jeremy", :password => "jeremy1" }

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

inv = dev.rpc.get_chassis_inventory

puts "Chassis: " + inv.xpath('chassis/description').text
puts "Chassis Serial-Number: " + inv.xpath('chassis/serial-number').text
login = {
target: 'vsrx',
username: 'jeremy',
password: 'jeremy1'
}

Netconf::SSH.new(login) do |dev|
inv = dev.rpc.get_chassis_inventory


puts 'Chassis: ' + inv.xpath('chassis/description').text
puts 'Chassis Serial-Number: ' + inv.xpath('chassis/serial-number').text
end
26 changes: 12 additions & 14 deletions examples/jnpr/get_inventory_serial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

serial_port = '/dev/ttyS4'

login = { :port => serial_port,
:username => "jeremy", :password => "jeremy1" }
login = {
port: serial_port,
username: 'jeremy',
password: 'jeremy1'
}

puts "Connecting to SERIAL: #{serial_port} ... please wait."

Netconf::Serial.new( login ){ |dev|

puts "Connected."
puts "Nabbing Inventory ..."

inv = dev.rpc.get_chassis_inventory

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

}

Netconf::Serial.new(login) do |dev|
puts 'Connected.'
puts 'Nabbing Inventory ...'

inv = dev.rpc.get_chassis_inventory

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

0 comments on commit 5dc0d40

Please sign in to comment.