Soup To Nuts

 awk 
 
Quotes: How can you easily produce quotes in awk?  \x27
        awk '{printf(" \x27%s\x27 \n",$1)}' data1
 
 
Note: IP addreses in the /proc/net files are represented as  little-endian four-byte hexadecmal numbers.
  • Take the local_address  0100007F:1FB6
  • Reverse odring of the bytes in IP address part:
    • 7F 00 00 01 : 0017
  • Convert hex-to dec
    • 127 0 0 1 : 23
 
 
 
awk program to do the above conversion:
 
Put the following in file awktcp.sc1
 
NR > 1 {
t="echo \"ibase=16;" substr($2,7,2)"\" |bc";  t | getline a; close(t);
t="echo \"ibase=16;" substr($2,5,2)"\" |bc"; t  | getline b; close(t);
t="echo \"ibase=16;" substr($2,3,2)"\" |bc"; t  | getline c; close(t);
t="echo \"ibase=16;" substr($2,1,2)"\" |bc"; t  | getline d; close(t);
t="echo \"ibase=16;" substr($2,10,4)"\" |bc"; t  | getline e; close(t);
 

t="echo \"ibase=16;" substr($3,7,2)"\" |bc"; t  | getline f; close(t);
t="echo \"ibase=16;" substr($3,5,2)"\" |bc"; t  | getline g; close(t);
t="echo \"ibase=16;" substr($3,3,2)"\" |bc"; t  | getline h; close(t);
t="echo \"ibase=16;" substr($3,1,2)"\" |bc"; t  | getline i; close(t);
t="echo \"ibase=16;" substr($3,10,4)"\" |bc"; t  | getline j; close(t);
if ( a > 0)
printf("%d.%d.%d.%d: %d \t %d.%d.%d.%d: %d\n",a,b,c,d,e,f,g,h,i,j)  }
 
Then, run
 
cat /proc/net/tcp |awk -f awktcp.sc1
cat /proc/net/udp |awk -f awktcp.sc1
 
You'll get something like the following:
 
130 91 96 184 : 22
192 168 1 101 : 2454
192 168 1 101 : 3412
192 168 1 101 : 2155
192 168 1 101 : 2106
 

More samples of awk programs can be found in the download.


 
Changes
6.22.03 Alpha 0.0.1 release 
 
 
SourceForge.net Logo