Skip to content

Instantly share code, notes, and snippets.

@spiarh
Forked from staaldraad/awk_netstat.sh
Created September 21, 2021 21:20

Revisions

  1. @staaldraad staaldraad revised this gist Dec 11, 2017. 1 changed file with 3 additions and 6 deletions.
    9 changes: 3 additions & 6 deletions awk_netstat.sh
    Original file line number Diff line number Diff line change
    @@ -11,8 +11,7 @@ grep -v "rem_address" /proc/net/tcp | awk 'function hextodec(str,ret,n,i,k,c){
    ret = 0
    n = length(str)
    for (i = 1; i <= n; i++) {
    c = substr(str, i, 1)
    c = tolower(c)
    c = tolower(substr(str, i, 1))
    k = index("123456789abcdef", c)
    ret = ret * 16 + k
    }
    @@ -24,8 +23,7 @@ grep -v "rem_address" /proc/net/tcp | awk 'function hextodec(str,ret,n,i,k,c){
    ret = 0
    n = length(str)
    for (i = 1; i <= n; i++) {
    c = substr(str, i, 1)
    c = tolower(c)
    c = tolower(substr(str, i, 1))
    k = index("123456789abcdef", c)
    ret = ret * 16 + k
    }
    @@ -38,8 +36,7 @@ awk 'function hextodec(str,ret,n,i,k,c){
    ret = 0
    n = length(str)
    for (i = 1; i <= n; i++) {
    c = substr(str, i, 1)
    c = tolower(c)
    c = tolower(substr(str, i, 1))
    k = index("123456789abcdef", c)
    ret = ret * 16 + k
    }
  2. @staaldraad staaldraad revised this gist Dec 11, 2017. No changes.
  3. @staaldraad staaldraad created this gist Dec 11, 2017.
    56 changes: 56 additions & 0 deletions awk_netstat.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    # Gawk version
    # Remote
    grep -v "rem_address" /proc/net/tcp | awk '{x=strtonum("0x"substr($3,index($3,":")-2,2)); for (i=5; i>0; i-=2) x = x"."strtonum("0x"substr($3,i,2))}{print x":"strtonum("0x"substr($3,index($3,":")+1,4))}'

    # Local
    grep -v "rem_address" /proc/net/tcp | awk '{x=strtonum("0x"substr($2,index($2,":")-2,2)); for (i=5; i>0; i-=2) x = x"."strtonum("0x"substr($2,i,2))}{print x":"strtonum("0x"substr($2,index($2,":")+1,4))}'

    # No Gawk
    # Local
    grep -v "rem_address" /proc/net/tcp | awk 'function hextodec(str,ret,n,i,k,c){
    ret = 0
    n = length(str)
    for (i = 1; i <= n; i++) {
    c = substr(str, i, 1)
    c = tolower(c)
    k = index("123456789abcdef", c)
    ret = ret * 16 + k
    }
    return ret
    } {x=hextodec(substr($2,index($2,":")-2,2)); for (i=5; i>0; i-=2) x = x"."hextodec(substr($2,i,2))}{print x":"hextodec(substr($2,index($2,":")+1,4))}'

    # Remote
    grep -v "rem_address" /proc/net/tcp | awk 'function hextodec(str,ret,n,i,k,c){
    ret = 0
    n = length(str)
    for (i = 1; i <= n; i++) {
    c = substr(str, i, 1)
    c = tolower(c)
    k = index("123456789abcdef", c)
    ret = ret * 16 + k
    }
    return ret
    } {x=hextodec(substr($3,index($3,":")-2,2)); for (i=5; i>0; i-=2) x = x"."hextodec(substr($3,i,2))}{print x":"hextodec(substr($3,index($3,":")+1,4))}'


    # All in one
    awk 'function hextodec(str,ret,n,i,k,c){
    ret = 0
    n = length(str)
    for (i = 1; i <= n; i++) {
    c = substr(str, i, 1)
    c = tolower(c)
    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