4 #set -u # Error on unset var
8 # - log (to stdout or file):
9 # run arp-scan and convert output to our log format
13 # - status (from stdin or file):
15 # - seen devices, sorted by last-seen
21 # - [ ] Gather more info on each device. How? nmap?
28 local -r level
="$1"; shift
29 local -r fmt="$1\n"; shift
32 printf '%s [%s] ' "$(date '+%Y-%m-%d %H:%M:%S')" "$level" >&2
33 printf "$fmt" $args >&2
41 if [[ -n "$_debug" ]]; then
47 local -r interval_init
="$1"
48 local -r log_file
="$2"
49 local interval_curr
="$interval_init"
53 if sudo arp-scan
--localnet; then
55 interval_curr
="$interval_init"
57 error
'(.) scan failure'
58 interval_curr
=$
(( interval_curr
* 2 ))
60 debug
'(>) sleep for %d seconds' "$interval_curr"
61 sleep "$interval_curr";
65 /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ {
75 local -r log_file
="$1"
78 echo 'mac ip staleness_cur staleness_avg age freq dist'
79 echo '--- -- ------------- ------------- --- ---- ----'
80 sort -n -k 1 "$log_file" \
81 |
awk -v now
="$(date '+%s')" \
88 interval[mac, ip, intervals[mac, ip]++] = ts - seen_last[mac, ip]
90 if (!seen_last[mac, ip] || ts > seen_last[mac, ip] ) seen_last[mac, ip] = ts
91 if (!seen_first[mac, ip] || ts < seen_first[mac, ip]) seen_first[mac, ip] = ts
96 split(key, macip, SUBSEP)
99 staleness_cur = now - seen_last[mac, ip]
100 age = now - seen_first[mac, ip]
101 dist = 100 * (freq[mac, ip] / NR)
103 for (i=1; i<=intervals[mac, ip]; i++)
104 intervals_sum += interval[mac, ip, i]
105 staleness_avg = intervals_sum / intervals[mac, ip]
109 sprintf("%d", staleness_cur), \
110 sprintf("%d", staleness_avg), \
111 sprintf("%d", age), \
137 log_file
='/dev/stdout'
139 if [[ -n "$2" ]]; then
141 if [[ -n "$3" ]]; then
145 debug
'(>) log | interval:"%s" log_file:"%s"' "$interval" "$log_file"
146 log
"$interval" "$log_file"
147 debug
'(.) log | interval:"%s" log_file:"%s"' "$interval" "$log_file"
150 log_file
='/dev/stdin'
151 if [[ -n "$2" ]]; then
154 debug
'(>) status | log_file:"%s"' "$log_file"
156 debug
'(.) status | log_file:"%s"' "$log_file"
159 error
'Unknown command: "%s"' "$cmd"
This page took 0.104794 seconds and 4 git commands to generate.