Linux : I programatically changed my default route to eth0 but now I can't ping, any suggestions?

StackOverflow https://stackoverflow.com/questions/22748511

سؤال

I'm writing a program for my embedded Debian Linux system. It has a cell modem that is brought up as ppp0 and also has eth0 that may or may not be plugged in. How I want the system to work is for it to use ppp0 as the default route, unless it can establish a connection to the outside world over eth0, in that case eth0 should be made the default route. The eth0 cable could be unplugged at any time. To do this I've written a C++ program that uses curl to try and establish a connection to a public web server over eth0. If successful it then tries to go and make the default route eth0. I've borrowed some code from the Linux pppd project to manipulate the routing information. The system boots up with ppp0 active. I then run my program, and it detects it can make a connection via eth0 (as the cable is plugged in), and it then changes the default route to eth0. After that networking somehow stops working, I can no longer ping and public servers. Somehow my changing the route has killed networking. Before I run my program, the output of "route" is:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
80.sub-66-174-2 *               255.255.255.255 UH    0      0        0 ppp0
192.168.98.0    *               255.255.255.0   U     0      0        0 eth0
loopback        *               255.0.0.0       U     0      0        0 lo
default         *               0.0.0.0         U     0      0        0 ppp0

After I run my program, the output of "route" is:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
66.174.217.80   *               255.255.255.255 UH    0      0        0 ppp0
192.168.98.0    *               255.255.255.0   U     0      0        0 eth0
loopback        *               255.0.0.0       U     0      0        0 lo
default         *               0.0.0.0         U     0      0        0 eth0

So I can't see any obvious signs of what's wrong. The code is posted at http://pastebin.com/AwKdvSf1

Thanks for any help or insights on how to fix this.

هل كانت مفيدة؟

المحلول

your default gateway ip address is not defined.

type route add default gw <gateway ip> as root (or sudo)

NOTE: using dhcp to assign ip settings would be best way I think to solve your problem otherwise you'll have to define the default route statically.

نصائح أخرى

You'll have to add DEFROUTE=no in your ppp0 configuration.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top