#!/bin/bash

ports=$(ss -ptln "sport > :1023 or sport = :80 or sport = :443" | grep -vE 'systemd|127.0.0.|%lo|::1|Netid|Local|next-router' | awk '{print $4}' | awk -F: '{print $NF}' | sort -u)
ips=$(ip a|grep 'inet '|grep -v '127.0.0'|awk '{print $2}')

printf '\n'
printf '+-----------------------------------+\n'
printf '| %-33s |\n' 'Matomo is listening on'

while read -r ip; do
    printf '| %-33s |\n' "  => $ip"
done <<< $ips

printf '+------------+----------------------+\n'
printf '| %-10s | %-20s |\n' 'Ports' 'Process'
printf '+------------+----------------------+\n'

while read -r port; do
    application=$(ss -lptn "sport == :$port" | grep -vE 'Netid|Local' | awk '{print $6}' | cut -d'"' -f2)
    printf '| %-10s | %-20s |\n' $port $application
done <<< $ports

# Print table footer
printf '+------------+----------------------+\n'
printf '\n'
