Created
July 23, 2018 23:10
-
-
Save pbrandiezs/bed4dff0e059049f321bf52e73777473 to your computer and use it in GitHub Desktop.
Check open ports using an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
require 'timeout' | |
def is_port_open?(ip, port) | |
begin | |
Timeout::timeout(1) do | |
begin | |
s = TCPSocket.new(ip, port) | |
s.close | |
return true | |
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH | |
return false | |
end | |
end | |
rescue Timeout::Error | |
end | |
return false | |
end | |
targets = [ | |
%w[google.com 80], | |
%w[1.2.3.4 80], | |
%w[google.com 22] | |
] | |
bad_targets = [] | |
targets.each do |target,port| | |
if is_port_open?(target, port) | |
puts "open - #{target}, port #{port}" | |
else | |
puts "closed - #{target}, port #{port}" | |
bad_one = [target,port] | |
bad_targets << bad_one | |
end | |
end | |
puts "bad targets #{bad_targets}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment