LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 05-08-2011, 09:10 AM   #1
Kernel Johnson
LQ Newbie
 
Registered: Jul 2007
Posts: 8

Rep: Reputation: 0
Lightbulb How to lookup names in LAN


So here are two machines, one fixed and one laptop. The fixed has one cable bound IP, the laptop either the cable bound or WLAN IP.

I can edit the laptop's /etc/hosts to match the hostname to the IP, but the other way around is troublesome, as the laptop's IP changes depending on whether it is plugged or wireless.

What software do I need to install in order to automatically update the IP of the laptop on the fixed machine, depending on the laptops interface?

P.S.: both are latest Ubuntu
 
Old 05-08-2011, 09:13 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Hello,

I know on my personal LAN, I have all of my hostnames bound to each MAC address on my router, with static routes; Have you thought of doing something like that?

Josh
 
Old 05-08-2011, 09:19 AM   #3
Kernel Johnson
LQ Newbie
 
Registered: Jul 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by corp769 View Post
Hello,

I know on my personal LAN, I have all of my hostnames bound to each MAC address on my router, with static routes; Have you thought of doing something like that?

Josh
Well, actually this is somewhat the situation right now:

router: X.X.X.1
fixed cable: X.X.X.2
laptop cable: X.X.X.3
laptop wireless: X.X.X.4

If I was to assign the IP X.X.X.3 to both the MAC of the laptop's wireless and cable, then there will be a conflict. Or is this not what you suggest?
 
Old 05-08-2011, 09:33 AM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
The MAC addresses will not be the same for both devices on your laptop. Look at the output of ifconfig -a in your terminal, and you will see what I am talking about.
 
Old 05-08-2011, 09:44 AM   #5
Kernel Johnson
LQ Newbie
 
Registered: Jul 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by corp769 View Post
The MAC addresses will not be the same for both devices on your laptop. Look at the output of ifconfig -a in your terminal, and you will see what I am talking about.
That's right. But if I was to assign both MACs the X.X.X.3, then whenever I plug in the cable without disconnecting the WLAN first, there will be an IP conflict. So I wondered what software could circumvent such a clash on a higher level, actually mapping the hostnames dynamically to the IPs in an event driven fashion.

Sure, you could write a script that disconnects the WLAN whenever the cable is plugged. On the downside, if I walk around with that thing and start a download, go back to my desk and plug in the cable, the download will cease.
 
Old 05-08-2011, 06:54 PM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by Kernel Johnson View Post
What software do I need to install in order to automatically update the IP of the laptop on the fixed machine, depending on the laptops interface?
A very simple method is to use a heartbeat ping to see if the laptop is accessible, and modify /etc/hosts whenever the situation changes.

Assume you have in /etc/hosts something like
Code:
a.b.c.d    laptop
1.2.3.4    laptop-cable
5.6.7.8    laptop-wlan
Then, save the following as /usr/local/laptop-ip:
Code:
#!/bin/bash
exec 2>&1

# Check once every interval seconds
interval=30

# Name, normal name, and alternate name in /etc/hosts.
host="laptop"
host_default="laptop-cable"
host_alternate="laptop-wlan"

# Ignore HUP and USR1 signals (if run as a service).
trap "" HUP USR1

# Escape dots in the names.
host="${host//./\\.}"
host_default="${host_default//./\\.}"
host_alternate="${host_alternate//./\\.}"

while [ 1 ]; do
    ip=`sed -n -e "/[\t ]$host[\t ]*$/ s/^[\t ]*\([^\t ]*\).*$/\1/p" /etc/hosts`
    ip_default=`sed -n -e "/[\t ]$host_default[\t ]*$/ s/^[\t ]*\([^\t ]*\).*$/\1/p" /etc/hosts`
    ip_alternate=`sed -n -e "/[\t ]$host_alternate[\t ]*$/ s/^[\t ]*\([^\t ]*\).*$/\1/p" /etc/hosts`

    if [ "$ip_default" == "" -o "$ip_alternate" == "" ]; then
        # Default and/or alternate IP is not specified. Abort.
        exit 1
    fi

    if ping -q -c 1 -w 1 $ip_default &>/dev/null ; then

        # Default IP is accessible. Update /etc/hosts if necessary.
        if [ "$ip" != "$ip_default" ]; then
            sed -e "/[\t ]$host[\t ]*$/ s|^[\t ]*[^\t ]*|$ip_default|" -i /etc/hosts
        fi

    else

        # Use alternate IP. Update /etc/hosts if necessary.
        if [ "$ip" != "$ip_alternate" ]; then
            sed -e "/[\t ]$host[\t ]*$/ s|^[\t ]*[^\t ]*|$ip_alternate|" -i /etc/hosts
        fi
    fi

    # Sleep until its time to check again.
    sleep $interval || exit $?
done
Then, you can start that in the background with
Code:
sudo bash -c '( setsid /usr/local/bin/laptop-ip </dev/null >/dev/null & )'
or, in a service script with
Code:
( setsid /usr/local/bin/laptop-id </dev/null >/dev/null & )
The script periodically pings the preferred IP address -- the cable one. If successful, is makes sure /etc/hosts uses that for the laptop. Otherwise, the script makes sure /etc/hosts uses the alternate IP address -- the WLAN one -- for the laptop.

The script only pings the cable IP address, because that should have minimal side effects. (If you ping the WLAN address, your WLAN router may not be able to sleep, wasting power.)

Hope this helps.
 
1 members found this post helpful.
Old 05-08-2011, 07:06 PM   #7
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Quote:
Originally Posted by Nominal Animal View Post
A very simple method is to use a heartbeat ping to see if the laptop is accessible, and modify /etc/hosts whenever the situation changes.

The script periodically pings the preferred IP address -- the cable one. If successful, is makes sure /etc/hosts uses that for the laptop. Otherwise, the script makes sure /etc/hosts uses the alternate IP address -- the WLAN one -- for the laptop.

The script only pings the cable IP address, because that should have minimal side effects. (If you ping the WLAN address, your WLAN router may not be able to sleep, wasting power.)

Hope this helps.
Yup, you definitely won this thread.....
 
Old 05-09-2011, 12:11 PM   #8
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Red face

Quote:
Originally Posted by corp769 View Post
Yup, you definitely won this thread.....
Sarcasm?

The easiest option should be to just use failover routing on both machines:
Code:
route add default gw cable-router-IP dev eth0
route add default gw wlan-router-IP dev wlan0
echo 5 > /proc/sys/net/ipv4/route/gc_timeout
which should work for all connections (not just to the fixed machine) on the laptop, with a five-second delay in failover. On the fixed machine, for each laptop IP address, add
Code:
route add host laptop-IP gw cable-router-IP dev eth0
route add host laptop-IP gw wlan-router-IP dev wlan0
echo 5 > /proc/sys/net/ipv4/route/gc_timeout
which should create a failover route to the laptop with a five-second delay. I recommend using less than ten seconds, so that connections don't have enough time to die.

Right now I'm unable to test whether this works (or needs additional tuning), but it should work better than anything else (except possibly augmenting this with iptables). I have no idea why I didn't suggest this earlier; I guess I was in I'm-gonna-write-a-script mode.
 
Old 05-09-2011, 08:55 PM   #9
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
LOL.... I get in those moods sometimes too man. But yeah, I was going more towards the route add host method, until you posted that script, which then I just stopped.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ACL for Bind 9 only allowing host names lookup for LAN j.smith1981 Linux - Server 3 02-28-2011 10:53 AM
Machine names don't work across LAN stonehinge03 Linux - Newbie 5 03-23-2010 11:54 PM
Constant LAN addresses for names? (Fedora) GregLee Linux - Networking 3 07-15-2007 11:30 PM
DNS Problem - Can't resolve LAN IP's to names crazee64 Linux - Networking 3 07-12-2006 04:26 PM
slow lookup of names (resolution¿?) hgb Linux - Networking 2 03-30-2006 12:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 07:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration