Skip to content

Instantly share code, notes, and snippets.

@gmzi
Created May 31, 2022 15:11
Show Gist options
  • Save gmzi/359a398d9c075c600c9b98ef69870df9 to your computer and use it in GitHub Desktop.
Save gmzi/359a398d9c075c600c9b98ef69870df9 to your computer and use it in GitHub Desktop.
Open local Flask server in remote machine
#!/bin/sh
# This script takes the IP of the local machine, then makes a URL pointing to the local Flask server (http://192.168.1.9:5000),
# opens the URL in remote machine's Chrome tab, runs the Flask server, and plays a sound when ready.
# USAGE: in Flask's project folder: ~/path-to-script/openFlaskInRemote.sh [remote_machine_name]
IP=""
# find all the active connections of the local machine,
# and find the IP address of the first one
for i in $(ifconfig -lu) ; do
if (ifconfig $i | grep -q "status: active") ; then
IP=$(ifconfig $i | grep -i "inet ")
break
fi ;
done
# Finish process if no active connection:
if [ "$IP" = "" ] ; then
echo "no active connection"
else
# trim the IP address to use only the first part:
INET=$(awk '{split($0, array); print array[2]}' <<< $IP)
# parse it to the url:
URL="http://$INET:5000"
# if other port is passed as an argument, parse it instead of the
# default one:
if [ "$1" != "" ] ; then
URL="http://$INET:$1"
fi
# open $URL
echo "$URL"
TARGET="$1"
if [ "$TARGET" = "" ] ; then
TARGET="rainbow"
fi
echo "Connecting to $TARGET..."
# 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
# CONNECT AND OPEN XXX'S LOCALHOST:
ssh $TARGET@$IP 'bash -s' < ./openUrl.sh $URL
fi
flask run -h 192.168.1.9
#!/bin/sh
# This script takes a url and opens it in a Chrome tab.
# USAGE: ./openUrl.sh <url>
# parse machine name in url:
URL="$1"
open -a "Google Chrome" $URL
echo "ready to go"
# Play sound,
afplay /System/Library/Sounds/Glass.aiff
say "code"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment