forked from zycoder0day/toolsoxygencall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awk_netstat.sh
65 lines (63 loc) · 1.87 KB
/
awk_netstat.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#! /usr/bin/env sh
#
# AWK 'netstat' if netstat or lsof is not available.
#
# Fine script by staaldraad
# https://gist.github.com/staaldraad/4c4c80800ce15b6bef1c1186eaa8da9f
# Improved and extended by various people
# THC-2023
# strtonum() is only available in GNU awk. Thus define out our hextodec() to make it portable
# All in one
echo "Proto Local Address Foreign Address State"
awk 'BEGIN{states["01"]="ESTABLISHED"
states["02"]="SYN_SENT"
states["03"]="SYN_RECV"
states["04"]="FIN_WAIT1"
states["05"]="FIN_WAIT2"
states["06"]="TIME_WAIT"
states["07"]="CLOSE"
states["08"]="CLOSE_WAIT"
states["09"]="LAST_ACK"
states["0A"]="LISTEN"
states["0B"]="CLOSING"
states["0C"]="NEW_SYN_RECV"
}
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 {{local=getIP($2);remote=getIP($3)}{printf "tcp % -23s % -23s %s\n", local, remote, states[$4]}}' /proc/net/tcp
echo "Kernel IP routing table"
echo "Destination Gateway Genmask Iface"
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,length(str)-1,2));
for (i=5; i>0; i-=2) {
ret = ret"."hextodec(substr(str,i,2))
}
return ret
}
NR > 1 {{dst=getip($2);gw=getip($3);mask=getip($8)}{printf "% -15s % -15s % -15s %s\n", dst, gw, mask, $1}}' /proc/net/route