messelink
December 10th, 2010, 02:49 PM
There may be a better way to do this, but I wasn't able to find it. What this code does is check every time a connection is made to a wireless network and comment or uncomment a line in /etc/hosts. For me it solved the problem of being able to reach a server from home or from work using the same url.
This example checks whether you are connected to wireless network MYEXAMPLE. If you are it removes the "#" from the line in /etc/hosts so that now on this machine myexample.dyndns.org will point to 192.168.1.2. If you aren't connected to MYEXAMPLE it will add a "#" in front of the line, making it a comment, and therefore myexample.dyndns.org will point to whatever DNS resolves to.
First add a line to /etc/hosts
#192.168.1.2 myexample.dyndns.org myexample
Then create two files in /etc/network/if-up.d
roaming
#!/bin/sh
# Parameter settings
WIF="wlan0"
LOCALESSID="MYEXAMPLE"
# Set locations of utils
READLINK=/bin/readlink
DIRNAME=/usr/bin/dirname
IWCONFIG=/sbin/iwconfig
AWK=/usr/bin/awk
SED=/bin/sed
# Find the absolute path this script is in
SCRIPT="`$READLINK -f $0`"
SCRIPTPATH="`$DIRNAME $SCRIPT`"
ESSID="`$IWCONFIG $WIF | $AWK -f $SCRIPTPATH/getESSID.awk`"
if [ "$ESSID" = "$LOCALESSID" ]
then
`$SED -i 's/#192\.168\.1\.2/192\.168\.1\.2/g' /etc/hosts`
else
`$SED -i '/#/!s/192\.168\.1\.2/#192\.168\.1\.2/g' /etc/hosts`
fi
and
getESSID.awk
#!/bin/awk -f
BEGIN { FS="\"" }; {print $2;exit}
Make sure both files are owned by root:root and that only "roaming" is executable and make a symbolic link to roaming in /etc/network/if-down.d.
If someone has a good idea on how to move the IP address to the parameters part of the script, I'd love to hear it.
This example checks whether you are connected to wireless network MYEXAMPLE. If you are it removes the "#" from the line in /etc/hosts so that now on this machine myexample.dyndns.org will point to 192.168.1.2. If you aren't connected to MYEXAMPLE it will add a "#" in front of the line, making it a comment, and therefore myexample.dyndns.org will point to whatever DNS resolves to.
First add a line to /etc/hosts
#192.168.1.2 myexample.dyndns.org myexample
Then create two files in /etc/network/if-up.d
roaming
#!/bin/sh
# Parameter settings
WIF="wlan0"
LOCALESSID="MYEXAMPLE"
# Set locations of utils
READLINK=/bin/readlink
DIRNAME=/usr/bin/dirname
IWCONFIG=/sbin/iwconfig
AWK=/usr/bin/awk
SED=/bin/sed
# Find the absolute path this script is in
SCRIPT="`$READLINK -f $0`"
SCRIPTPATH="`$DIRNAME $SCRIPT`"
ESSID="`$IWCONFIG $WIF | $AWK -f $SCRIPTPATH/getESSID.awk`"
if [ "$ESSID" = "$LOCALESSID" ]
then
`$SED -i 's/#192\.168\.1\.2/192\.168\.1\.2/g' /etc/hosts`
else
`$SED -i '/#/!s/192\.168\.1\.2/#192\.168\.1\.2/g' /etc/hosts`
fi
and
getESSID.awk
#!/bin/awk -f
BEGIN { FS="\"" }; {print $2;exit}
Make sure both files are owned by root:root and that only "roaming" is executable and make a symbolic link to roaming in /etc/network/if-down.d.
If someone has a good idea on how to move the IP address to the parameters part of the script, I'd love to hear it.