-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_ip_logger.sh
101 lines (94 loc) · 2.29 KB
/
app_ip_logger.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
source ./app_ip_logger-config.txt
public_ip=`curl -s $public_ip_host`
os=`uname -o`
if [ -f ${app_list_file} ]
then
declare -a app_list
while read -r n
do
app_list+=( "$n" )
done <<< `cat $app_list_file`
else
declare -a app_list
for name in ${app_name}
do
app_list+=( "${name}" )
done
fi
if [ ! -f ${log_file} ]
then
touch ${log_file}
else
echo >> ${log_file}
fi
ip_logged=`cat ${log_file}`
echo [`date`] starting $0 on $os ...
echo [`date`] starting $0 on $os ... >> ${log_file}
echo app list : ${app_list[@]}
echo app list : ${app_list[@]} >> ${log_file}
echo current public ip : ${public_ip}
echo current public ip : ${public_ip} >> ${log_file}
case ${os} in
"GNU/Linux")
ps_cmd="ps -ax"
ps_col=1
netstat_cmd="netstat -4np"
netstat_col=5
;;
Msys)
ps_cmd="ps -W"
ps_col=4
netstat_cmd="netstat -no"
netstat_col=3
;;
*)
echo os not supported!!!
exit;;
esac
while true
do
for loop in ${!app_list[@]}
do
app="${app_list[$loop]}"
app_pids=`$ps_cmd | grep -i "${app}" | awk '{print $'${ps_col}'}'`
if [ ! -z "${app_pids}" ]
then
for pid in ${app_pids}
do
new_ip_list=`$netstat_cmd | grep ${pid} | awk '{print $'${netstat_col}'}'`
if [ ! -z "$new_ip_list" ]
then
for new_ip in ${new_ip_list}
do
#skip localhost 127.0.0.1
echo ${new_ip} | grep -q 127.0.0.1
if [ $? -eq 1 ]
then
echo ${current_ip_list} | grep -q ${new_ip}
if [ $? -eq 1 ]
then
current_ip_list="${current_ip_list} ${new_ip}"
if [ ${reverse_lookup} -eq 1 ]
then
ip_only=`echo ${new_ip} | sed 's/:.*//'`
ip_name=`nslookup ${ip_only} ${dns_server} 2> /dev/null | grep -i name | awk {'print $2'}`
fi
echo ${ip_logged} | grep -q ${new_ip}
if [ $? -eq 1 ]
then
echo "[+] "${app} : ${new_ip} ${ip_name}
echo ${app} : ${new_ip} ${ip_name} >> ${log_file}
ip_logged=`cat ${log_file}`
else
echo "[=] "${app} : ${new_ip} ${ip_name}
fi
fi
fi
done
fi
done
fi
done
#debug
sleep ${sleep}
done