Netstat without netstat

From UVOO Tech Wiki
Revision as of 18:46, 10 July 2023 by Busk (talk | contribs) (Created page with "``` # Get all open ports in hex format declare -a open_ports=($(cat /proc/net/tcp /proc/net/raw /proc/net/udp | grep -v "local_address" | awk '{ print $2 }')) # Define functi...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
# Get all open ports in hex format
declare -a open_ports=($(cat /proc/net/tcp /proc/net/raw /proc/net/udp | grep -v "local_address" | awk '{ print $2 }'))

# Define function for converting
dec2ip () {
    ip=$1
    s=""
    for i in {1..4}; do 
    s='.'$((ip%256))$s && ((ip>>=8)); 
    done; 
    echo ${s:1} | sed 's/\./\n/g' | tac | sed ':a; $!{N;ba};s/\n/./g'
}

# Show all open ports and decode hex to dec
for tuple in ${open_ports[*]}; do 
    port=${tuple#*:}
    ip=${tuple%:*}
    echo $(dec2ip $((0x${ip}))):$((0x${port})); 
done | sort | uniq -c