Thursday 6 February 2014

Get info about connected processes

This is a little script i wrote to see all tcp/udp connections and see what they are

#! /bin/bash

tcpports=$(netstat -ano | grep tcp | grep -E -o ":[0-9]+" | grep -E -o "[0-9]+")
udpports=$(netstat -ano | grep udp | grep -E -o ":[0-9]+" | grep -E -o "[0-9]+")
for p in $tcpports
do
    pid=$(sudo fuser $p/tcp 2>/dev/null | grep -E -o "[0-9]+")
if [ "$pid" ]
then
    name=$(ps -eo comm,pid | grep $pid | egrep -o '^[^0-9]+')
echo $name ' with pid ' $pid ' is on ' $p
whatis $name
fi
done
for p in $udpports
do
    pid=$(sudo fuser $p/udp 2>/dev/null | grep -E -o "[0-9]+")
if [ "$pid" ]
then
    name=$(ps -eo comm,pid | grep $pid | egrep -o '^[^0-9]+')
echo $name ' with pid ' $pid ' is on ' $p
whatis $name
fi
done

No comments:

Post a Comment