Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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?
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:
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.
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.
#!/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
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.)
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.)
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.