Skip to content

Instantly share code, notes, and snippets.

@alexanderfefelov
Created December 17, 2015 14:37
Show Gist options
  • Save alexanderfefelov/1ffca091e28cace0f043 to your computer and use it in GitHub Desktop.
Save alexanderfefelov/1ffca091e28cace0f043 to your computer and use it in GitHub Desktop.
Prints conntrack stats for protocols and connection states
#!/usr/bin/env python
protocols = dict()
connection_states = dict()
import subprocess
output = subprocess.check_output('conntrack -L', shell = True)
for row in output.split('\n'):
elements = row.split()
try:
element = elements[0]
if element in protocols:
protocols[element] += 1
else:
protocols[element] = 1
except IndexError:
pass
try:
element = elements[3]
if element[0].isupper():
if element in connection_states:
connection_states[element] += 1
else:
connection_states[element] = 1
except IndexError:
pass
print protocols
print connection_states
@alexanderfefelov
Copy link
Author

Sample output:

{'unknown': 8, 'udp': 31327, 'icmp': 81, 'gre': 2, 'tcp': 43877}
{'ESTABLISHED': 29297, 'SYN_SENT2': 9, 'LAST_ACK': 82, 'FIN_WAIT': 10, 'SYN_RECV': 7, 'TIME_WAIT': 5193, 'CLOSE': 448, 'SYN_SENT': 8329, 'CLOSE_WAIT': 502}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment