netconf 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/jnpr/edit-config-jnpr-set.rb +55 -0
- data/examples/jnpr/edit-config-jnpr-text.rb +64 -0
- data/examples/jnpr/edit-config-jnpr.rb +62 -0
- data/examples/jnpr/edit-config-std.rb +65 -0
- data/examples/jnpr/edit-config-text-std.rb +69 -0
- data/examples/jnpr/get-config-jnpr.rb +59 -0
- data/examples/jnpr/get-config-matching.rb +20 -0
- data/examples/jnpr/get-config-std.rb +49 -0
- data/examples/jnpr/get-inventory-serial-explicit.rb +29 -0
- data/examples/jnpr/get-inventory-serial.rb +29 -0
- data/examples/jnpr/get-inventory-telnet.rb +15 -0
- data/examples/jnpr/get-inventory.rb +19 -0
- data/examples/jnpr/scp.rb +22 -0
- data/lib/net/netconf.rb +14 -0
- data/lib/net/netconf/exception.rb +38 -0
- data/lib/net/netconf/jnpr.rb +8 -0
- data/lib/net/netconf/jnpr/rpc.rb +134 -0
- data/lib/net/netconf/jnpr/serial.rb +17 -0
- data/lib/net/netconf/jnpr/telnet.rb +23 -0
- data/lib/net/netconf/localhost.rb +9 -0
- data/lib/net/netconf/rpc.rb +71 -0
- data/lib/net/netconf/rpc_std.rb +137 -0
- data/lib/net/netconf/serial.rb +132 -0
- data/lib/net/netconf/ssh.rb +77 -0
- data/lib/net/netconf/telnet.rb +58 -0
- data/lib/net/netconf/transport.rb +113 -0
- metadata +103 -0
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'net/netconf/jnpr'
|
2
|
+
|
3
|
+
puts "NETCONF v.#{Netconf::VERSION}"
|
4
|
+
|
5
|
+
login = { :target => 'vsrx', :username => "jeremy", :password => "jeremy1" }
|
6
|
+
|
7
|
+
new_host_name = "vsrx"
|
8
|
+
|
9
|
+
puts "Connecting to device: #{login[:target]}"
|
10
|
+
|
11
|
+
Netconf::SSH.new( login ){ |dev|
|
12
|
+
puts "Connected!"
|
13
|
+
|
14
|
+
location = []
|
15
|
+
location << 'set system location building "Main Campus, C"'
|
16
|
+
location << "set system location floor 15"
|
17
|
+
location << "set system location rack 1117"
|
18
|
+
|
19
|
+
begin
|
20
|
+
|
21
|
+
rsp = dev.rpc.lock_configuration
|
22
|
+
|
23
|
+
# --------------------------------------------------------------------
|
24
|
+
# configuration as BLOCK
|
25
|
+
|
26
|
+
rsp = dev.rpc.load_configuration( :format => 'set' ) {
|
27
|
+
"set system host-name #{new_host_name}"
|
28
|
+
}
|
29
|
+
|
30
|
+
# --------------------------------------------------------------------
|
31
|
+
# configuration as PARAM
|
32
|
+
|
33
|
+
rsp = dev.rpc.load_configuration( location, :format => 'set' )
|
34
|
+
|
35
|
+
rpc = dev.rpc.check_configuration
|
36
|
+
rpc = dev.rpc.commit_configuration
|
37
|
+
rpc = dev.rpc.unlock_configuration
|
38
|
+
|
39
|
+
rescue Netconf::LockError => e
|
40
|
+
puts "Lock error"
|
41
|
+
rescue Netconf::EditError => e
|
42
|
+
puts "Edit error"
|
43
|
+
rescue Netconf::ValidateError => e
|
44
|
+
puts "Validate error"
|
45
|
+
rescue Netconf::CommitError => e
|
46
|
+
puts "Commit error"
|
47
|
+
rescue Netconf::RpcError => e
|
48
|
+
puts "General RPC error"
|
49
|
+
else
|
50
|
+
puts "Configuration Committed."
|
51
|
+
end
|
52
|
+
}
|
53
|
+
|
54
|
+
|
55
|
+
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'net/netconf/jnpr'
|
2
|
+
|
3
|
+
puts "NETCONF v.#{Netconf::VERSION}"
|
4
|
+
|
5
|
+
login = { :target => 'vsrx', :username => "jeremy", :password => "jeremy1" }
|
6
|
+
|
7
|
+
new_host_name = "vsrx-gizmo"
|
8
|
+
|
9
|
+
puts "Connecting to device: #{login[:target]}"
|
10
|
+
|
11
|
+
Netconf::SSH.new( login ){ |dev|
|
12
|
+
puts "Connected!"
|
13
|
+
|
14
|
+
location = <<EOCONF
|
15
|
+
system {
|
16
|
+
location {
|
17
|
+
building "Main Campus, E"
|
18
|
+
floor 15
|
19
|
+
rack 1117
|
20
|
+
}
|
21
|
+
}
|
22
|
+
EOCONF
|
23
|
+
|
24
|
+
begin
|
25
|
+
|
26
|
+
rsp = dev.rpc.lock_configuration
|
27
|
+
|
28
|
+
# --------------------------------------------------------------------
|
29
|
+
# configuration as BLOCK
|
30
|
+
|
31
|
+
rsp = dev.rpc.load_configuration( :format => 'text' ) {
|
32
|
+
<<-EOCONF
|
33
|
+
system {
|
34
|
+
host-name #{new_host_name}
|
35
|
+
}
|
36
|
+
EOCONF
|
37
|
+
}
|
38
|
+
|
39
|
+
# --------------------------------------------------------------------
|
40
|
+
# configuration as PARAM
|
41
|
+
|
42
|
+
rsp = dev.rpc.load_configuration( location, :format => 'text' )
|
43
|
+
|
44
|
+
rpc = dev.rpc.check_configuration
|
45
|
+
rpc = dev.rpc.commit_configuration
|
46
|
+
rpc = dev.rpc.unlock_configuration
|
47
|
+
|
48
|
+
rescue Netconf::LockError => e
|
49
|
+
puts "Lock error"
|
50
|
+
rescue Netconf::EditError => e
|
51
|
+
puts "Edit error"
|
52
|
+
rescue Netconf::ValidateError => e
|
53
|
+
puts "Validate error"
|
54
|
+
rescue Netconf::CommitError => e
|
55
|
+
puts "Commit error"
|
56
|
+
rescue Netconf::RpcError => e
|
57
|
+
puts "General RPC error"
|
58
|
+
else
|
59
|
+
puts "Configuration Committed."
|
60
|
+
end
|
61
|
+
}
|
62
|
+
|
63
|
+
|
64
|
+
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'net/netconf/jnpr'
|
2
|
+
|
3
|
+
puts "NETCONF v.#{Netconf::VERSION}"
|
4
|
+
|
5
|
+
login = { :target => 'vsrx', :username => "jeremy", :password => "jeremy1" }
|
6
|
+
|
7
|
+
new_host_name = "vsrx-jjj"
|
8
|
+
|
9
|
+
puts "Connecting to device: #{login[:target]}"
|
10
|
+
|
11
|
+
Netconf::SSH.new( login ){ |dev|
|
12
|
+
puts "Connected!"
|
13
|
+
|
14
|
+
location = Nokogiri::XML::Builder.new{ |x|
|
15
|
+
x.system {
|
16
|
+
x.location {
|
17
|
+
x.building "Main Campus, C"
|
18
|
+
x.floor 15
|
19
|
+
x.rack 37
|
20
|
+
}
|
21
|
+
}
|
22
|
+
}
|
23
|
+
|
24
|
+
begin
|
25
|
+
|
26
|
+
rsp = dev.rpc.lock_configuration
|
27
|
+
|
28
|
+
# --------------------------------------------------------------------
|
29
|
+
# configuration as BLOCK
|
30
|
+
|
31
|
+
rsp = dev.rpc.load_configuration{ |x|
|
32
|
+
x.system {
|
33
|
+
x.send(:'host-name', new_host_name )
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
# --------------------------------------------------------------------
|
38
|
+
# configuration as PARAM
|
39
|
+
|
40
|
+
rsp = dev.rpc.load_configuration( location )
|
41
|
+
rpc = dev.rpc.check_configuration
|
42
|
+
rpc = dev.rpc.commit_configuration
|
43
|
+
rpc = dev.rpc.unlock_configuration
|
44
|
+
|
45
|
+
rescue Netconf::LockError => e
|
46
|
+
puts "Lock error"
|
47
|
+
rescue Netconf::EditError => e
|
48
|
+
puts "Edit error"
|
49
|
+
rescue Netconf::ValidateError => e
|
50
|
+
puts "Validate error"
|
51
|
+
rescue Netconf::CommitError => e
|
52
|
+
puts "Commit error"
|
53
|
+
rescue Netconf::RpcError => e
|
54
|
+
puts "General RPC error"
|
55
|
+
binding.pry
|
56
|
+
else
|
57
|
+
puts "Configuration Committed."
|
58
|
+
end
|
59
|
+
}
|
60
|
+
|
61
|
+
|
62
|
+
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'net/netconf'
|
2
|
+
|
3
|
+
puts "NETCONF v.#{Netconf::VERSION}"
|
4
|
+
|
5
|
+
login = { :target => 'vsrx', :username => "jeremy", :password => "jeremy1" }
|
6
|
+
|
7
|
+
new_host_name = "vsrx-abc"
|
8
|
+
|
9
|
+
puts "Connecting to device: #{login[:target]}"
|
10
|
+
|
11
|
+
Netconf::SSH.new( login ){ |dev|
|
12
|
+
puts "Connected!"
|
13
|
+
|
14
|
+
target = 'candidate'
|
15
|
+
|
16
|
+
location = Nokogiri::XML::Builder.new{ |x| x.configuration {
|
17
|
+
x.system {
|
18
|
+
x.location {
|
19
|
+
x.building "Main Campus, A"
|
20
|
+
x.floor 5
|
21
|
+
x.rack 27
|
22
|
+
}
|
23
|
+
}
|
24
|
+
}}
|
25
|
+
|
26
|
+
begin
|
27
|
+
|
28
|
+
rsp = dev.rpc.lock target
|
29
|
+
|
30
|
+
# --------------------------------------------------------------------
|
31
|
+
# configuration as BLOCK
|
32
|
+
|
33
|
+
rsp = dev.rpc.edit_config{ |x| x.configuration {
|
34
|
+
x.system {
|
35
|
+
x.send(:'host-name', new_host_name )
|
36
|
+
}
|
37
|
+
}}
|
38
|
+
|
39
|
+
# --------------------------------------------------------------------
|
40
|
+
# configuration as PARAM
|
41
|
+
|
42
|
+
rsp = dev.rpc.edit_config( location )
|
43
|
+
|
44
|
+
rsp = dev.rpc.validate target
|
45
|
+
rpc = dev.rpc.commit
|
46
|
+
rpc = dev.rpc.unlock target
|
47
|
+
|
48
|
+
rescue Netconf::LockError => e
|
49
|
+
puts "Lock error"
|
50
|
+
rescue Netconf::EditError => e
|
51
|
+
puts "Edit error"
|
52
|
+
rescue Netconf::ValidateError => e
|
53
|
+
puts "Validate error"
|
54
|
+
rescue Netconf::CommitError => e
|
55
|
+
puts "Commit error"
|
56
|
+
rescue Netconf::RpcError => e
|
57
|
+
puts "General RPC error"
|
58
|
+
binding.pry
|
59
|
+
else
|
60
|
+
puts "Configuration Committed."
|
61
|
+
end
|
62
|
+
}
|
63
|
+
|
64
|
+
|
65
|
+
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'net/netconf'
|
2
|
+
|
3
|
+
puts "NETCONF v.#{Netconf::VERSION}"
|
4
|
+
|
5
|
+
login = { :target => 'vsrx', :username => "jeremy", :password => "jeremy1" }
|
6
|
+
|
7
|
+
new_host_name = "vsrx"
|
8
|
+
|
9
|
+
puts "Connecting to device: #{login[:target]}"
|
10
|
+
|
11
|
+
Netconf::SSH.new( login ){ |dev|
|
12
|
+
puts "Connected!"
|
13
|
+
|
14
|
+
target = 'candidate'
|
15
|
+
|
16
|
+
location = Nokogiri::XML::Builder.new{ |x| x.send(:'configuration-text', <<-EOCONF
|
17
|
+
system {
|
18
|
+
location {
|
19
|
+
building "Main Campus, ABC123"
|
20
|
+
floor 5
|
21
|
+
rack 27
|
22
|
+
}
|
23
|
+
}
|
24
|
+
EOCONF
|
25
|
+
)}
|
26
|
+
|
27
|
+
|
28
|
+
begin
|
29
|
+
|
30
|
+
rsp = dev.rpc.lock target
|
31
|
+
|
32
|
+
# --------------------------------------------------------------------
|
33
|
+
# configuration as BLOCK
|
34
|
+
|
35
|
+
rsp = dev.rpc.edit_config(:toplevel => 'config-text'){
|
36
|
+
|x| x.send(:'configuration-text', <<EOCONF
|
37
|
+
system {
|
38
|
+
host-name #{new_host_name};
|
39
|
+
}
|
40
|
+
EOCONF
|
41
|
+
)}
|
42
|
+
|
43
|
+
# --------------------------------------------------------------------
|
44
|
+
# configuration as PARAM
|
45
|
+
|
46
|
+
rsp = dev.rpc.edit_config( location, :toplevel => 'config-text' )
|
47
|
+
|
48
|
+
rsp = dev.rpc.validate target
|
49
|
+
rpc = dev.rpc.commit
|
50
|
+
rpc = dev.rpc.unlock target
|
51
|
+
|
52
|
+
rescue Netconf::LockError => e
|
53
|
+
puts "Lock error"
|
54
|
+
rescue Netconf::EditError => e
|
55
|
+
puts "Edit error"
|
56
|
+
rescue Netconf::ValidateError => e
|
57
|
+
puts "Validate error"
|
58
|
+
rescue Netconf::CommitError => e
|
59
|
+
puts "Commit error"
|
60
|
+
rescue Netconf::RpcError => e
|
61
|
+
puts "General RPC error"
|
62
|
+
binding.pry
|
63
|
+
else
|
64
|
+
puts "Configuration Committed."
|
65
|
+
end
|
66
|
+
}
|
67
|
+
|
68
|
+
|
69
|
+
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'net/netconf/jnpr' # note: including Juniper specific extension
|
2
|
+
|
3
|
+
puts "NETCONF v.#{Netconf::VERSION}"
|
4
|
+
|
5
|
+
login = { :target => 'vsrx', :username => "jeremy", :password => "jeremy1" }
|
6
|
+
|
7
|
+
puts "Connecting to device: #{login[:target]}"
|
8
|
+
|
9
|
+
Netconf::SSH.new( login ){ |dev|
|
10
|
+
puts "Connected."
|
11
|
+
|
12
|
+
puts "Retrieving full config, please wait ... "
|
13
|
+
cfgall = dev.rpc.get_configuration # Junos specific RPC
|
14
|
+
puts "Showing 'system' hierarchy ..."
|
15
|
+
puts cfgall.xpath('system') # Root is <configuration>, so don't need to include it in XPath
|
16
|
+
|
17
|
+
# ----------------------------------------------------------------------
|
18
|
+
# specifying a filter as a block to get_configuration
|
19
|
+
# Junos extension does the proper toplevel wrapping
|
20
|
+
|
21
|
+
puts "Retrieved services from BLOCK, as XML:"
|
22
|
+
|
23
|
+
cfgsvc1_1 = dev.rpc.get_configuration{ |x|
|
24
|
+
x.system { x.services }
|
25
|
+
}
|
26
|
+
|
27
|
+
cfgsvc1_1.xpath('system/services/*').each{|s| puts s.name }
|
28
|
+
|
29
|
+
puts "Retrieved services from BLOCK, as TEXT:"
|
30
|
+
|
31
|
+
cfgsvc1_2 = dev.rpc.get_configuration( :format => 'text' ){ |x|
|
32
|
+
x.system { x.services }
|
33
|
+
}
|
34
|
+
|
35
|
+
puts cfgsvc1_2.text
|
36
|
+
|
37
|
+
# ----------------------------------------------------------------------
|
38
|
+
# specifying a filter as a parameter to get_configuration
|
39
|
+
|
40
|
+
filter = Nokogiri::XML::Builder.new{ |x|
|
41
|
+
x.system { x.services }
|
42
|
+
}
|
43
|
+
|
44
|
+
puts "Retrieved services by PARAM, as XML"
|
45
|
+
|
46
|
+
cfgsvc2 = dev.rpc.get_configuration( filter )
|
47
|
+
cfgsvc2.xpath('system/services/*').each{|s| puts s.name }
|
48
|
+
|
49
|
+
# ----------------------------------------------------------------------
|
50
|
+
# specifying a filter as a parameter to get_configuration,
|
51
|
+
# get response back in "text" format
|
52
|
+
|
53
|
+
puts "Retrieved services by PARAM, as TEXT:"
|
54
|
+
cfgsvc3 = dev.rpc.get_configuration( filter, :format => 'text' )
|
55
|
+
puts cfgsvc3.text
|
56
|
+
}
|
57
|
+
|
58
|
+
|
59
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'net/netconf/jnpr'
|
2
|
+
|
3
|
+
puts "NETCONF v.#{Netconf::VERSION}"
|
4
|
+
|
5
|
+
login = { :target => 'vsrx', :username => "jeremy", :password => "jeremy1" }
|
6
|
+
|
7
|
+
Netconf::SSH.new( login ){ |dev|
|
8
|
+
|
9
|
+
configs = dev.rpc.get_configuration{ |x|
|
10
|
+
x.interfaces( :matching => 'interface[name="ge-*"]' )
|
11
|
+
}
|
12
|
+
|
13
|
+
ge_cfgs = configs.xpath('interfaces/interface')
|
14
|
+
|
15
|
+
puts "There are #{ge_cfgs.count} GE interfaces:"
|
16
|
+
ge_cfgs.each{|ifd|
|
17
|
+
units = ifd.xpath('unit').count
|
18
|
+
puts " " + ifd.xpath('name')[0].text + " with #{units} unit" + ((units>1) ? "s" : '')
|
19
|
+
}
|
20
|
+
}
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'net/netconf'
|
2
|
+
|
3
|
+
puts "NETCONF v.#{Netconf::VERSION}"
|
4
|
+
|
5
|
+
login = { :target => 'vsrx', :username => "jeremy", :password => "jeremy1" }
|
6
|
+
|
7
|
+
puts "Connecting to device: #{login[:target]}"
|
8
|
+
|
9
|
+
Netconf::SSH.new( login ){ |dev|
|
10
|
+
puts "Connected."
|
11
|
+
|
12
|
+
# ----------------------------------------------------------------------
|
13
|
+
# retrieve the full config. Default source is 'running'
|
14
|
+
# Alternatively you can pass the source name as a string parameter
|
15
|
+
# to #get_config
|
16
|
+
|
17
|
+
puts "Retrieving full config, please wait ... "
|
18
|
+
cfgall = dev.rpc.get_config
|
19
|
+
puts "Showing 'system' hierarchy ..."
|
20
|
+
puts cfgall.xpath('configuration/system') # JUNOS toplevel config element is <configuration>
|
21
|
+
|
22
|
+
# ----------------------------------------------------------------------
|
23
|
+
# specifying a filter as a block to get_config
|
24
|
+
|
25
|
+
cfgsvc1 = dev.rpc.get_config{ |x|
|
26
|
+
x.configuration { x.system { x.services }}
|
27
|
+
}
|
28
|
+
|
29
|
+
puts "Retrieved services as BLOCK:"
|
30
|
+
cfgsvc1.xpath('//services/*').each{|s| puts s.name }
|
31
|
+
|
32
|
+
# ----------------------------------------------------------------------
|
33
|
+
# specifying a filter as a parameter to get_config
|
34
|
+
|
35
|
+
filter = Nokogiri::XML::Builder.new{ |x|
|
36
|
+
x.configuration { x.system { x.services }}
|
37
|
+
}
|
38
|
+
|
39
|
+
cfgsvc2 = dev.rpc.get_config( filter )
|
40
|
+
puts "Retrieved services as PARAM:"
|
41
|
+
cfgsvc2.xpath('//services/*').each{|s| puts s.name }
|
42
|
+
|
43
|
+
cfgsvc3 = dev.rpc.get_config( filter )
|
44
|
+
puts "Retrieved services as PARAM, re-used filter"
|
45
|
+
cfgsvc3.xpath('//services/*').each{|s| puts s.name }
|
46
|
+
}
|
47
|
+
|
48
|
+
|
49
|
+
|