Skip to content

Instantly share code, notes, and snippets.

@picatz
Last active July 9, 2017 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save picatz/b3f025906ce7e3e8fdf19e33b0f9ca48 to your computer and use it in GitHub Desktop.
Save picatz/b3f025906ce7e3e8fdf19e33b0f9ca48 to your computer and use it in GitHub Desktop.
Crystal Port Scanner
require "socket"
host = "target_ip"
ports = (1..1024).to_a
channel = Channel(Nil).new
semaphore = Mutex.new
spawn do
60.times do
spawn do
begin
while port = ports.pop
begin
s = TCPSocket.new(host, port, connect_timeout: 1)
semaphore.synchronize { puts port.to_s + " Open" }
rescue
semaphore.synchronize { puts port.to_s + " Closed" }
ensure
s.close if s
spawn do
channel.send(nil) # end of operation
end
end
end
rescue
# when ya' just can't pop no mo'
end
end
end
end
ports.size.times do
channel.receive
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment