This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Active sessions
#14
You can filter the result by local port (80 or 443) and also by TCP state. TCP_ESTABLISHED should be enough since visualization uses web sockets where connection is always open during an active session.

Code:
states = {
  'TCP_ESTABLISHED',
  'TCP_SYN_SENT',
  'TCP_SYN_RECV',
  'TCP_FIN_WAIT1',
  'TCP_FIN_WAIT2',
  'TCP_TIME_WAIT',
  'TCP_CLOSE',
  'TCP_CLOSE_WAIT',
  'TCP_LAST_ACK',
  'TCP_LISTEN',
  'TCP_CLOSING',
};

-- convert hex to readable IP
function toip(hex)
  local res = {}

  for i = 4, 1, -1 do
    local j = (i - 1) * 2 + 1
    local ch = hex:sub(j, j + 1)
    res[ #res + 1 ] = tonumber(ch, 16)
  end

  return table.concat(res, '.')
end

for line in io.lines('/proc/net/tcp') do
  local_ip, local_port, remote_ip, remote_port, state =
    line:match('(%x+):(%x+)%s+(%x+):(%x+)%s+(%x+)')

  -- valid line
  if local_ip and local_port and remote_ip and remote_port and state then
    local_ip = toip(local_ip)
    local_port = tonumber(local_port, 16)

    remote_ip = toip(remote_ip)
    remote_port = tonumber(remote_port, 16)

    state = states[ tonumber(state, 16) ]

    log(local_ip, local_port, remote_ip, remote_port, state)
  end
end
Reply


Messages In This Thread
Active sessions - by Thomas - 19.10.2017, 16:34
RE: Active sessions - by Erwin van der Zwart - 19.10.2017, 18:37
RE: Active sessions - by Thomas - 24.10.2017, 16:06
RE: Active sessions - by Erwin van der Zwart - 24.10.2017, 17:07
RE: Active sessions - by Thomas - 24.10.2017, 17:42
RE: Active sessions - by Erwin van der Zwart - 24.10.2017, 20:50
RE: Active sessions - by Thomas - 02.11.2017, 14:12
RE: Active sessions - by admin - 02.11.2017, 14:29
RE: Active sessions - by Thomas - 02.11.2017, 14:55
RE: Active sessions - by Erwin van der Zwart - 02.11.2017, 19:20
RE: Active sessions - by Thomas - 07.11.2017, 14:56
RE: Active sessions - by admin - 08.11.2017, 12:26
RE: Active sessions - by Thomas - 08.11.2017, 17:59
RE: Active sessions - by admin - 09.11.2017, 11:49
RE: Active sessions - by Thomas - 09.11.2017, 15:08

Forum Jump: