Created
April 9, 2011 18:41
-
-
Save seungjin/911655 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# $ netcat $HOST $PORT -w 1 -q 0 </dev/null && do_something | |
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 | |
host = ARGV[0].split(":")[0] | |
ip = ARGV[0].split(":")[1].to_i | |
puts is_port_open?(host,ip) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment