|
@@ -1,6 +1,6 @@
|
1
|
1
|
#!/bin/sh
|
|
2
|
+REFRESH=2
|
2
|
3
|
HOSTS_FILES="$HOME/.hosts_scanner"
|
3
|
|
-#HOSTS_FILES="/tmp/hosts"
|
4
|
4
|
verbose=0
|
5
|
5
|
res_file=$(mktemp -t hosts_scanner.XXXXXXXXXX)
|
6
|
6
|
date_cmd="date --rfc-3339=s"
|
|
@@ -9,10 +9,17 @@ lock="${res_file}.lock"
|
9
|
9
|
|
10
|
10
|
pids=""
|
11
|
11
|
clean_exit() {
|
|
12
|
+ clear
|
|
13
|
+ reset
|
12
|
14
|
echo "Ctrl+C pressed, exiting..."
|
13
|
15
|
for pid in $(echo $pids)
|
14
|
16
|
do
|
|
17
|
+ echo killing $pid
|
15
|
18
|
kill $pid
|
|
19
|
+ done
|
|
20
|
+ for pid in $(echo $pids)
|
|
21
|
+ do
|
|
22
|
+ wait $pid
|
16
|
23
|
echo $pid killed
|
17
|
24
|
done
|
18
|
25
|
rmdir ${res_file}.lock 2>/dev/null
|
|
@@ -25,8 +32,10 @@ clean_cidr() {
|
25
|
32
|
echo "CIDR killed, cleaning..."
|
26
|
33
|
for pid in $(echo $cidr_pids)
|
27
|
34
|
do
|
|
35
|
+ echo CIDR killing $pid
|
28
|
36
|
kill $pid
|
29
|
|
- echo $pid killed
|
|
37
|
+ wait $pid
|
|
38
|
+ echo CIDR $pid killed
|
30
|
39
|
done
|
31
|
40
|
exit 0
|
32
|
41
|
}
|
|
@@ -49,7 +58,10 @@ upd_ping() {
|
49
|
58
|
# $1 host
|
50
|
59
|
# $2 file
|
51
|
60
|
# $3 lock
|
|
61
|
+ # $4 delay
|
|
62
|
+
|
52
|
63
|
lock="$3"
|
|
64
|
+ test -z "$4" && delay=5 || delay=$4
|
53
|
65
|
last_up="never"
|
54
|
66
|
|
55
|
67
|
while [ 1 ]
|
|
@@ -75,7 +87,7 @@ upd_ping() {
|
75
|
87
|
|
76
|
88
|
semrelease $lock
|
77
|
89
|
|
78
|
|
- sleep 5
|
|
90
|
+ sleep $delay
|
79
|
91
|
done
|
80
|
92
|
}
|
81
|
93
|
|
|
@@ -84,21 +96,40 @@ cidr_scan() {
|
84
|
96
|
# $1 cidr
|
85
|
97
|
# $2 result file
|
86
|
98
|
# $3 lock
|
|
99
|
+ # $4 delay
|
|
100
|
+
|
|
101
|
+ test -z "$4" && delay=5 || delay=$4
|
87
|
102
|
|
88
|
103
|
trap clean_cidr 15
|
89
|
104
|
while [ 1 ]
|
90
|
105
|
do
|
|
106
|
+ semlock $lock
|
|
107
|
+ echo "CIDR scanning $1" >&2
|
|
108
|
+ semrelease $lock
|
|
109
|
+
|
91
|
110
|
for h in $(nmap -sP -oG - $1 | grep "Up$" | cut -d " " -f 2)
|
92
|
111
|
do
|
93
|
112
|
rehost=$(echo $h|sed 's/\./\\./g')
|
94
|
|
- if grep -v " $rehost " $2 > /dev/null
|
|
113
|
+ if grep " $rehost " $2 > /dev/null
|
95
|
114
|
then
|
|
115
|
+ continue
|
|
116
|
+ else
|
96
|
117
|
printf "%30s %8s %30s\n" $h "unknw" "unknwn" >> $res_file
|
97
|
118
|
upd_ping $h $2 $3 &
|
98
|
119
|
pid=$!
|
99
|
120
|
cidr_pids="${cidr_pids}\n$pid"
|
|
121
|
+
|
|
122
|
+ semlock $lock
|
|
123
|
+ echo "$pid scanning $h" >&2
|
|
124
|
+ semrelease $lock
|
100
|
125
|
fi
|
101
|
126
|
done
|
|
127
|
+
|
|
128
|
+ semlock $lock
|
|
129
|
+ echo "CIDR scanned $1" >&2
|
|
130
|
+ semrelease $lock
|
|
131
|
+
|
|
132
|
+ sleep $delay
|
102
|
133
|
done
|
103
|
134
|
}
|
104
|
135
|
|
|
@@ -116,10 +147,10 @@ for h in $(cat "$HOSTS_FILES" )
|
116
|
147
|
do
|
117
|
148
|
if echo $h |grep -v '/' > /dev/null
|
118
|
149
|
then
|
119
|
|
- upd_ping $h $res_file $lock &
|
|
150
|
+ upd_ping $h $res_file $lock 5 &
|
120
|
151
|
else
|
121
|
152
|
# cidr scan
|
122
|
|
- cidr_scan $h $res_file $lock &
|
|
153
|
+ cidr_scan $h $res_file $lock 10 &
|
123
|
154
|
fi
|
124
|
155
|
pid=$!
|
125
|
156
|
pids="${pids}\n$pid"
|
|
@@ -129,6 +160,8 @@ sleep 1
|
129
|
160
|
|
130
|
161
|
while [ 1 ]
|
131
|
162
|
do
|
|
163
|
+ #sleep 1
|
|
164
|
+ #continue
|
132
|
165
|
|
133
|
166
|
semlock $lock
|
134
|
167
|
clear
|
|
@@ -145,5 +178,5 @@ do
|
145
|
178
|
sed -E -e "s/\snever$/$(tput sgr0)\0/" -e "s/^.* down .*$/$(tput setaf 1)\0$(tput sgr0)/" $res_file
|
146
|
179
|
|
147
|
180
|
semrelease $lock
|
148
|
|
- sleep 1
|
|
181
|
+ sleep $REFRESH
|
149
|
182
|
done
|