Skip to content

Instantly share code, notes, and snippets.

@gmzi
Created May 13, 2022 14:43
Show Gist options
  • Save gmzi/4b5e30947d1255ffa0c3ef4fdb8cd1bc to your computer and use it in GitHub Desktop.
Save gmzi/4b5e30947d1255ffa0c3ef4fdb8cd1bc to your computer and use it in GitHub Desktop.
Find and open a remote machine's localhost server
# This script is to find and open the localhost server of a target remote
# machine connected to the same network and identified by name, in cases where `mymachine.local` is not available.
# There's a default machine named "XXX", script accepts custom machine names as first argument.
# The localhost port defaults to 3000 but
# can also be passed as an argument. Script has been tested on Mac OS only.
# USAGE: ./findAndOpenLocalhost.sh <machine_name> <port_number>
#!/bin/sh
# Uppercase user input for machine name, or use default:
TARGET="$(awk '{print toupper($0)}' <<< $1)"
if [ "$TARGET" = "" ] ; then
echo "no target specified, running default: xxx"
TARGET="XXX"
fi
# Scan all machines in local network:
ALLCONNECTIONS="$(arp -a)"
# Select IP addresses only:
IPS_RAW="$(echo "$ALLCONNECTIONS" | grep -E "([0-9]{1,3}[\.]){3}[0-9]{1,3}" | awk '{print $2}')"
# Remove parentheses from IP addresses:
IPS="$(echo "$IPS_RAW" | tr -d '()')"
echo "searching target..."
SUCESS=""
# Check Server Name of each IP address, and compare it to the target:
for i in $IPS; do
CHECK=$(smbutil status $i | grep "Server:")
if [ "$CHECK" = "Server: $TARGET" ]; then
SUCESS="true"
echo "$TARGET opened in Chrome browser"
TARGETIP="$i"
URL="http://$TARGETIP:3000"
if [ "$2" != "" ]; then
URL="http://$TARGETIP:$2"
fi
open -a "Google Chrome" $URL
break
fi
done
if [ "$SUCESS" = "" ]; then
echo "Target not found. Scanned ip's: \n$IPS"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment