-
Notifications
You must be signed in to change notification settings - Fork 0
/
NET__IdentifyConnectionRemoteHost.sh
68 lines (57 loc) · 1.76 KB
/
NET__IdentifyConnectionRemoteHost.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
66
67
68
#!/bin/sh
#####################################################################################
###
### $Id: NET__IdentifyConnectionRemoteHost.sh,v 1.1 2020/09/06 21:56:12 root Exp $
###
### Script to report on open network connections; option to report only IPs for remote/local interface.
###
#####################################################################################
TMP=/tmp/`basename "$0" ".sh" `.tmp
rm -f ${TMP}
indir=0
list=0
while [ $# -gt 0 ]
do
case $1 in
--iplist ) list=1 ; indir=0 ; shift ;;
--in ) list=1 ; indir=1 ; shift ;;
--out ) list=1 ; indir=0 ; shift ;;
* ) echo "\n\t Invalid parameter '$1' used on command line. Only valid options: [ --iplist | --in | -out ] \n Bye!\n" ; exit 1 ;;
esac
done
#IFACE=`ifconfig | grep 'BROADCAST' | cut -f1 -d\: `
#IP=`ifconfig ${IFACE} | grep 'inet ' | awk '{print $2}' | cut -f2 -d\: `
#echo $IP
IP=`hostname -I | awk '{ print $1 }' `
if [ -z "${IP}" ]
then
echo "\n\t Unable to get IP address of this host.\n"
exit 1
fi
for remote in `ss -a -e -p | grep $IP | awk '{print $6}' | cut -f1 -d\: `
do
if [ -z "${remote}" ]
then
echo "\n\t Unable to get address of remote host.\n"
exit 1
fi
#nslookup ${remote} >sample.${remote}.nslookup
#whois ${remote} >sample.${remote}.whois
#ping -c 1 ${remote} | tee sample.${remote}.ping
ss -a -e -p | grep $IP |
awk '{ printf("%s %-10s %2s %2s %21s %21s %-44s %-9s %-11s %s %s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12 ) ; }'
done | sort | uniq > ${TMP}
if [ ${list} -eq 1 ]
then
if [ ${indir} -eq 1 ]
then
awk '{ print $5 }' ${TMP} | cut -f1 -d\: | awk '{ print $1 }' | sort -n | uniq
else
awk '{ print $6 }' ${TMP} | cut -f1 -d\: | awk '{ print $1 }' | sort -n | uniq
fi
else
cat ${TMP}
fi
exit 0
exit 0
exit 0