Results 1 to 3 of 3

Thread: Change hosts file by network connection?

  1. #1
    Join Date
    May 2008
    Beans
    52

    Change hosts file by network connection?

    Is it possible to have different /etc/hosts file for different network connections without having to go in and change it every time?

    The why: I have dyndns and port forwarding to get to my desktop. My laptop is sometimes on the same network, and sometimes not. Also, sometimes the dyndns doesn't update properly, or the outside connection is down, but I want to get to my desktop (and I'm too lazy to walk up the stairs). I'd like to be able to keep one set of bookmarks, ssh command aliases, etc. that would always get to it the fastest and most reliable way possible.

    Is this possible?

  2. #2
    Join Date
    May 2008
    Beans
    52

    Re: Change hosts file by network connection?

    Bamp. Found this: http://www.sysadminsjourney.com/cont...twork-location
    So I need to place a script in /etc/NetworkManager/dispatcher.d that will figure out if I'm at home (or another network where I would know the desktop's IP), and change the hosts file accordingly.
    Some semi-pseudocode:
    Code:
    if [ OnHomeNetwork ]; then
    	#Use special host file
    	echo "mydomain.homelinux.org	192.168.1.200" > /etc/hosts
    else
    	#Use the normal host file
    	cp /etc/hosts.bak /etc/hosts
    fi
    So now the question is, how do I make the shell script know that I'm on my home network?

  3. #3
    Join Date
    May 2008
    Beans
    52

    Re: Change hosts file by network connection?

    I got it! Solution below. It isn't perfect; if I'm ever in range of my home network and not connected to it (probably not going to happen), it'll give me a bad host. But other than that, this should work:

    Code:
    wifi=`iwlist wlan0 scan|grep SSID`
    if [[ $wifi == *MiWiFi* ]]
    then
    	#Use special host file
    	echo "mydomain.homelinux.org    192.168.1.200" >> /etc/hosts
    else
    	#Use the normal host file
    	cp /etc/hosts.bak /etc/hosts
    fi
    Just replace MiWiFi with my wireless network SSID, and replace mydomain.homelinux.org with my hostname, then put it in /etc/NetworkManager/dispatcher.d and I'm good! I'm hoping the NetworkManager scripts run as root, otherwise it won't work...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •