Skip to content

Instantly share code, notes, and snippets.

@gmzi
Created May 13, 2022 18:21
Show Gist options
  • Save gmzi/4567e41b7e7ef098cddb5e60fb11e158 to your computer and use it in GitHub Desktop.
Save gmzi/4567e41b7e7ef098cddb5e60fb11e158 to your computer and use it in GitHub Desktop.
Find device by name in .local network, connect via ssh
# This script finds a device in local network by its name (using "devicename.local"), and
# then connects to it via ssh. Default device is "rainbow", other device name can be
# passed as a parameter.
# USAGE: ./connect.sh [machine_name]
#!/bin/sh
# input machine name, or use default:
TARGET="$1"
if [ "$TARGET" = "" ] ; then
TARGET="rainbow"
fi
# find IP of target device:
CONNECTION="$(arp $TARGET.local)"
# Clean IP from non useful data:
IP_RAW="$(echo "$CONNECTION" | grep -E "([0-9]{1,3}[\.]){3}[0-9]{1,3}" | awk '{print $2}')"
# Remove parentheses from IP address:
IP="$(echo "$IP_RAW" | tr -d '()')"
# connect to device via ssh:
ssh $TARGET@$IP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment