-
Notifications
You must be signed in to change notification settings - Fork 0
/
show-connections.sh
56 lines (49 loc) · 2.39 KB
/
show-connections.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
#!/bin/sh
InputFile=/proc/net/ip_conntrack
TempFile=/tmp/ip_contrack.tmp
Spaces=' '
sSpaces=' '
netaddr=`ifconfig br0 | grep Bcast | cut -d":" -f3 | sed 's/Mask//' | sed 's/ //' | sed 's/255//' | sed 's/\(.*\)./\1 /' | sed 's/ //'`
cp $InputFile $TempFile
echo -e "Network address: $netaddr"
echo -e "Outbound connections:"
echo -e "Local Remote Port"
echo -e "----------------------------------+-----------------------------------------------------+-----------"
while IFS= read -r line <&3; do
p=`echo $line | cut -d" " -f4`
if [[ "$p" == "ESTABLISHED" ]]
then
q=`echo $line | cut -d"=" -f2 | sed 's/dst=//' | sed 's/dst//'`
if [[ ${q:0:${#netaddr}} == "$netaddr" ]]
then
r=`echo $line | cut -d"=" -f8 | sed 's/dst=//' | sed 's/dst//'`
port=`echo $line | cut -d"=" -f5 | sed 's/packets=//' | sed 's/packets//'`
remote=`nslookup $r | grep 'Address 1' | tail -1 | cut -d' ' -f4`
local=`nslookup $q | grep 'Address 1' | tail -1 | cut -d' ' -f4`
if [[ "$local" == "" ]]; then local=$q; fi
if [[ "$remote" == "" ]]; then remote=$r; fi
printf '%s%s%s%s%s\n' "$local ${sSpaces:${#local}} $remote ${Spaces:${#remote}} $port"
fi
fi
done 3< "$TempFile"
echo -e "\nInbound connections:"
echo -e "Remote Local Port"
echo -e "-----------------------------------------------------+----------------------------------+-----------"
while IFS= read -r line <&3; do
p=`echo $line | cut -d" " -f4`
if [[ "$p" == "ESTABLISHED" ]]
then
r=`echo $line | cut -d"=" -f2 | sed 's/dst=//' | sed 's/dst//'`
if [[ ${r:0:${#netaddr}} != "$netaddr" ]]
then
q=`echo $line | cut -d"=" -f8 | sed 's/dst=//' | sed 's/dst//'`
port=`echo $line | cut -d"=" -f5 | sed 's/packets=//' | sed 's/packets//'`
remote=`nslookup $r | grep 'Address 1' | tail -1 | cut -d' ' -f4`
local=`nslookup $q | grep 'Address 1' | tail -1 | cut -d' ' -f4`
if [[ "$local" == "" ]]; then local=$q; fi
if [[ "$remote" == "" ]]; then remote=$r; fi
printf '%s%s%s%s%s\n' "$remote ${Spaces:${#remote}} $local ${sSpaces:${#local}} $port"
fi
fi
done 3< "$TempFile"
rm $TempFile