LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Problem with netstat? (https://www.linuxquestions.org/questions/linux-networking-3/problem-with-netstat-24785/)

NiM 07-01-2002 05:25 PM

Problem with netstat?
 
Hi,

if I execute:

Code:

[root@chips /root]# netstat -rn                                                Kernel IP routing table
Destination    Gateway        Genmask        Flags  MSS Window  irtt Iface
192.168.0.0    0.0.0.0        255.255.255.0  U        40 0          0 eth0
80.195.244.0    0.0.0.0        255.255.255.0  U        40 0          0 eth1
127.0.0.0      0.0.0.0        255.0.0.0      U        40 0          0 lo
0.0.0.0        80.195.244.1    0.0.0.0        UG      40 0          0 eth1
0.0.0.0        192.168.0.1    0.0.0.0        UG      40 0          0 eth0

That's all correct, my cable internet is on eth1, and linux routes all internet traffic to it fine.

Than, I execute a command that should find my internet IP, which changes dynamicly and is assingned via DHCP. The correct IP is however shown in ifconfig eth1...

Code:

[root@chips /root]# netstat -rn | egrep ^0.0.0.0 | sed -e "s,.* ,,g" | xargs /sbin/ifconfig | grep "inet.addr" | sed -e "s,.*addr:,," -e "s, .*,,"
It returns nothing, and strangely, my internet stops working, so I check out the routing tables again...

Code:

[root@chips /root]# netstat -rn                                                Kernel IP routing table
Destination    Gateway        Genmask        Flags  MSS Window  irtt Iface
192.168.0.0    0.0.0.0        255.255.255.0  U        40 0          0 eth0
127.0.0.0      0.0.0.0        255.0.0.0      U        40 0          0 lo
62.0.0.0        0.0.0.0        255.0.0.0      U        40 0          0 eth1
0.0.0.0        192.168.0.1    0.0.0.0        UG      40 0          0 eth0

Huh? what happned there? The IP finding code I used used to work, however, my eth1 recently changed IRQs and I had to set it up again, is there something I've forgotten to do when setting it up again, oris there an error in the IP finding code?

Thanks for your help,
- Nick

Mik 07-02-2002 03:00 AM

I don't know why your routing table gets screwed up, but as far as I can see the code you are using to get your ip address won't work. Here is why:

First you run 'netstat -rn | egrep ^0.0.0.0'. This will give you every line starting with 0.0.0.0 which will be:

0.0.0.0 80.195.244.1 0.0.0.0 UG 40 0 0 eth1
0.0.0.0 192.168.0.1 0.0.0.0 UG 40 0 0 eth0

Then you pipe it through 'sed -e "s,.* ,,g"'. This will replace any set of characters that is followed by a space with nothing. So what you will have left is:

eth1
eth0

Next you pass it to /sbin/ifconfig using the xargs command. Now since you got two lines back instead of just one. The command that will be run will be:

/sbin/ifconfig eth1 eth0

If you try running that command you should get an error. So it wouldn't matter how you parse the rest of the information from it. I assume where the code used to work you would had only one interface.

But you said you get the ip through dhcp. I use dhcpcd and this is how I get the ip address that was retrieved.

export `grep IPADDR /etc/dhcpc/dhcpcd-eth?.info`
This ofcourse assumes there is only one interface which retrieves it's ip through dhcp. You can replace the ? with the right number if you want it to get a specific one. That command sets the variable IPADDR so you can then just use $IPADDR for whatever you wanted to do with it.

NiM 07-02-2002 05:50 AM

Thanks!
 
Hi,

Hmm, maybe before, I only had 1 line beginning with 0.0.0.0, it's supposed to find the internet NIC and grab the IP from it...

Still, your code's much easier :) And now at least I can echo it when I need it!

Cheers & Thanks for your help!

- Nick


All times are GMT -5. The time now is 09:46 AM.