Module: SshDevice
- Included in:
- Brocade::SAN::Switch
- Defined in:
- lib/brocadesan/device.rb,
lib/brocadesan/device.rb
Overview
Basic wrapper class that runs SSH queries on the device and returns Response
It is used to extend further classes with SSH query mechanism
Defined Under Namespace
Constant Summary collapse
- DEFAULT_QUERY_PROMPT =
default query prompt that will preceed each command started by
query
in the Responsedata
value is “> ”
This way the parser can separate from commands and data. The default will work everwhere where the retunned data is not prepended by “> ”
Ignore the ssh prompt like this: super_server(admin)> cmd super_server(admin)> data
This on the other hand would be problem. In this case override the prompt super_server(admin)> cmd super_server(admin)>> data
"> "
Instance Attribute Summary collapse
-
#prompt ⇒ Object
readonly
Returns the value of attribute prompt.
Instance Method Summary collapse
-
#get_mode ⇒ Object
get current query mode.
-
#initialize(address, user, password, opts = {}) ⇒ Object
Initialization method.
-
#interactive_mode(&block) ⇒ Object
Defines a block of code to run in interactive mode.
-
#query(*cmds) ⇒ Object
Queries
cmds
commands directly. -
#script_mode(&block) ⇒ Object
Defines a block of code to run in script mode.
-
#session(&block) ⇒ Object
Opens a session block.
-
#set_mode(mode) ⇒ Object
sets current query mode.
Instance Attribute Details
#prompt ⇒ Object (readonly)
Returns the value of attribute prompt.
25 26 27 |
# File 'lib/brocadesan/device.rb', line 25 def prompt @prompt end |
Instance Method Details
#get_mode ⇒ Object
get current query mode
returns either interactive
or script
default mode is script
51 52 53 |
# File 'lib/brocadesan/device.rb', line 51 def get_mode [true,false].include?(@opts[:interactive]) ? (@opts[:interactive]==true ? "interactive" : "script") : "script" end |
#initialize(address, user, password, opts = {}) ⇒ Object
Initialization method
opts
can be:
- :interactive
-
true
/false
will use interactive query can be set later - :prompt
-
prompt
will override the DEFAULT_QUERY_PROMPT
36 37 38 39 40 41 42 43 44 |
# File 'lib/brocadesan/device.rb', line 36 def initialize(address,user,password,opts={}) @address=address @user=user @password=password @opts=opts @session=nil @session_level=0 @prompt = opts[:prompt] ? opts[:prompt].to_s : DEFAULT_QUERY_PROMPT end |
#interactive_mode(&block) ⇒ Object
Defines a block of code to run in interactive mode
the mode will be reverted back to initial mode after leaving the block
146 147 148 |
# File 'lib/brocadesan/device.rb', line 146 def interactive_mode(&block) run_in_mode :interactive, &block end |
#query(*cmds) ⇒ Object
Queries cmds
commands directly. This method is to be used to implement higlevel API or to do more difficult queries that have to be parsed separately and for which there is now highlevel API
When started in interactive mode be sure the command is followed by inputs for that command. If command will require additional input and there is not one provided the prompt will receive enter
. It will do this maximum of 100 times, then it gives up and returns whatever it got until then.
Query command will open connection to device if called outside session block or use existing session if called within session block.
Example:
>> class Test
>> include SshDevice
>> end
>> device = Test.new("address","user","password")
>> device.query("switchname")
=> #<Test::Response:0x2bb1e00 @errors="", @data="> switchname\nsanswitchA\n", @parsed={:parsing_position=>"end"}>
Returns instance of Response or raises Error if the connection cannot be opened
Raises Error if SSH returns error. SSH error will be available in the exception message
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/brocadesan/device.rb', line 90 def query(*cmds) output=nil if session_exist? output=exec(@session,cmds) else Net::SSH.start @address, @user, :password=>@password do |ssh| output=exec(ssh,cmds) end end raise self.class::Error.new(output.errors) if !output.errors.empty? return output end |
#script_mode(&block) ⇒ Object
Defines a block of code to run in script mode
the mode will be reverted back to initial mode after leaving the block
139 140 141 |
# File 'lib/brocadesan/device.rb', line 139 def script_mode(&block) run_in_mode :script, &block end |
#session(&block) ⇒ Object
Opens a session block
All queries within the session block use the same connection. This speeds up the query processing.
The connection is closed at the end of the block
The command supports session blocks within session blocks. Session will be closed only at the last block
Example:
device.session do
device.query("switchname")
device.version
end
Must receive block, raises SshDevice::Error otherwise
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/brocadesan/device.rb', line 120 def session(&block) raise Error.no_block if !block_given? begin @session_level+=1 if !session_exist? @session=Net::SSH.start @address, @user, :password=>@password end yield rescue => e raise e ensure @session_level-=1 @session.close if @session && @session_level==0 && !@session.closed? end end |
#set_mode(mode) ⇒ Object
sets current query mode
mode
: interactive or script
interactive - used to do interactive queries in scripted manner by providing all responses in advance, see #query
61 62 63 64 |
# File 'lib/brocadesan/device.rb', line 61 def set_mode(mode) @opts[:interactive] = mode.to_s == "interactive" ? true : false get_mode end |