Class: CiscoNxapi::NxapiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/cisco_nxapi/cisco_nxapi.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • value

    the value to set the attribute cache_auto to.



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

Returns:

  • (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

Returns:

  • (Boolean)


122
123
124
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 122

def cache_enable?
  @cache_enable
end

#cache_flushObject

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.

Parameters:

  • commands (String, Array<String>)

    either of: 1) The configuration sequence, as a newline-separated string 2) An array of command strings (one command per string, no newlines)

Raises:



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().

Parameters:

  • command (String)

    the exec command to execute

Returns:

  • (String, nil)

    the body of the output of the exec command (if any)



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

#inspectObject



114
115
116
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 114

def inspect
  "<NxapiClient of #{@http.address}>"
end

#reloadObject



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.

Parameters:

  • command (String)

    the show command to execute

  • type (:ascii, :structured) (defaults to: :ascii)

    ASCII or structured output. Default is :ascii

Returns:

  • (String)

    the output of the show command, if type == :ascii

  • (Hash{String=>String})

    key-value pairs, if type == :structured

Raises:



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_sObject



110
111
112
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 110

def to_s
  @http.address
end