Class: CiscoNxapi::NxapiClient
- Inherits:
-
Object
- Object
- CiscoNxapi::NxapiClient
- Defined in:
- lib/cisco_nxapi/cisco_nxapi.rb
Instance Attribute Summary collapse
-
#cache_auto ⇒ Object
writeonly
Sets the attribute cache_auto.
Instance Method Summary collapse
- #cache_auto? ⇒ Boolean
- #cache_enable=(enable) ⇒ Object
- #cache_enable? ⇒ Boolean
-
#cache_flush ⇒ Object
Clear the cache of CLI output results.
-
#config(commands) ⇒ Object
Configure the given command(s) on the device.
-
#exec(command) ⇒ String?
Executes a command in exec mode on the device.
-
#initialize(address = nil, username = nil, password = nil) ⇒ NxapiClient
constructor
Constructor for NxapiClient.
- #inspect ⇒ Object
- #reload ⇒ Object
-
#show(command, type = :ascii) ⇒ String, Hash{String=>String}
Executes a “show” command on the device, returning either ASCII or structured output.
- #to_s ⇒ Object
Constructor Details
#initialize(address = nil, username = nil, password = nil) ⇒ NxapiClient
Constructor for NxapiClient. By default this connects to the local unix domain socket. If you need to connect to a remote device, you must provide the address/username/password parameters.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 73 def initialize(address = nil, username = nil, password = nil) # Default: connect to unix domain socket on localhost, if available if address.nil? if File.socket?(NXAPI_UDS) # net_http_unix provides NetX::HTTPUnix, a small subclass of Net::HTTP # which supports connection to local unix domain sockets. We need this # in order to run natively under NX-OS but it's not needed for off-box # unit testing where the base Net::HTTP will meet our needs. require 'net_http_unix' @http = NetX::HTTPUnix.new('unix://' + NXAPI_UDS) else fail "No address specified but no UDS found at #{NXAPI_UDS} either" end else fail TypeError, 'invalid address' unless address.is_a?(String) fail ArgumentError, 'empty address' if address.empty? # Remote connection. This is primarily expected # when running e.g. from a Unix server as part of Minitest. @http = Net::HTTP.new(address) # In this case, a username and password are mandatory fail TypeError if username.nil? || password.nil? end unless username.nil? fail TypeError, 'invalid username' unless username.is_a?(String) fail ArgumentError, 'empty username' unless username.length > 0 end unless password.nil? fail TypeError, 'invalid password' unless password.is_a?(String) fail ArgumentError, 'empty password' unless password.length > 0 end @username = username @password = password @cache_enable = true @cache_auto = true cache_flush end |
Instance Attribute Details
#cache_auto=(value) ⇒ Object (writeonly)
Sets the attribute cache_auto
135 136 137 |
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 135 def cache_auto=(value) @cache_auto = value end |
Instance Method Details
#cache_auto? ⇒ Boolean
131 132 133 |
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 131 def cache_auto? @cache_auto end |
#cache_enable=(enable) ⇒ Object
126 127 128 129 |
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 126 def cache_enable=(enable) @cache_enable = enable cache_flush unless enable end |
#cache_enable? ⇒ Boolean
122 123 124 |
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 122 def cache_enable? @cache_enable end |
#cache_flush ⇒ Object
Clear the cache of CLI output results.
If cache_auto is true (default) then this will be performed automatically whenever a config() or exec() is called, but providers may also call this to explicitly force the cache to be cleared.
142 143 144 145 146 147 148 |
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 142 def cache_flush @cache_hash = { 'cli_conf' => {}, 'cli_show' => {}, 'cli_show_ascii' => {} } end |
#config(commands) ⇒ Object
Configure the given command(s) on the device.
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 157 def config(commands) cache_flush if cache_auto? if commands.is_a?(String) commands = commands.split(/\n/) elsif !commands.is_a?(Array) fail TypeError end req('cli_conf', commands) end |
#exec(command) ⇒ String?
Executes a command in exec mode on the device.
If cache_auto? (on by default) is set then the CLI cache will be flushed.
For “show” commands please use show() instead of exec().
177 178 179 180 |
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 177 def exec(command) cache_flush if cache_auto? req('cli_show_ascii', command) end |
#inspect ⇒ Object
114 115 116 |
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 114 def inspect "<NxapiClient of #{@http.address}>" end |
#reload ⇒ Object
118 119 120 |
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 118 def reload # no-op for now end |
#show(command, type = :ascii) ⇒ String, Hash{String=>String}
Executes a “show” command on the device, returning either ASCII or structured output.
Unlike config() and exec() this will not clear the CLI cache; multiple calls to the same “show” command may return cached data rather than querying the device repeatedly.
198 199 200 201 202 203 204 205 206 |
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 198 def show(command, type = :ascii) if type == :ascii return req('cli_show_ascii', command) elsif type == :structured return req('cli_show', command) else fail TypeError end end |
#to_s ⇒ Object
110 111 112 |
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 110 def to_s @http.address end |