Skip to content

Instantly share code, notes, and snippets.

@pstray
Last active August 17, 2018 13:21
Show Gist options
  • Save pstray/7017fac7f4cf754e990a39b28f44be66 to your computer and use it in GitHub Desktop.
Save pstray/7017fac7f4cf754e990a39b28f44be66 to your computer and use it in GitHub Desktop.
A small script for use in ssh-config Match statements.
#! /bin/bash
#
# Usage in ~/.ssh_config, usefull RPi defaults:
#
# Match exec "ether-match %h 'b8:27:eb:*'"
# User pi
# ForwardX11 no
# CheckHostIP no
# StrictHostKeyChecking no
host="$1"
if [ -z "$host" ]; then
exit 1
fi
if [[ "$host" =~ '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' ]]; then
ip="$host"
else
ip=$(getent ahostsv4 "$host" | head -n 1 | awk '{ print $1 }')
fi
arp_hwaddr=$(arp -n | awk '$1 == "'"$ip"'" && $2 == "ether" { print $3 }')
if [ -z "$arp_hwaddr" ]; then
ping -c1 -W 1 -q -n "$ip" >& /dev/null 2>&1
arp_hwaddr=$(arp -n | awk '$1 == "'"$ip"'" && $2 == "ether" { print $3 }')
fi
if [ -z "$arp_hwaddr" ]; then
exit 1
fi
for ether in "$@"; do
if [[ "$arp_hwaddr" == $ether ]]; then
exit 0
fi
done
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment