#!/usr/bin/env bash
# Run arping in duplicate address detection mode for specified destination

while getopts 'I:d:' OPT; do
  case "$OPT" in
    I) interface="$OPTARG";;
    d) destination="$OPTARG";;
  esac
done

if [[ -z "$interface" || -z "$destination" ]]; then
  echo "Usage: $0 -I device -d destination"
  exit 1
fi

output=$(arping -D -I $interface -c 2 $destination)

if [ $? -eq 0 ]; then
  echo "ERROR: Duplicate address detected for $destination"
  echo "$output"
  exit 2
else
  echo "OK: Reply received for $destination"
  exit 0
fi

#!/usr/bin/env bash
#
# Send 2 ARP REQUEST broadcast packets and alert if:
#   1. There are no replies.
#   2. There is more than 1 MAC address in the replies.

while getopts 'I:d:' OPT; do
  case "$OPT" in
    I) interface="$OPTARG";;
    d) destination="$OPTARG";;
  esac
done

if [[ -z "$interface" || -z "$destination" ]]; then
  echo "Usage: $0 -I device -d destination"
  exit 1
fi

output=$(arping -b -c 2 -I $interface $destination)

if [ $? -ne 0 ]; then
  echo "ERROR: No ARP replies for destination: $destination"
  echo "$output"
  exit 2
fi

mac_address=$(echo "$output" | grep 'reply from' | awk '{ print $5 }' | sort | uniq)
if [[ -z "$mac_address" ]]; then
  echo "WARN: Error parsing output for MAC addresses:"
  echo "$output"
  exit 1
fi

num_address=$(echo "$mac_address" | wc -l)
status="Received replies from ${mac_address//$'\n'/,} for destination: $destination"

if [ $num_address -ne 1 ]; then
  echo "ERROR: $status"
  echo "$output"
  exit 2
else
  echo "OK: $status"
  exit 0
fi


Fix script so it actually works
Fix script so it actually works
