Saturday 8 June 2013

Listing network connections along with associated application name in conky

Conky's TCP port monitor (tcp_portmon) is quite useful to list inbound/outbound connections. You can find many examples of tcp_portmon by googling concy inbound outbound connections.

Only the problem i see with tcp_portmon is that it doesn't provide name of associated application/process using that connection.

To list application name along with connection details we can make use of netstat command with conky's texeci.

After applying it in conky, it will look similar to this

Here is how to do it
Open your .conkyrc file in text editor

gedit ~/.conkyrc

Paste following at the end of TEXT section

${color Tan2}ESTABLISHED Connections${color DarkSlateGray}${hr 2}

${offset 10}${color lightgrey}Inbound: ${color white}${tcp_portmon 1 32767 count}    ${color lightgrey}Outbound: ${color white}${tcp_portmon 32768 61000 count}${alignr}${color lightgrey}Total: ${color white}${tcp_portmon 1 65535 count}

${color slate grey}Process                 ${color slate grey}Remote Host                     ${color slate grey}L-Port ${alignr}${color slate grey}R-Port${color lightgrey}

${font Ubuntu Mono:size=8}${texeci 30 netstat -ap | grep 'ESTABLISHED' | sed -r 's|.*[0-9.]+:([0-9]*)\s*([^:]+):([a-zA-Z]+)[^/]+/([a-zA-Z0-9]*)|\4 \2 \1 \3|' | awk '{printf("%-14s %-20s %5s %7s\n",$1,$2,$3,$4)}'}

Save and close file.

Now conky will display established connections along with associated process name and refresh it every 30 seconds.

Instead of remote host and port name you can display remote IP and port number like below

Conky displaying connection list with remote IP and port

For this layout paste following in your .conkyrc files instead of above.

${color Tan2}ESTABLISHED Connections${color DarkSlateGray}${hr 2}

${offset 10}${color lightgrey}Inbound: ${color white}${tcp_portmon 1 32767 count}    ${color lightgrey}Outbound: ${color white}${tcp_portmon 32768 61000 count}${alignr}${color lightgrey}Total: ${color white}${tcp_portmon 1 65535 count}

${color slate grey}Process                 ${color slate grey}Remote IP              ${color slate grey}L-Port ${alignr}${color slate grey}R-Port${color lightgrey}

${font Ubuntu Mono:size=8}${texeci 30 netstat -anp | grep 'ESTABLISHED' | sed -r 's|.*192.[0-9.]+:([0-9]*)\s*([0-9.]+):([0-9]+)[^/]+/([a-zA-Z0-9]*)|\4 \2 \1 \3|' | awk '{printf("%-14s %-15s %5s %5s\n",$1,$2,$3,$4)}'}

No comments:

Post a Comment