I have several remote devices that create reverse ssh connections back to my server, so that I ssh to them and manage them remotely.  They connect back to me via a custom port.  When I do a netstat -an | grep 127.0.0.0.1: on my server, I get a list of devices that are currently connected.  The output looks like this:tcp        0      0 127.0.0.1:6001          0.0.0.0:*               LISTENtcp        0      0 127.0.0.1:6002          0.0.0.0:*               LISTENtcp        0      0 127.0.0.1:6003          0.0.0.0:*               LISTENWhere 600x is the port the device is connected on.My question is this. I want to make a script that i can run on my server that shows which devices are current connected.  I want to have config file that the script would read that lists my devices like this:NORTH:6001SOUTH:6002EAST:6003WEST:6004and the script would parse the output of the netstat command and the output would look like this:Active connections:NORTH, SOUTH, EASTInactive connections:WESTI hope this isn't too confusing. Is this something that can be done with grep and awk?
Ideas for parsing netstat results for active connections
linux;text processing;awk;grep;scripting
There are three steps here Just extract active port names from netstatnetstat -an | grep 127.0.0.0.1: | awk -F [ :]+ '{print $5}'the awk specifies group of spaces and colons as separators and picks up the port id only.Compare these ports from the device list file (say named device_list.txt), by using grep and output of the command in step 1 as patternnetstat -an | grep 127.0.0.0.1: | awk -F [ :]+ '{print $5}' | grep -f - device_list.txtThis will give an output like thisNORTH:6001SOUTH:6002EAST:6003The inactive connections can be obtained by using a -v in the grep netstat -an | grep 127.0.0.0.1: | awk -F [ :]+ '{print $5}' | grep -v -f - device_list.txtThe output of this isWEST:6004You could then script around the above to print in any format you want