-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapmacs.sh
executable file
·37 lines (31 loc) · 1.18 KB
/
mapmacs.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
#!/bin/bash
# first check if this script is running now and exit if so
if [ $(ps -ef | grep -v grep | grep $0 | wc -l) -gt 3 ]; then exit 0; fi
mkdir state >& /dev/null
cd state
# get the network address, this way isn't perfect, but it works for most cases.
network=$(ip route | grep default | awk ' { print $3 }'| awk -F'[./]' '{print $1"."$2"."$3".0"}')/24
# if your network isn't typical, you can set the network here manually:
# network=192.168.100.128/28
if [ "$#" -lt 1 ]; then echo Usage example: $0 aa:bb:cc:dd:ee:ff; exit 2; fi;
# nmap -sP -T4 $network >& /dev/null
nmap -sn $network >& /dev/null
# loop over mac addresses that were given on the command line
for mac in $*; do
ip=$(arp -n | grep $mac | awk ' { print $1 }')
if [ -n "$ip" ]; then
# ping the ip address
ping $ip -n -q -c 2 -i 0.2 -w 1 >& /dev/null
# if the ping was successful
if [ $? -eq 0 ]; then
touch $mac
else
# if the ping was not successful
# do nothing (we wqant to save history)
# rm -f $mac
nodelete=1
fi
fi
done
cd ..
exit 0