Skip to content

Instantly share code, notes, and snippets.

@tangnotes
Created October 27, 2021 14:59
Show Gist options
  • Save tangnotes/7adaad2f6f3ba3136c2ed0a749c200df to your computer and use it in GitHub Desktop.
Save tangnotes/7adaad2f6f3ba3136c2ed0a749c200df to your computer and use it in GitHub Desktop.
netstat without netstat

See https://staaldraad.github.io/2017/12/20/netstat-without-netstat/

awk 'function hextodec(str,ret,n,i,k,c){
    ret = 0
    n = length(str)
    for (i = 1; i <= n; i++) {
        c = tolower(substr(str, i, 1))
        k = index("123456789abcdef", c)
        ret = ret * 16 + k
    }
    return ret
}
function getIP(str,ret){
    ret=hextodec(substr(str,index(str,":")-2,2)); 
    for (i=5; i>0; i-=2) {
        ret = ret"."hextodec(substr(str,i,2))
    }
    ret = ret":"hextodec(substr(str,index(str,":")+1,4))
    return ret
} 
NR > 1 {{if(NR==2)print "Local - Remote";local=getIP($2);remote=getIP($3)}{print local" - "remote}}' /proc/net/tcp 
@tangnotes
Copy link
Author

tangnotes commented Nov 8, 2021

https://unix.stackexchange.com/questions/131101/without-using-network-command-lines-in-linux-how-to-know-list-of-open-ports-and

PORT=8080;cat /proc/net/* | awk -F " " '{print $2 ":" $10 }' | grep -i `printf "%x:" $PORT` | awk -F ":" '{print "PORT=" $2 ", INODE=" $3 }'
find /proc -lname "socket:\[$INODE\]" 2> /dev/null | head -n 1 | awk -F "/" '{print "PID="$3}'
PORT=8080;find /proc -lname "socket:\[$(cat /proc/net/* | awk -F " " '{print $2 ":" $10 }' | grep -i `printf "%x:" $PORT` | head -n 1 | awk -F ":" '{print $3}')\]" 2> /dev/null | head -n 1 | awk -F "/" '{print "PID="$3}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment