Skip to content

Instantly share code, notes, and snippets.

@seungjin
Created April 9, 2011 18:41
Show Gist options
  • Save seungjin/911655 to your computer and use it in GitHub Desktop.
Save seungjin/911655 to your computer and use it in GitHub Desktop.
#!/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