Last active
August 2, 2020 14:13
-
-
Save shaunoneil/f84b5c84203a7cc2dbc185a89d70a403 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SNAME=$(uname -s) | |
# Assume a default interface. eth0 isn't so predictable on linux anymore, TODO a better one there. | |
case $SNAME in | |
"Darwin") IFACE=${1-en0};; | |
"Linux") IFACE=${1-eth0};; | |
*) echo Sorry, I was too lazy to add your OS; exit 1;; | |
esac | |
# pull my own LL address from ifconfig. by OS again because ifconfig has slightly different format. | |
case $SNAME in | |
"Darwin") MYSELF=$(ifconfig $IFACE | grep fe80 | awk '{print $2}' | sed 's|%.*||');; | |
"Linux") MYSELF=$(ifconfig $IFACE | grep fe80 | awk '{print $3}' | sed 's|/.*||');; | |
esac | |
# Sanity check | |
if [ -z "$MYSELF" ]; then | |
echo Could not determin own linklocal address. | |
exit 1 | |
fi | |
# ping v6 broadcast (ff02::1) and remove myself from the reults. | |
# -c 3 because for a single ping, ping stops listening after the first return, which is usually the local machine | |
BCAST=ff02::1 | |
case $SNAME in | |
# broadcast ping | ..bytes from | cut times | tidy up | remove myself | and output | |
"Darwin") NEIGHBOURS=$(ping6 -c 3 -I $IFACE $BCAST | grep ^16 | sed 's|%.*||' | sort|uniq | grep -v $MYSELF | awk '{print $4}');; | |
"Linux") NEIGHBOURS=$(ping6 -c 3 -I $IFACE $BCAST | grep ^64 | sed 's|: .*||' | sort|uniq | grep -v $MYSELF | awk '{print $4}');; | |
esac | |
# now we're getting somewhere! | |
NDCOUNT=$(echo $NEIGHBOURS | wc -w | tr -d " ") # count results | |
if [ $NDCOUNT -eq 1 ]; then | |
ssh pi@$NEIGHBOURS%$IFACE # do eeet | |
else | |
echo Could not guess, $NDCOUNT neighbours # or not | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment