Hi,
I have written a small tcp scanner procedural and wanted to change it to
OOP but something went wrong. Until this line everything works fine:
my_scanner.scanning(hosts,ports)
but here I always get:
test.rb:95: private method `scanning’ called for
#Scanner:0xb7c9c428 (NoMethodError)
but why? my_scanner is from the Scanner class which owns the method
scanning?!? Do I get something wrong?
here’s my code:
#!/usr/bin/ruby -w
require ‘socket’
require ‘timeout’
class Scanner
def initialize
@hosts,@ports = Array($*)
end
def portarrange
case @ports
when /^.+[..]/ # for
ranges (75…123)
@ports = @ports.split(“…”)
@ports = @ports[0].to_i…@ports[1].to_i
when /^.+[,]/ # for
multiple ports (34,345,213)
@ports = @ports.split(“,”)
else
@ports = Array(@ports) # for
one single port (21)
end
end
def hostarrange
case @hosts
when /^.+[,]/ # for
multiple IP’s/hosts (81.95.1.40,64.111.124.251,www.cnn.com)
@hosts = @hosts.split(“,”)
else
@hosts = Array(@hosts) # for
one single port (81.95.1.40)
end
end
def output(state,port)
printed = false
portsfile = File.new("ports", "r")
scanpat = "^.+ #{port}/tcp"
portsfile.each_line do |line|
if line =~ Regexp.new(scanpat)
puts "#{state} : #{line}"
printed = true
end
puts "#{state} : #{port}" if printed == false
end
ensure
portsfile.close
end
end
def scanning(hosts,ports)
hosts.each do |host|
begin
puts "scanning #{host}:"
ports.each do |port|
begin
Timeout::timeout(10){TCPSocket.new(host, port)} #
set timeout here
rescue Timeout::Error
output(“filtered”,port)
rescue
output(“closed”,port)
else
output(“open”,port)
end end
end
end
end
##################### code start #####################
puts “no arguments past,correct usage:\nruby #{$0} [hosts] [ports]\n” if
!ARGV[1]
my_scanner = Scanner.new
hosts = my_scanner.hostarrange
ports = my_scanner.portarrange
=begin
puts "debugging: "
puts hosts
puts ports
everything alright until here
=end
my_scanner.scanning(hosts,ports)
##################### eof #####################
–
greets
(
)
(
/\ .-"""-. /\
//\\/ ,,, \//\\
|/\| ,;;;;;, |/\|
//\\\;-"""-;///\\
// \/ . \/ \\
(| ,-_| \ | / |_-, |)
//`__\.-.-./__`\\
// /.-(() ())-.\ \\
(\ |) '---' (| /)
` (| |) `
jgs \) (/
one must still have chaos in oneself to be able to give birth to a
dancing star