Skip to content

Instantly share code, notes, and snippets.

@stevie-chambers
Created October 30, 2011 21:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevie-chambers/1326436 to your computer and use it in GitHub Desktop.
Save stevie-chambers/1326436 to your computer and use it in GitHub Desktop.
Basic Ruby interface to the Nexus 1000V
class NX1K
require 'net/ssh'
attr_writer :host, :user, :pwd
def send(xml)
session = Net::SSH.start(@host, @user, :password => @pwd, :timeout => 10, :verbose => :debug) do |session|
channel = session.open_channel do |channel|
channel.subsystem("xmlagent") do |xmlagent, success|
xmlagent.on_data do |xmlagent, data|
puts "(xmlagent) ON_DATA: #{data.inspect}"
xmlagent.close if data === "]]>]]>"
end
xmlagent.on_close do |xmlagent|
puts "(xmlagent) ON_CLOSE"
end
xmlagent.on_eof do |xmlagent|
puts "(xmlagent) ON_EOF"
end
xmlagent.send_data(IO.read('nx1k_hello.xml'))
xmlagent.send_data(xml)
end #xmlagent
end #channel
end #session
end #send
end #class
vsm = NX1K.new
vsm.host = '192.168.1.130'
vsm.user = 'admin'
vsm.pwd = 'VCEr0cks!'
vsm.send(IO.read('nx1k_show_int.xml'))
<?xml version="1.0"?>
<nc:hello xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<nc:capabilities>
<nc:capability>urn:ietf:params:xml:ns:netconf:base:1.0</nc:capability>
</nc:capabilities>
</nc:hello>]]>]]>
<?xml version="1.0"?>
<nf:rpc xmlns:nf="urn:ietf:params:xml:ns:netconf:base:1.0"
xmlns:nxos="http://www.cisco.com/nxos:1.0" message-id="110">
<nxos:exec-command>
<nxos:cmd>show interface brief</nxos:cmd>
</nxos:exec-command>
</nf:rpc>]]>]]>
09:04:13 [~/Programming/Ruby/cherub] $ ruby nx1k.rb
D, [2011-10-30T21:07:02.322845 #8370] DEBUG -- net.ssh.transport.session[81011dd8]: establishing connection to 192.168.1.130:22
D, [2011-10-30T21:07:02.324348 #8370] DEBUG -- net.ssh.transport.session[81011dd8]: connection established
I, [2011-10-30T21:07:02.324556 #8370] INFO -- net.ssh.transport.server_version[81010d20]: negotiating protocol version
D, [2011-10-30T21:07:02.355571 #8370] DEBUG -- net.ssh.transport.server_version[81010d20]: remote is `SSH-2.0-OpenSSH_4.5'
D, [2011-10-30T21:07:02.355690 #8370] DEBUG -- net.ssh.transport.server_version[81010d20]: local is `SSH-2.0-Ruby/Net::SSH_2.2.1 x86_64-darwin10.8.0'
D, [2011-10-30T21:07:02.365020 #8370] DEBUG -- tcpsocket[81011414]: read 736 bytes
D, [2011-10-30T21:07:02.365200 #8370] DEBUG -- tcpsocket[81011414]: received packet nr 0 type 20 len 732
I, [2011-10-30T21:07:02.365287 #8370] INFO -- net.ssh.transport.algorithms[8100f72c]: got KEXINIT from server
I, [2011-10-30T21:07:02.365477 #8370] INFO -- net.ssh.transport.algorithms[8100f72c]: sending KEXINIT
D, [2011-10-30T21:07:02.365711 #8370] DEBUG -- tcpsocket[81011414]: queueing packet nr 0 type 20 len 556
D, [2011-10-30T21:07:02.365839 #8370] DEBUG -- tcpsocket[81011414]: sent 560 bytes
I, [2011-10-30T21:07:02.365888 #8370] INFO -- net.ssh.transport.algorithms[8100f72c]: negotiating algorithms
D, [2011-10-30T21:07:02.366107 #8370] DEBUG -- net.ssh.transport.algorithms[8100f72c]: negotiated:
* kex: diffie-hellman-group-exchange-sha1
* host_key: ssh-rsa
* encryption_server: aes128-cbc
* encryption_client: aes128-cbc
* hmac_client: hmac-sha1
* hmac_server: hmac-sha1
* compression_client: none
* compression_server: none
* language_client:
* language_server:
D, [2011-10-30T21:07:02.366157 #8370] DEBUG -- net.ssh.transport.algorithms[8100f72c]: exchanging keys
D, [2011-10-30T21:07:02.366435 #8370] DEBUG -- tcpsocket[81011414]: queueing packet nr 1 type 34 len 20
D, [2011-10-30T21:07:02.366502 #8370] DEBUG -- tcpsocket[81011414]: sent 24 bytes
D, [2011-10-30T21:07:02.443702 #8370] DEBUG -- tcpsocket[81011414]: read 152 bytes
D, [2011-10-30T21:07:02.443896 #8370] DEBUG -- tcpsocket[81011414]: received packet nr 1 type 31 len 148
D, [2011-10-30T21:07:02.453510 #8370] DEBUG -- tcpsocket[81011414]: queueing packet nr 2 type 32 len 140
D, [2011-10-30T21:07:02.453823 #8370] DEBUG -- tcpsocket[81011414]: sent 144 bytes
D, [2011-10-30T21:07:02.460345 #8370] DEBUG -- tcpsocket[81011414]: read 464 bytes
D, [2011-10-30T21:07:02.460502 #8370] DEBUG -- tcpsocket[81011414]: received packet nr 2 type 33 len 444
D, [2011-10-30T21:07:02.465379 #8370] DEBUG -- tcpsocket[81011414]: queueing packet nr 3 type 21 len 20
D, [2011-10-30T21:07:02.465505 #8370] DEBUG -- tcpsocket[81011414]: sent 24 bytes
D, [2011-10-30T21:07:02.465605 #8370] DEBUG -- tcpsocket[81011414]: received packet nr 3 type 21 len 12
D, [2011-10-30T21:07:02.465989 #8370] DEBUG -- net.ssh.authentication.session[81409508]: beginning authentication of `admin'
D, [2011-10-30T21:07:02.466146 #8370] DEBUG -- tcpsocket[81011414]: queueing packet nr 4 type 5 len 28
D, [2011-10-30T21:07:02.466209 #8370] DEBUG -- tcpsocket[81011414]: sent 52 bytes
D, [2011-10-30T21:07:02.551214 #8370] DEBUG -- tcpsocket[81011414]: read 52 bytes
D, [2011-10-30T21:07:02.551452 #8370] DEBUG -- tcpsocket[81011414]: received packet nr 4 type 6 len 28
D, [2011-10-30T21:07:02.551634 #8370] DEBUG -- net.ssh.authentication.session[81409508]: trying publickey
D, [2011-10-30T21:07:02.551825 #8370] DEBUG -- net.ssh.authentication.agent[8109e878]: connecting to ssh-agent
D, [2011-10-30T21:07:02.552008 #8370] DEBUG -- net.ssh.authentication.agent[8109e878]: sending agent request 1 len 51
D, [2011-10-30T21:07:02.552208 #8370] DEBUG -- net.ssh.authentication.agent[8109e878]: received agent packet 2 len 5
D, [2011-10-30T21:07:02.552307 #8370] DEBUG -- net.ssh.authentication.agent[8109e878]: sending agent request 11 len 0
D, [2011-10-30T21:07:02.552413 #8370] DEBUG -- net.ssh.authentication.agent[8109e878]: received agent packet 12 len 5
D, [2011-10-30T21:07:02.552459 #8370] DEBUG -- net.ssh.authentication.session[81409508]: trying hostbased
D, [2011-10-30T21:07:02.552542 #8370] DEBUG -- net.ssh.authentication.agent[8109e878]: sending agent request 11 len 0
D, [2011-10-30T21:07:02.552637 #8370] DEBUG -- net.ssh.authentication.agent[8109e878]: received agent packet 12 len 5
D, [2011-10-30T21:07:02.552683 #8370] DEBUG -- net.ssh.authentication.session[81409508]: trying password
D, [2011-10-30T21:07:02.552814 #8370] DEBUG -- tcpsocket[81011414]: queueing packet nr 5 type 50 len 60
D, [2011-10-30T21:07:02.552920 #8370] DEBUG -- tcpsocket[81011414]: sent 84 bytes
D, [2011-10-30T21:07:02.584305 #8370] DEBUG -- tcpsocket[81011414]: read 36 bytes
D, [2011-10-30T21:07:02.584550 #8370] DEBUG -- tcpsocket[81011414]: received packet nr 5 type 52 len 12
D, [2011-10-30T21:07:02.584708 #8370] DEBUG -- net.ssh.authentication.methods.password[810992ec]: password succeeded
D, [2011-10-30T21:07:02.585000 #8370] DEBUG -- tcpsocket[81011414]: queueing packet nr 6 type 90 len 44
I, [2011-10-30T21:07:02.585110 #8370] INFO -- net.ssh.connection.session[810967cc]: closing remaining channels (1 open)
D, [2011-10-30T21:07:02.585357 #8370] DEBUG -- tcpsocket[81011414]: sent 68 bytes
D, [2011-10-30T21:07:02.591875 #8370] DEBUG -- tcpsocket[81011414]: read 52 bytes
D, [2011-10-30T21:07:02.592159 #8370] DEBUG -- tcpsocket[81011414]: received packet nr 6 type 91 len 28
I, [2011-10-30T21:07:02.592280 #8370] INFO -- net.ssh.connection.session[810967cc]: channel_open_confirmation: 0 0 0 32768
I, [2011-10-30T21:07:02.592425 #8370] INFO -- net.ssh.connection.channel[81096628]: sending channel request "subsystem"
D, [2011-10-30T21:07:02.592601 #8370] DEBUG -- tcpsocket[81011414]: queueing packet nr 7 type 98 len 44
D, [2011-10-30T21:07:02.592770 #8370] DEBUG -- tcpsocket[81011414]: sent 68 bytes
D, [2011-10-30T21:07:02.652913 #8370] DEBUG -- tcpsocket[81011414]: read 364 bytes
D, [2011-10-30T21:07:02.653223 #8370] DEBUG -- tcpsocket[81011414]: received packet nr 7 type 93 len 28
I, [2011-10-30T21:07:02.653327 #8370] INFO -- net.ssh.connection.session[810967cc]: channel_window_adjust: 0 +131072
D, [2011-10-30T21:07:02.653433 #8370] DEBUG -- tcpsocket[81011414]: received packet nr 8 type 99 len 12
I, [2011-10-30T21:07:02.653517 #8370] INFO -- net.ssh.connection.session[810967cc]: channel_success: 0
D, [2011-10-30T21:07:02.653803 #8370] DEBUG -- tcpsocket[81011414]: received packet nr 9 type 94 len 252
I, [2011-10-30T21:07:02.653900 #8370] INFO -- net.ssh.connection.session[810967cc]: channel_data: 0 229b
(xmlagent) ON_DATA: "<?xml version=\"1.0\"?>\n<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n <capabilities>\n <capability>urn:ietf:params:xml:ns:netconf:base:1.0</capability>\n </capabilities>\n <session-id>3989</session-id>\n</hello>\n]]>]]>"
D, [2011-10-30T21:07:02.654131 #8370] DEBUG -- tcpsocket[81011414]: queueing packet nr 8 type 94 len 492
D, [2011-10-30T21:07:02.654310 #8370] DEBUG -- tcpsocket[81011414]: sent 516 bytes
D, [2011-10-30T21:07:02.752237 #8370] DEBUG -- tcpsocket[81011414]: read 884 bytes
D, [2011-10-30T21:07:02.752419 #8370] DEBUG -- tcpsocket[81011414]: read 52 bytes
D, [2011-10-30T21:07:02.752611 #8370] DEBUG -- tcpsocket[81011414]: received packet nr 10 type 94 len 860
I, [2011-10-30T21:07:02.752707 #8370] INFO -- net.ssh.connection.session[810967cc]: channel_data: 0 834b
(xmlagent) ON_DATA: "<?xml version=\"1.0\"?>\n<nf:rpc-reply xmlns:nf=\"urn:ietf:params:xml:ns:netconf:base:1.0\" xmlns:nxos=\"http://www.cisco.com/nxos:1.0\" message-id=\"110\">\n <nf:data>\n--------------------------------------------------------------------------------\nPort VRF Status IP Address Speed MTU\n--------------------------------------------------------------------------------\nmgmt0 -- up 192.168.1.130 1000 1500\n\n--------------------------------------------------------------------------------\nPort VRF Status IP Address Speed MTU\n--------------------------------------------------------------------------------\ncontrol0 -- up -- 1000 1500\n</nf:data>\n</nf:rpc-reply>\n"
D, [2011-10-30T21:07:02.752908 #8370] DEBUG -- tcpsocket[81011414]: received packet nr 11 type 94 len 28
I, [2011-10-30T21:07:02.752989 #8370] INFO -- net.ssh.connection.session[810967cc]: channel_data: 0 6b
(xmlagent) ON_DATA: "]]>]]>"
D, [2011-10-30T21:07:02.753198 #8370] DEBUG -- tcpsocket[81011414]: queueing packet nr 9 type 97 len 28
D, [2011-10-30T21:07:02.753363 #8370] DEBUG -- tcpsocket[81011414]: sent 52 bytes
D, [2011-10-30T21:07:02.765223 #8370] DEBUG -- tcpsocket[81011414]: read 104 bytes
D, [2011-10-30T21:07:02.765507 #8370] DEBUG -- tcpsocket[81011414]: received packet nr 12 type 98 len 44
I, [2011-10-30T21:07:02.765648 #8370] INFO -- net.ssh.connection.session[810967cc]: channel_request: 0 exit-status false
D, [2011-10-30T21:07:02.765823 #8370] DEBUG -- tcpsocket[81011414]: received packet nr 13 type 97 len 12
I, [2011-10-30T21:07:02.765892 #8370] INFO -- net.ssh.connection.session[810967cc]: channel_close: 0
(xmlagent) ON_CLOSE
09:07:02 [~/Programming/Ruby/cherub] $
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment