Module: Ronin::Network::Telnet

Included in:
Net, Mixins::Telnet, Support
Defined in:
lib/ronin/network/telnet.rb

Overview

Provides helper methods for communicating with Telnet services.

Constant Summary collapse

DEFAULT_PORT =

Default telnet port

23
DEFAULT_PROMPT =

The default prompt regular expression

/[$%#>] \z/n
DEFAULT_TIMEOUT =

The default timeout

10

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_portInteger

Returns The default Ronin Telnet port.

Returns:

  • (Integer)

    The default Ronin Telnet port.


43
44
45
# File 'lib/ronin/network/telnet.rb', line 43

def self.default_port
  @default_port ||= DEFAULT_PORT
end

.default_port=(port) ⇒ Object

Sets the default Ronin Telnet port.

Parameters:

  • port (Integer)

    The new default Ronin Telnet port.


55
56
57
# File 'lib/ronin/network/telnet.rb', line 55

def self.default_port=(port)
  @default_port = port
end

.default_promptRegexp

Returns The default Ronin Telnet prompt pattern.

Returns:

  • (Regexp)

    The default Ronin Telnet prompt pattern.


65
66
67
# File 'lib/ronin/network/telnet.rb', line 65

def self.default_prompt
  @default_prompt ||= DEFAULT_PROMPT
end

.default_prompt=(prompt) ⇒ Object

Sets the default Ronin Telnet prompt pattern.

Parameters:

  • prompt (Regexp)

    The new default Ronin Telnet prompt pattern.


77
78
79
# File 'lib/ronin/network/telnet.rb', line 77

def self.default_prompt=(prompt)
  @default_prompt = prompt
end

.default_timeoutInteger

Returns The default Ronin Telnet timeout.

Returns:

  • (Integer)

    The default Ronin Telnet timeout.


87
88
89
# File 'lib/ronin/network/telnet.rb', line 87

def self.default_timeout
  @default_timeout ||= DEFAULT_TIMEOUT
end

.default_timeout=(timeout) ⇒ Object

Sets the default Ronin Telnet timeout.

Parameters:

  • timeout (Integer)

    The new default Ronin Telnet timeout.


99
100
101
# File 'lib/ronin/network/telnet.rb', line 99

def self.default_timeout=(timeout)
  @default_timeout = timeout
end

.proxyTelnet, ...

Returns The Ronin Telnet proxy.

Returns:

  • (Telnet, IO, nil)

    The Ronin Telnet proxy.


109
110
111
# File 'lib/ronin/network/telnet.rb', line 109

def self.proxy
  @proxy ||= nil
end

.proxy=(new_proxy) ⇒ Object

Sets the Ronin Telnet proxy.

Parameters:

  • new_proxy (Telnet, IO, nil)

    The new Ronin Telnet proxy.


121
122
123
# File 'lib/ronin/network/telnet.rb', line 121

def self.proxy=(new_proxy)
  @proxy = new_proxy
end

Instance Method Details

#telnet_connect(host, options = {}) {|session| ... } ⇒ Net::Telnet

Creates a new Telnet connection.

Examples:

telnet_connect('towel.blinkenlights.nl')
# => #<Net::Telnet: ...>

Parameters:

  • host (String)

    The host to connect to.

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :port (Integer) — default: Telnet.default_port

    The port to connect to.

  • :binmode (Boolean)

    Indicates that newline substitution shall not be performed.

  • :output_log (String)

    The name of the file to write connection status messages and all received traffic to.

  • :dump_log (String)

    Similar to the :output_log option, but connection output is also written in hexdump format.

  • :prompt (Regexp) — default: Telnet.default_prompt

    A regular expression matching the host command-line prompt sequence, used to determine when a command has finished.

  • :telnet (Boolean) — default: true

    Indicates that the connection shall behave as a telnet connection.

  • :plain (Boolean)

    Indicates that the connection shall behave as a normal TCP connection.

  • :timeout (Integer) — default: Telnet.default_timeout

    The number of seconds to wait before timing out both the initial attempt to connect to host, and all attempts to read data from the host.

  • :wait_time (Integer)

    The amount of time to wait after seeing what looks like a prompt.

  • :proxy (Net::Telnet, IO) — default: Telnet.proxy

    A proxy object to used instead of opening a direct connection to the host.

  • :user (String)

    The user to login as.

  • :password (String)

    The password to login with.

Yields:

  • (session)

    If a block is given, it will be passed the newly created Telnet session.

Yield Parameters:

  • session (Net::Telnet)

    The newly created Telnet session.

Returns:

  • (Net::Telnet)

    The Telnet session


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
# File 'lib/ronin/network/telnet.rb', line 193

def telnet_connect(host,options={})
  telnet_options = {
    'Host'       => host.to_s,
    'Port'       => (options[:port]      || Telnet.default_port),
    'Binmode'    => (options[:binmode]   || false),
    'Waittime'   => (options[:wait_time] || 0),
    'Prompt'     => (options[:prompt]    || Telnet.default_prompt),
    'Timeout'    => (options[:timeout]   || Telnet.default_timeout)
  }

  if (options[:telnet] && !options[:plain])
    telnet_options['Telnetmode'] = true
  end

  if options[:output_log]
    telnet_options['Output_log'] = options[:output_log]
  end

  if options[:dump_log]
    telnet_options['Dump_log'] = options[:dump_log]
  end

  if (proxy = (options[:proxy] || Telnet.proxy))
    telnet_options['Proxy'] = proxy
  end

  session = Net::Telnet.new(telnet_options)
  session.(options[:user],options[:password]) if options[:user]

  yield session if block_given?
  return session
end

#telnet_session(host, options = {}) {|session| ... } ⇒ nil

Starts a new Telnet session.

Examples:

telnet_session('towel.blinkenlights.nl') do |movie|
  movie.each_line { |line| puts line }
end

Parameters:

  • host (String)

    The host to connect to.

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (session)

    If a block is given, it will be passed the newly created Telnet session. After the block has returned, the Telnet session will be closed.

Yield Parameters:

  • session (Net::Telnet)

    The newly created Telnet session.

Returns:

  • (nil)

See Also:


254
255
256
257
258
259
260
261
# File 'lib/ronin/network/telnet.rb', line 254

def telnet_session(host,options={})
  session = telnet_connect(host,options)

  yield session if block_given?

  session.close
  return nil
end