lib/ronin/network/telnet.rb in ronin-support-0.3.0 vs lib/ronin/network/telnet.rb in ronin-support-0.4.0.rc1

- old
+ new

@@ -15,16 +15,16 @@ # # You should have received a copy of the GNU Lesser General Public License # along with Ronin Support. If not, see <http://www.gnu.org/licenses/>. # -require 'ronin/network/extensions/telnet' +require 'net/telnet' module Ronin module Network # - # Global settings for accessing Telnet. + # Provides helper methods for communicating with Telnet services. # module Telnet # Default telnet port DEFAULT_PORT = 23 @@ -118,9 +118,143 @@ # # @api public # def Telnet.proxy=(new_proxy) @proxy = new_proxy + end + + # + # Creates a new Telnet connection. + # + # @param [String] host + # The host to connect to. + # + # @param [Hash] options + # Additional options. + # + # @option options [Integer] :port (Telnet.default_port) + # The port to connect to. + # + # @option options [Boolean] :binmode + # Indicates that newline substitution shall not be performed. + # + # @option options [String] :output_log + # The name of the file to write connection status messages and all + # received traffic to. + # + # @option options [String] :dump_log + # Similar to the `:output_log` option, but connection output is also + # written in hexdump format. + # + # @option options [Regexp] :prompt (Telnet.default_prompt) + # A regular expression matching the host command-line prompt sequence, + # used to determine when a command has finished. + # + # @option options [Boolean] :telnet (true) + # Indicates that the connection shall behave as a telnet connection. + # + # @option options [Boolean] :plain + # Indicates that the connection shall behave as a normal TCP + # connection. + # + # @option options [Integer] :timeout (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. + # + # @option options [Integer] :wait_time + # The amount of time to wait after seeing what looks like a prompt. + # + # @option options [Net::Telnet, IO] :proxy (Telnet.proxy) + # A proxy object to used instead of opening a direct connection to the + # host. + # + # @option options [String] :user + # The user to login as. + # + # @option options [String] :password + # The password to login with. + # + # @yield [session] + # If a block is given, it will be passed the newly created Telnet + # session. + # + # @yieldparam [Net::Telnet] session + # The newly created Telnet session. + # + # @return [Net::Telnet] + # The Telnet session + # + # @example + # telnet_connect('towel.blinkenlights.nl') + # # => #<Net::Telnet: ...> + # + # @api public + # + def telnet_connect(host,options={}) + host = host.to_s + telnet_options = {} + + telnet_options['Host'] = host + telnet_options['Port'] = (options[:port] || Telnet.default_port) + telnet_options['Binmode'] = options[:binmode] + telnet_options['Output_log'] = options[:output_log] + telnet_options['Dump_log'] = options[:dump_log] + telnet_options['Prompt'] = (options[:prompt] || Telnet.default_prompt) + + if (options[:telnet] && !options[:plain]) + telnet_options['Telnetmode'] = true + end + + telnet_options['Timeout'] = (options[:timeout] || Telnet.default_timeout) + telnet_options['Waittime'] = options[:wait_time] + telnet_options['Proxy'] = (options[:proxy] || Telnet.proxy) + + user = options[:user] + passwd = options[:passwd] + + session = Net::Telnet.new(telnet_options) + session.login(user,passwd) if user + + yield session if block_given? + return session + end + + # + # Starts a new Telnet session. + # + # @param [String] host + # The host to connect to. + # + # @param [Hash] options + # Additional options. + # + # @yield [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. + # + # @yieldparam [Net::Telnet] session + # The newly created Telnet session. + # + # @return [nil] + # + # @example + # telnet_session('towel.blinkenlights.nl') do |movie| + # movie.each_line { |line| puts line } + # end + # + # @see telnet_session + # + # @api public + # + def telnet_session(host,options={}) + session = telnet_connect(host,options) + + yield session if block_given? + + session.close + return nil end end end end