LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   best method to determine network devices in a script? (https://www.linuxquestions.org/questions/linux-newbie-8/best-method-to-determine-network-devices-in-a-script-697140/)

agrestic 01-13-2009 09:37 PM

best method to determine network devices in a script?
 
Hi. Let me start by saying I googled for answers to both of these for about a half hour, to no avail. I found related topics; got close! But, alas, no cigar. As part of a small script I'm using the following to get the name of my network interface, "eth0", and tell it to another command.
Code:

iface=$(ifconfig -s | head -n 2 | tail -n 1 | head -c 8)
My cousin tested it for me on his machine (different distro), but the head-tail-head doesn't find the same field since the file contents aren't identical. Is there a way to retrieve the device name regardless of what distro you use?

Also, I'm retrieving my gateway's internal IP using:
Code:

nameserver=$(cat /etc/resolv.conf | tail -c14)
Obviously resolv.conf is different, even between Sabayon 4 & ubuntu intrepid (my last config; haven't tested it w/ Slack 12.2 yet). So in a similar vein, is there a way to retrieve the gateway's internal IP regardless of distro?

Thanks for your time.

chrism01 01-13-2009 11:09 PM

If you ust want it for your system, just run the first parts

/sbin/ifconfig -s

or
/sbin/route

and
cat /etc/resolv.conf

and adapt accordingly.
If you truly want intelligent parsing, I'd recommend using Perl.

farslayer 01-14-2009 07:32 AM

querying /etc/resolv.conf for the gateway address is not the best approach.. that is the DNS server and is not always the gateway. you would be better off querying with route.

dfgw=$(route | grep default | head -c 30 | tail -c 14)

agrestic 01-15-2009 05:15 AM

Python's tough for me, so Perl's out. :D Route, otoh, suits me. Thanks guys!


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