Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Networking
User Name
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.


  Search this Thread
Old 05-05-2006, 10:45 PM   #1
Registered: Apr 2004
Posts: 340

Rep: Reputation: 30
need ip address and gateway in a particular format

ifconfig gives me a lot of information but not exactly how I want it.

My linux PC is a dhcp client.
Assuming that my ip address is and the gateway is

Can you help me with a script that determines my ip address and gateway and returns it in the format


I need to pass this as a value to another script.

Old 05-06-2006, 12:04 AM   #2
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You can get the ip address of the interface using ifconfig or ip. The route you will need to use another source, either the "route" command or from the /etc/sysconfig/network/routes (might be in /etc/routes instead).
For example to extract the second column of the routes file to obtain the default gateway:
awk '/default/{print $2}' /etc/sysconfig/network/routes
or using the "route" command:
/sbin/route | awk '/default/{print $2}'

To extract the ipaddress from "/sbin/wlan0" for example:
/sbin/ifconfig wlan0 | sed -n '2s/.*inet addr:\([^ ]*\).*/\1/p'
In a script you may have something like this:
/sbin/ifconfig $interface | sed -n '2s/.*inet addr:\([^ ]*\)/\1/p'
The outputs can be assigned to variables and reassembled like this
Old 05-06-2006, 12:39 AM   #3
Registered: Jul 2004
Location: louisiana
Distribution: fedora 4/kubuntu
Posts: 116

Rep: Reputation: 15
gateway and ip

Just a little something to get you started:

ipAddress=`/sbin/ifconfig | grep 'inet addr:' | grep -v '' | cut -d: -f2 | grep -o '[1-9.]*'`

echo $ipAddress
You can get gateway by netstat -nr, do a little parsing, then just concatenate your variables to get your required format. Let me know if you need more help.
Old 05-06-2006, 10:45 AM   #4
Registered: Jul 2004
Location: louisiana
Distribution: fedora 4/kubuntu
Posts: 116

Rep: Reputation: 15
Sorry jschiwal, I had not refreshed my page to see that you had posted already and to note an excellent solution. Again, my apologies.


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
Getting gateway address in C/C++ jormel Programming 2 01-13-2005 07:24 AM
GATEWAY address emailssent Linux - Networking 2 09-30-2004 06:30 AM
gateway address amanjsingh Linux - Newbie 3 06-05-2004 06:42 AM
What is Gateway address and what is Network address? abefroman Linux - Networking 1 03-17-2004 09:25 PM
Gateway Address BittaBrotha Linux - Networking 3 10-13-2003 08:56 AM > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 03:47 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration