Class: Net::SSH::Telnet
- Inherits:
-
Object
- Object
- Net::SSH::Telnet
- Defined in:
- lib/net/ssh/telnet.rb
Overview
Net::SSH::Telnet
Provides a simple send/expect interface with an API almost identical to Net::Telnet. Please see Net::Telnet for main documentation. Only the differences are documented here.
Defined Under Namespace
Classes: TinyFactory
Constant Summary collapse
Instance Attribute Summary collapse
-
#channel ⇒ Object
readonly
The ssh session and channel we are using.
-
#ssh ⇒ Object
readonly
The ssh session and channel we are using.
Instance Method Summary collapse
-
#binmode(mode = nil) ⇒ Object
Turn newline conversion on (
mode
== false) or off (mode
== true), or return the current value (mode
is not specified). -
#binmode=(mode) ⇒ Object
Turn newline conversion on (false) or off (true).
-
#close ⇒ Object
Close the ssh channel, and also the entire ssh session if we opened it.
-
#cmd(options) ⇒ Object
Send a command to the host.
-
#initialize(options, &blk) ⇒ Telnet
constructor
Creates a new Net::SSH::Telnet object.
-
#print(string) ⇒ Object
Sends a string to the host.
-
#puts(string) ⇒ Object
Sends a string to the host.
-
#waitfor(options) ⇒ Object
Read data from the host until a certain sequence is matched.
-
#write(string) ⇒ Object
Write
string
to the host.
Constructor Details
#initialize(options, &blk) ⇒ Telnet
Creates a new Net::SSH::Telnet object.
The API is similar to Net::Telnet, although you will need to pass in either an existing Net::SSH::Session object or a Username and Password, as shown below.
Note that unlike Net::Telnet there is no preprocess method automatically setting up options related to proper character translations, so if your remote ptty is configured differently than the typical linux one you may need to pass in a different terminator or call ‘stty’ remotely to set it into an expected mode. This is better explained by the author of perl’s Net::SSH::Expect here:
search.cpan.org/~bnegrao/Net-SSH-Expect-1.04/lib/Net/SSH/Expect.pod #IMPORTANT_NOTES_ABOUT_DEALING_WITH_SSH_AND_PSEUDO-TERMINALS
though for most installs the default LF should be fine. See example 5 below.
A new option is added to correct a misfeature of Net::Telnet. If you pass “FailEOF” => true, then if the remote end disconnects while you are still waiting for your match pattern then an EOFError is raised. Otherwise, it reverts to the same behaviour as Net::Telnet, which is just to return whatever data was sent so far, or nil if no data was returned so far. (This is a poor design because you can’t tell whether the expected pattern was successfully matched or the remote end disconnected unexpectedly, unless you perform a second match on the return string). See blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/11373 blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/11380
Example 1 - pass existing Net::SSH::Session object
ssh = Net::SSH.start("127.0.0.1",
:username=>"test123",
:password=>"pass456"
)
s = Net::SSH::Telnet.new(
"Dump_log" => "/dev/stdout",
"Session" => ssh
)
puts "Logged in"
p s.cmd("echo hello")
This is the most flexible way as it allows you to set up the SSH session using whatever authentication system you like. When done this way, calling Net::SSH::Telnet.new multiple times will create multiple channels, and #close will only close one channel.
In all later examples, calling #close will close the entire Net::SSH::Session object (and therefore drop the TCP connection)
Example 2 - pass host, username and password
s = Net::SSH::Telnet.new(
"Dump_log" => "/dev/stdout",
"Host" => "127.0.0.1",
"Username" => "test123",
"Password" => "pass456"
)
puts "Logged in"
puts s.cmd("echo hello")
Example 3 - pass open IO object, username and password (this is really just for compatibility with Net::Telnet Proxy feature)
require 'socket'
sock = TCPSocket.open("127.0.0.1",22)
s = Net::SSH::Telnet.new(
"Dump_log" => "/dev/stdout",
"Proxy" => sock,
"Username" => "test123",
"Password" => "pass456"
)
puts "Logged in"
puts s.cmd("echo hello")
Example 4 - pass a connection factory, host, username and password; Net::SSH will call #open(host,port) on this object. Included just because it was easy :-)
require 'socket'
s = Net::SSH::Telnet.new(
"Dump_log" => "/dev/stdout",
"Factory" => TCPSocket,
"Host" => "127.0.0.1",
"Username" => "test123",
"Password" => "pass456"
)
puts "Logged in"
puts s.cmd("echo hello")
Example 5 - connection to a SAN device running a customized NetBSD with different ptty defaults, it doesn’t convert LF -> CR+LF (see the man page for ‘stty’) and uses a custom prompt
s = Net::SSH::Telnet.new(
"Host" => "192.168.1.1",
"Username" => "test123",
"Password" => "pass456",
"Prompt" => %r{^\S+>\s.*$},
"Terminator" => "\r"
)
puts "Logged in"
puts s.cmd("show alerts")
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/net/ssh/telnet.rb', line 139 def initialize(, &blk) # :yield: mesg @options = @options["Host"] = "localhost" unless @options.has_key?("Host") @options["Port"] = 22 unless @options.has_key?("Port") @options["Prompt"] = /[$%#>] \z/n unless @options.has_key?("Prompt") @options["Timeout"] = 10 unless @options.has_key?("Timeout") @options["Waittime"] = 0 unless @options.has_key?("Waittime") @options["Terminator"] = LF unless @options.has_key?("Terminator") unless @options.has_key?("Binmode") @options["Binmode"] = false else unless (true == @options["Binmode"] or false == @options["Binmode"]) raise ArgumentError, "Binmode option must be true or false" end end if @options.has_key?("Output_log") @log = File.open(@options["Output_log"], 'a+') @log.sync = true @log.binmode end if @options.has_key?("Dump_log") @dumplog = File.open(@options["Dump_log"], 'a+') @dumplog.sync = true @dumplog.binmode def @dumplog.log_dump(dir, x) # :nodoc: len = x.length addr = 0 offset = 0 while 0 < len if len < 16 line = x[offset, len] else line = x[offset, 16] end hexvals = line.unpack('H*')[0] hexvals += ' ' * (32 - hexvals.length) hexvals = format("%s %s %s %s " * 4, *hexvals.unpack('a2' * 16)) line = line.gsub(/[\000-\037\177-\377]/n, '.') printf "%s 0x%5.5x: %s%s\n", dir, addr, hexvals, line addr += 16 offset += 16 len -= 16 end print "\n" end end if @options.has_key?("Session") @ssh = @options["Session"] @close_all = false elsif @options.has_key?("Proxy") @ssh = Net::SSH.start(nil, nil, :host_name => @options["Host"], # ignored :port => @options["Port"], # ignored :user => @options["Username"], :password => @options["Password"], :timeout => @options["Timeout"], :proxy => TinyFactory.new(@options["Proxy"]) ) @close_all = true else = "Trying " + @options["Host"] + "...\n" yield() if block_given? @log.write() if @options.has_key?("Output_log") @dumplog.log_dump('#', ) if @options.has_key?("Dump_log") begin @ssh = Net::SSH.start(nil, nil, :host_name => @options["Host"], :port => @options["Port"], :user => @options["Username"], :password => @options["Password"], :timeout => @options["Timeout"], :proxy => @options["Factory"] ) @close_all = true rescue TimeoutError raise TimeoutError, "timed out while opening a connection to the host" rescue @log.write($ERROR_INFO.to_s + "\n") if @options.has_key?("Output_log") @dumplog.log_dump('#', $ERROR_INFO.to_s + "\n") if @options.has_key?("Dump_log") raise end = "Connected to " + @options["Host"] + ".\n" yield() if block_given? @log.write() if @options.has_key?("Output_log") @dumplog.log_dump('#', ) if @options.has_key?("Dump_log") end @buf = "" @eof = false @channel = nil @ssh.open_channel do |channel| channel.request_pty { |ch,success| if success == false raise "Failed to open ssh pty" end } channel.send_channel_request("shell") { |ch, success| if success @channel = ch waitfor(@options['Prompt'], &blk) return else raise "Failed to open ssh shell" end } channel.on_data { |ch,data| @buf << data } channel.on_extended_data { |ch,type,data| @buf << data if type == 1 } channel.on_close { @eof = true } end @ssh.loop end |
Instance Attribute Details
#channel ⇒ Object (readonly)
The ssh session and channel we are using.
267 268 269 |
# File 'lib/net/ssh/telnet.rb', line 267 def channel @channel end |
#ssh ⇒ Object (readonly)
The ssh session and channel we are using.
267 268 269 |
# File 'lib/net/ssh/telnet.rb', line 267 def ssh @ssh end |
Instance Method Details
#binmode(mode = nil) ⇒ Object
Turn newline conversion on (mode
== false) or off (mode
== true), or return the current value (mode
is not specified).
271 272 273 274 275 276 277 278 279 280 |
# File 'lib/net/ssh/telnet.rb', line 271 def binmode(mode = nil) case mode when nil @options["Binmode"] when true, false @options["Binmode"] = mode else raise ArgumentError, "argument must be true or false" end end |
#binmode=(mode) ⇒ Object
Turn newline conversion on (false) or off (true).
283 284 285 286 287 288 289 |
# File 'lib/net/ssh/telnet.rb', line 283 def binmode=(mode) if (true == mode or false == mode) @options["Binmode"] = mode else raise ArgumentError, "argument must be true or false" end end |
#close ⇒ Object
Close the ssh channel, and also the entire ssh session if we opened it.
260 261 262 263 264 |
# File 'lib/net/ssh/telnet.rb', line 260 def close @channel.close if @channel @channel = nil @ssh.close if @close_all and @ssh end |
#cmd(options) ⇒ Object
Send a command to the host.
More exactly, sends a string to the host, and reads in all received data until is sees the prompt or other matched sequence.
The command or other string will have the newline sequence appended to it.
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/net/ssh/telnet.rb', line 398 def cmd() # :yield: recvdata match = @options["Prompt"] time_out = @options["Timeout"] fail_eof = @options["FailEOF"] if .kind_of?(Hash) string = ["String"] match = ["Match"] if .has_key?("Match") time_out = ["Timeout"] if .has_key?("Timeout") fail_eof = ["FailEOF"] if .has_key?("FailEOF") else string = end self.puts(string) if block_given? waitfor({"Prompt" => match, "Timeout" => time_out, "FailEOF" => fail_eof}){|c| yield c } else waitfor({"Prompt" => match, "Timeout" => time_out, "FailEOF" => fail_eof}) end end |
#print(string) ⇒ Object
Sends a string to the host.
This does not automatically append a newline to the string. Embedded newlines may be converted depending upon the values of binmode or terminator.
373 374 375 376 377 378 379 380 381 |
# File 'lib/net/ssh/telnet.rb', line 373 def print(string) terminator = @options["Terminator"] if @options["Binmode"] self.write(string) else self.write(string.gsub(/\n/n, terminator)) end end |
#puts(string) ⇒ Object
Sends a string to the host.
Same as #print(), but appends a newline to the string.
386 387 388 |
# File 'lib/net/ssh/telnet.rb', line 386 def puts(string) self.print(string + "\n") end |
#waitfor(options) ⇒ Object
Read data from the host until a certain sequence is matched.
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/net/ssh/telnet.rb', line 293 def waitfor() # :yield: recvdata time_out = @options["Timeout"] waittime = @options["Waittime"] fail_eof = @options["FailEOF"] if .kind_of?(Hash) prompt = if .has_key?("Match") ["Match"] elsif .has_key?("Prompt") ["Prompt"] elsif .has_key?("String") Regexp.new( Regexp.quote(["String"]) ) end time_out = ["Timeout"] if .has_key?("Timeout") waittime = ["Waittime"] if .has_key?("Waittime") fail_eof = ["FailEOF"] if .has_key?("FailEOF") else prompt = end if time_out == false time_out = nil end line = '' buf = '' rest = '' sock = @ssh.transport.socket until prompt === line and @buf == "" and (@eof or (not sock.closed? and not IO::select([sock], nil, nil, waittime))) while @buf == "" and !@eof # timeout is covered by net-ssh begin @channel.connection.process(0.1) rescue IOError @eof = true end end if @buf != "" c = @buf; @buf = "" @dumplog.log_dump('<', c) if @options.has_key?("Dump_log") buf = rest + c rest = '' unless @options["Binmode"] if pt = buf.rindex(/\r\z/no) buf = buf[0 ... pt] rest = buf[pt .. -1] end buf.gsub!(/#{EOL}/no, "\n") end @log.print(buf) if @options.has_key?("Output_log") line += buf yield buf if block_given? elsif @eof # End of file reached break if prompt === line raise EOFError if fail_eof if line == '' line = nil yield nil if block_given? end break end end line end |
#write(string) ⇒ Object
Write string
to the host.
Does not perform any conversions on string
. Will log string
to the dumplog, if the Dump_log option is set.
363 364 365 366 |
# File 'lib/net/ssh/telnet.rb', line 363 def write(string) @dumplog.log_dump('>', string) if @options.has_key?("Dump_log") @channel.send_data string end |