#!/bin/bash
#
##
# Advanced Policy Firewall (APF) v1.7.6
#             (C) 2002-2019, R-fx Networks <proj@rfxn.com>
#             (C) 2019, Ryan MacDonald <ryan@rfxn.com>
# This program may be freely redistributed under the terms of the GNU GPL v2
##
#
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"
