-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscan_hosts.sh
42 lines (38 loc) · 1.33 KB
/
scan_hosts.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
NMAP=$(nmap -sP -PA21,22,25,554,3389 192.168.1.1/24) # set here the proper internal IP range
cat /dev/null > /tmp/hosts.ldif #tmp file
TIME=$(date +"%s")
# If your LDAP base is different modify here (note that dc=amm is used and generated by the program)
BASE="dc=amm,dc=nodomain"
echo dn: timestamp=$TIME,$BASE >> /tmp/hosts.ldif
echo objectClass: scan >> /tmp/hosts.ldif
echo timestamp: $TIME >> /tmp/host.ldif
echo >> /tmp/hosts.ldif
echo $NMAP | awk -F'Nmap scan report for ' '{
for(i = 2; i <= NF; i++) {
split($i,ipLine," Host");
split($i,macLine,". MAC Address: ");
split(macLine[2],mac," \(")
len = split(ipLine[1],hostname," \(");
if( len > 1 ) {
print substr(hostname[2], 1, length(hostname[2]) - 1) "|" hostname[1] "|" mac[1];
} else {
print hostname[1] "||" mac[1];
}
}
}' | while IFS='|' read IP HOST MAC; do
# echo "Adding entry: ip " $IP " hostname " $HOST " mac " $MAC
if [ "$IP" ] ; then
echo dn: ip=$IP,timestamp=$TIME,$BASE
echo objectClass: node
echo ip: $IP
if [ "$MAC" ] ; then
echo mac: $MAC
fi
if [ "$HOST" ] ; then
echo hostname: $HOST
fi
echo
fi >> /tmp/hosts.ldif
done;
# set here your user set here password log file (you can move or it's deleted on reboot)
ldapadd -c -x -D "cn=admin,dc=nodomain" -w admin -f /tmp/hosts.ldif >> /tmp/scan.log 2>&1