LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-13-2009, 10:37 PM   #1
agrestic
Member
 
Registered: Jan 2009
Location: atlanta, ga, usa
Distribution: sabayon 5, slack64, Lenny, LFS 6.4 user # 20665
Posts: 61

Rep: Reputation: 16
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.
 
Old 01-14-2009, 12:09 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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.
 
Old 01-14-2009, 08:32 AM   #3
farslayer
LQ Guru
 
Registered: Oct 2005
Location: Willoughby, Ohio
Distribution: linuxdebian
Posts: 7,232
Blog Entries: 5

Rep: Reputation: 190Reputation: 190
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)
 
Old 01-15-2009, 06:15 AM   #4
agrestic
Member
 
Registered: Jan 2009
Location: atlanta, ga, usa
Distribution: sabayon 5, slack64, Lenny, LFS 6.4 user # 20665
Posts: 61

Original Poster
Rep: Reputation: 16
Python's tough for me, so Perl's out. Route, otoh, suits me. Thanks guys!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Determine order of input devices DeNayGo Linux - Hardware 1 03-07-2007 01:23 AM
Determine what encryption method is used jonlake Linux - General 1 09-27-2006 01:28 AM
method to authenticate devices ddaas Linux - Security 7 02-23-2005 03:14 AM
determine type of device for USB devices lyar1031 Linux - Newbie 2 07-29-2004 09:02 PM
How to determine cdrom devices Vookimedlo Programming 1 07-09-2004 09:52 AM


All times are GMT -5. The time now is 11:08 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration