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
109
110
111
112
113
# 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
  # The default read time out is 60 seconds, which may be too short for
  # scaled configuration to apply. Change it to 300 seconds, which is
  # also used as the default config by firefox.
  @http.read_timeout = 300

  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.



140
141
142
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 140

def cache_auto=(value)
  @cache_auto = value
end

Instance Method Details

#cache_auto?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 136

def cache_auto?
  @cache_auto
end

#cache_enable=(enable) ⇒ Object



131
132
133
134
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 131

def cache_enable=(enable)
  @cache_enable = enable
  cache_flush unless enable
end

#cache_enable?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 127

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.



147
148
149
150
151
152
153
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 147

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:



162
163
164
165
166
167
168
169
170
171
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 162

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)



182
183
184
185
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 182

def exec(command)
  cache_flush if cache_auto?
  req('cli_show_ascii', command)
end

#inspectObject



119
120
121
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 119

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

#reloadObject



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

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:



203
204
205
206
207
208
209
210
211
# File 'lib/cisco_nxapi/cisco_nxapi.rb', line 203

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



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

def to_s
  @http.address
end