Skip to content

Instantly share code, notes, and snippets.

@ober
Created July 12, 2016 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ober/9d92d8576e56e7c05e9000496d08558c to your computer and use it in GitHub Desktop.
Save ober/9d92d8576e56e7c05e9000496d08558c to your computer and use it in GitHub Desktop.
netstat in awk
awk -Wposix 'BEGIN{ printf("{ \"tcp_connections\": [") }; {split($2,src,":");split(src[1],sip,"");split($3,dst,":");split(dst[1],dip,"");if ( NR > 1) printf(",");printf("{");printf("\"srcip\":\"%d.%d.%d.%d\",\"sport\": \"%d\",\"dstip\":\"%d.%d.%d.%d\",\"dport\":\"%d\"}","0x" sip[7] sip[8], "0x" sip[5] sip[6],"0x" sip[3] sip[4],"0x" sip[1] sip[2],"0x" src[2],"0x" dip[7] dip[8], "0x" dip[5] dip[6],"0x" dip[3] dip[4], "0x" dip[1] dip[2],"0x" dst[2]);}END{ print "]}"}' < /proc/net/tcp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment