Jump to content

Ruby Port Scanner

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
JMC31337

JMC31337

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
========================
require 'rubygems'
require 'net/ping'
puts "Enter IP Address to Scan:"
#ipaddress = gets

1.upto(65536)
{
|@x|
if Net:: PingTCP.new(#ipaddress,#x)
puts "Response/Port Open #x"
else
puts "No Response / Port closed #x"
}
exit 
===========================
CODED BY: JMC31337

Edited by WingedPanther, 30 January 2009 - 08:27 AM.
add code tags (the # button)


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Moved to correct thread, code tags added.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
shmitty

shmitty

    Newbie

  • Members
  • Pip
  • 3 posts
This isn't a portscanner. The code doesn't even execute.
If you want a portscanner, you've got to use sockets.

Try this code:

require 'socket'

puts "Enter IP Address to Scan:"
ipaddress = gets

1.upto(1024) {|port|
  begin
    TCPSocket.open(ipaddress.chop, port)
    puts "Response/Port Open: #{port}"
  rescue
    # uncomment the following line to show closed ports (noisy!)
    #puts "No Response / Port closed: #{port}"
  end
}

--
shmitty

treecode.com
klasifikasia.com
places.ae

#4
JMC31337

JMC31337

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
yup yur right... good lookin out Shmitty... guess i should proof read my own stuff once in awhile... Obviously RUBY aint my thang...
"Your Life Is Your Crime, It's Punishment Time"

#5
shmitty

shmitty

    Newbie

  • Members
  • Pip
  • 3 posts
No worries jmc, I was looking for a simple portscanner code snippet in ruby for a project I was working on and this is about the first one that pops up on Google.

By the way, if you don't want to mess around with non blocking sockets but you do want a timeout on that TCP connect attempt you can add the following code:


require 'socket'


puts "Enter IP Address to Scan:"

ipaddress = gets


1.upto(1024) {|port|

  begin

    timeout(5) do

      TCPSocket.open(ipaddress.chop, port)

    end

    puts "Response/Port Open: #{port}"

  rescue Timeout::Error

    # uncomment the following line to show closed ports (noisy!)

    #puts "No Response / Port closed: #{port}"

  rescue

    # uncomment the following line to show closed ports (noisy!)

    #puts "No Response / Port closed: #{port}"

  end

}


Kind of an easy way in Ruby to put a timer on stuff.
--
shmitty

treecode.com
klasifikasia.com
places.ae

#6
JMC31337

JMC31337

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
interesting... so what does rescue do?? is that Ruby's lil Exception Error Catching routine??
"Your Life Is Your Crime, It's Punishment Time"

#7
shmitty

shmitty

    Newbie

  • Members
  • Pip
  • 3 posts
Yes that's correct.

More info in the Ruby docs
ruby-doc.org/core-1.9/classes/Exception.html
--
shmitty

treecode.com
klasifikasia.com
places.ae