-
-
Save askn/8dacd6d47b2b234df2d2cfac76a0572c to your computer and use it in GitHub Desktop.
check if port is open on a remote host
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
#!/usr/bin/env ruby | |
require 'socket' | |
require 'timeout' | |
unless ARGV.length == 2 | |
abort "Usage: ruby #{__FILE__} HOST PORT" | |
end | |
def port_open?(ip, port, seconds=1) | |
# => checks if a port is open or not on a remote host | |
Timeout::timeout(seconds) do | |
begin | |
TCPSocket.new(ip, port).close | |
true | |
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError | |
false | |
end | |
end | |
rescue Timeout::Error | |
false | |
end | |
HOST = ARGV[0] | |
PORT = ARGV[1] | |
if port_open?(HOST, PORT) | |
puts "[OPEN]: Port #{PORT} is open on host #{HOST}" | |
else | |
puts "[NOT OPEN]: Port #{PORT} is not open on host #{HOST}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment