Last active
June 5, 2024 19:58
-
-
Save jdwhite/2b26f5f0b5dbc2c11d1558b05837e1b3 to your computer and use it in GitHub Desktop.
SSH ProxyCommand to connect to UTM guest on shared network.
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 | |
# | |
# Determine current IP address of the UTM guest VM and nc to it. | |
# Jason White (jdwhite@menelos.com) | |
# 2024-06-05 | |
# | |
# Note: VMs must be named the same as the SSH hostname. | |
# | |
# ~/.ssh/config entry for host as follows: | |
# | |
# Host utmguest-1 utmguest-2 utmtest-3 | |
# ProxyCommand proxy-utm-guest %h %p | |
# | |
# NetCat path. | |
NC=/usr/bin/nc | |
VM_NAME=$1 | |
VM_PORT=${2:-22} | |
VM_BASEPATH="${HOME}/Library/Containers/com.utmapp.UTM/Data/Documents" | |
VM_CONFIG="${VM_BASEPATH}/${VM_NAME}.utm/config.plist" | |
if [ ! -r "${VM_CONFIG}" ]; then | |
echo "No VM with that name - exiting." >&2 | |
exit 1 | |
fi | |
# This will not handle VMs with multiple network interfaces well. | |
MACADDR=$(grep -Eio '(([0-9A-F]{2}:){5}[0-9A-F]{2})' ${VM_CONFIG} | tr 'A-Z' 'a-z') | |
if [ ! -n "${MACADDR}" ]; then | |
echo "Unable to obtain MACADDR -- exiting" >&2 | |
exit 1 | |
fi | |
# Scan ARP cache on bridge interfaces to find matching IP address. | |
for bridge in $(ifconfig -l -X bridge\*); do | |
IPADDR=$(arp -ani ${bridge} | awk -v mac="${MACADDR}" ' $4==mac {print $2}' | tr -d '()') | |
if [ -n "$IPADDR" ]; then | |
exec ${NC} -4n ${IPADDR} ${VM_PORT} | |
break | |
fi | |
done | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment