Home > Scripts > Script to list out listening tcp and udp ports in Linux

Script to list out listening tcp and udp ports in Linux

The following script can be used to list out the listening tcp and udp ports in Linux. Open up a text editor and copy paste the entire contents pasted below, and save it as “openports.sh”(choose any name you like).

#!/bin/bash
tcp_ports=””
udp_ports=””
for ptcp in `netstat -napl | grep LISTEN | grep -v 127.0.0.1 | grep tcp | awk ‘{print$4}’ | grep : | tr ‘:’ ‘ ‘ | awk ‘{print$2}’ | sort -n`; do
if [ “$tcp_ports” == “” ]; then
tcp_ports=”$ptcp”
else
val=`echo $tcp_ports | grep -w $ptcp`
if [ “$val” == “” ]; then
tcp_ports=”$tcp_ports,$ptcp”
fi
fi
done

for pudp in `netstat -napl | grep -v 127.0.0.1 | grep udp | awk ‘{print$4}’ | grep : | tr ‘:’ ‘ ‘ | awk ‘{print$2}’ | sort -n`; do
if [ “$udp_ports” == “” ]; then
udp_ports=”$pudp”
else
val=`echo $udp_ports | grep -w $pudp`
if [ “$val” == “” ]; then
udp_ports=”$udp_ports,$pudp”
fi
fi
done

echo ” Listening TCP ports: $tcp_ports”
echo ” Listening UDP ports: $udp_ports”

Output of the above script will be in the following format,

root@jackal777 [~]# sh openports.sh
Listening TCP ports: 21,25,53,80,110,143,443,465,993,995,3306
Listening UDP ports: 53,123,230,631,5353,43354
root@jackal777 [~]#
Advertisements
Categories: Scripts
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: