c96192ffc46d3e21e7b606fae677d1dec541f5d8
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
="$1"
48 local -r log_file
="$2"
52 if ! sudo arp-scan
--localnet; then
59 /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ {
69 local -r log_file
="$1"
72 echo 'mac ip staleness_cur staleness_avg age freq dist'
73 echo '--- -- ------------- ------------- --- ---- ----'
74 sort -n -k 1 "$log_file" \
75 |
awk -v now
="$(date '+%s')" \
82 interval[mac, ip, intervals[mac, ip]++] = ts - seen_last[mac, ip]
84 if (!seen_last[mac, ip] || ts > seen_last[mac, ip] ) seen_last[mac, ip] = ts
85 if (!seen_first[mac, ip] || ts < seen_first[mac, ip]) seen_first[mac, ip] = ts
90 split(key, macip, SUBSEP)
93 staleness_cur = now - seen_last[mac, ip]
94 age = now - seen_first[mac, ip]
95 dist = 100 * (freq[mac, ip] / NR)
97 for (i=1; i<=intervals[mac, ip]; i++)
98 intervals_sum += interval[mac, ip, i]
99 staleness_avg = intervals_sum / intervals[mac, ip]
103 sprintf("%d", staleness_cur), \
104 sprintf("%d", staleness_avg), \
105 sprintf("%d", age), \
131 log_file
='/dev/stdout'
133 if [[ -n "$2" ]]; then
135 if [[ -n "$3" ]]; then
139 debug
'(>) log | interval:"%s" log_file:"%s"' "$interval" "$log_file"
140 log
"$interval" "$log_file"
141 debug
'(.) log | interval:"%s" log_file:"%s"' "$interval" "$log_file"
144 log_file
='/dev/stdin'
145 if [[ -n "$2" ]]; then
148 debug
'(>) status | log_file:"%s"' "$log_file"
150 debug
'(.) status | log_file:"%s"' "$log_file"
153 error
'Unknown command: "%s"' "$cmd"
This page took 0.082641 seconds and 3 git commands to generate.