Class: Cloudscopes::Network
- Inherits:
-
Object
- Object
- Cloudscopes::Network
- Defined in:
- lib/cloudscopes/network.rb
Instance Method Summary collapse
- #local_ip ⇒ Object
- #open_server_ports(port) ⇒ Object
- #port_open?(port) ⇒ Boolean
- #proc_ip_hex_to_dotted(hexstr) ⇒ Object
- #sockets ⇒ Object
Instance Method Details
#local_ip ⇒ Object
8 9 10 |
# File 'lib/cloudscopes/network.rb', line 8 def local_ip Socket.ip_address_list.detect {|intf| intf.ipv4? and !intf.ipv4_loopback? and !intf.ipv4_multicast? }.ip_address end |
#open_server_ports(port) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/cloudscopes/network.rb', line 44 def open_server_ports(port) sockets.select do |s| s[4].to_i == 1 # only established sockets end.select do |s| s[1] == port end end |
#port_open?(port) ⇒ Boolean
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/cloudscopes/network.rb', line 12 def port_open?(port) begin Timeout::timeout(1) do begin TCPSocket.new(local_ip, port).close return true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH return false end end rescue Timeout::Error return false end end |
#proc_ip_hex_to_dotted(hexstr) ⇒ Object
27 28 29 |
# File 'lib/cloudscopes/network.rb', line 27 def proc_ip_hex_to_dotted(hexstr) hexstr.scan(/../).map(&:hex).reverse*"." end |
#sockets ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cloudscopes/network.rb', line 31 def sockets File.read("/proc/net/tcp").split("\n").drop(1).collect do |l| l.strip.split(/\s+/) end.collect do |s| s[1..2] = s[1].split(/:/) + s[2].split(/:/) # split local/remote addresses to ip and port as separate fields s[1] = proc_ip_hex_to_dotted(s[1]) s[2] = s[2].to_i(16) s[3] = proc_ip_hex_to_dotted(s[3]) s[4] = s[4].to_i(16) s.drop(1) # drop the index end end |