Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Scan ports in Ruby (See related posts)

The following code originated from RubyForge: Simple TCP/IP port scanner: Project Info [rubyforge.org], and then the XML feature for reading and outputting port number information was added.


#!/usr/bin/ruby
#file: portscan.rb

require 'open-uri'
require 'socket'
require 'rexml/document'
include REXML

class PortScanner

  def initialize(host, xmlfileout='portscan_result.xml')
    high = 8192
    service_url = "http://rorbuilder.info/r/service_ports.xml"
    doc_result = main(service_url, host, high)

    File.new(xmlfileout,'w').puts(doc_result)
    
  end

  def main(url, host, high)

    buffer = open(url, "UserAgent" => "Ruby-PortScanner1.0")
    doc = Document.new(buffer)

    doc.root.elements.each('records/port') do |node|
      new_node = Element.new('open')
      new_node.text = 'n'
      node.add_element new_node
    end

    for port in 1 .. high
      begin
	      s = TCPsocket.open(host, port)
        puts 'port ' + port.to_s
        node_port = doc.root.elements["records/port[number='#{port.to_s}']"]
        
        unless node_port.nil? 
          port_name = node_port.elements['name'].text.to_s
          node_port.elements['open'].text = 'y'
        else 
          port_name = 'unknown'
          add_port(:doc => doc , :port => port , :name => port_name, :description => '') 
        end

	      printf "%s/%sopen\t%s\n", port, 'tcp'.ljust(11 - port.to_s.length), port_name

	      s.close
      rescue Errno::ECONNREFUSED
	      next
      end
    end
    return doc
  end

  def add_port(h)
    node_port = Element.new('port')

    add_child(node_port, 'number', h[:port])
    add_child(node_port, 'name', h[:name])
    add_child(node_port, 'description', h[:description])
    h[:doc].root.elements['records'].add_element node_port
  end

  def add_child(node,nodename, value)
    newnode = Element.new(nodename)
    newnode.text = value
    node.add_element(newnode)
  end

end

if __FILE__ == $0 then
  ps = PortScanner.new('192.168.1.106')  
end


screen output:
22/tcp      open        ssh
80/tcp      open        www
443/tcp     open        https
513/tcp     open        login
514/tcp     open        shell
4369/tcp    open        unknown
5222/tcp    open        xmpp-client
5269/tcp    open        xmpp-server
5280/tcp    open        unknown
8000/tcp    open        unknown
8001/tcp    open        unknown

xml output:

<ports>
  <summary/>
  <records>
    <port>
      <number>1</number>
      <name>tcpmux</name>
      <description>TCP port service multiplexer</description>
    <open>n</open></port>
    <port>
      <number>7</number>
      <name>echo</name>
      <description/>
    <open>n</open></port>
    <port>
      <number>7</number>
      <name>echo</name>
      <description/>
    <open>n</open></port>
...
    <port>
      <number>5222</number>
      <name>xmpp-client</name>
      <description>Jabber Client Connection</description>
    <open>y</open></port>
    <port>
      <number>5222</number>
      <name>xmpp-client</name>
      <description/>
    <open>n</open></port>
    <port>
      <number>5269</number>
      <name>xmpp-server</name>
      <description>Jabber Server Connection</description>
    <open>y</open></port>
    <port>
      <number>5269</number>
      <name>xmpp-server</name>
      <description/>
    <open>n</open></port>
...
    <port>
      <number>60179</number>
      <name>fido</name>
      <description>fidonet EMSI over TCP</description>
    <open>n</open></port>
  <port><number>4369</number><name>unknown</name><description></description></po
rt><port><number>5280</number><name>unknown</name><description></description></p
ort><port><number>8000</number><name>unknown</name><description></description></
port><port><number>8001</number><name>unknown</name><description></description><
/port></records>


You need to create an account or log in to post comments to this site.


Click here to browse all 6643 code snippets

Related Posts