lib/hawatel_ps/linux/proc_fetch.rb
def open_ports
          socket_list  = Array.new
          ['tcp','udp'].each do |protocol|
            File.foreach("/proc/net/#{protocol}").each do |line|
              hex_port = line.split(' ')[1].split(':')[1]
              hex_ip   = line.split(' ')[1].split(':')[0].scan(/../)
              socketid = line.split(' ')[9]
              if hex_port =~ /$$$$/
                hex_ip.map! { |e| e.to_i(16) }
                socket_attrs = { :address => "#{hex_ip[3]}.#{hex_ip[2]}.#{hex_ip[1]}.#{hex_ip[0]}",
                                 :port => hex_port.to_i(16),
                                 :protocol => protocol,
                                 :id => socketid }
                socket_list << socket_attrs
              end
            end
          end
          return socket_list
        end
# Get list open tcp/upd ports from net/tcp and net/udp file and replace to decimal
#
# @example
#  sockets = open_ports()
#  sockets.each do |socket|
#     puts "#{socket[:address]} #{socket[:port]}"
#  end
#
# @return [Array<Hash>] list all used tcp/udp sockets
Get list open tcp/upd ports from net/tcp and net/udp file and replace to decimal
Get list open tcp / upd ports from net / tcp and net / udp file and replace to decimal
def open_ports
          socket_list  = Array.new
          ['tcp','udp'].each do |protocol|
            File.foreach("/proc/net/#{protocol}").each do |line|
              hex_port = line.split(' ')[1].split(':')[1]
              hex_ip   = line.split(' ')[1].split(':')[0].scan(/../)
              socketid = line.split(' ')[9]
              if hex_port =~ /$$$$/
                hex_ip.map! { |e| e.to_i(16) }
                socket_attrs = { :address => "#{hex_ip[3]}.#{hex_ip[2]}.#{hex_ip[1]}.#{hex_ip[0]}",
                                 :port => hex_port.to_i(16),
                                 :protocol => protocol,
                                 :id => socketid }
                socket_list << socket_attrs
              end
            end
          end
          return socket_list
        end
def open_ports socket_list = Array . new [ 'tcp' , 'udp' ] . each do | protocol | File . foreach ( "/proc/net/#{protocol}" ) . each do | line | hex_port = line . split ( ' ' ) [ 1 ] . split ( ':' ) [ 1 ] hex_ip = line . split ( ' ' ) [ 1 ] . split ( ':' ) [ 0 ] . scan ( / .. / ) socketid = line . split ( ' ' ) [ 9 ] if hex_port =~ / $$$$ / hex_ip . map! { | e | e . to_i ( 16 ) } socket_attrs = { :address => "#{hex_ip[3]}.#{hex_ip[2]}.#{hex_ip[1]}.#{hex_ip[0]}" , :port => hex_port . to_i ( 16 ) , :protocol => protocol , :id => socketid } socket_list << socket_attrs end end end return socket_list end
Get list open tcp/upd ports from net/tcp and net/udp file and replace to decimal
Get list open tcp / upd ports from net / tcp and net / udp file and replace to decimal
list all used tcp/udp sockets
list all used tcp / udp sockets
Array<Hash>
example
sockets = open_ports() sockets.each do |socket| puts "#{socket[:address]} #{socket[:port]}" end
sockets = open_ports () sockets . each do |socket| puts " # { socket [ : address ] } # { socket [ : port ] } " end