You are not logged in.
Hi
I'm looking for a command-line program which can show whether or not my network interface has an IP address or not. It could use "ifconfig" to extract the information, I don't really care, but it has to be *fast* and it cannot require root access.
Thanks
Offline
What's so wrong with ifconfig itself then?
Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy
Offline
The thing is that I will be writing an xmobar plugin which will simply run this command and do something special depending on whether I have an IP or not.
Offline
What about the following?
ifconfig wlan0 | grep -w inet
Obviously you'll need to substitute your own wireless interface for
wlan0.
If you have an IP address you'll get a result. If you don't you won't.
Last edited by madalu (2009-06-07 16:36:16)
Offline
Thanks, but if someone knows some sed/awk magic to get only the interface's IP address, that would be great!
EDIT:
Using the little "sed" I know, this is what I came up with:
ifconfig wlan0 | grep -w inet | sed "s/^\\s*//" | sed "s/^inet addr://" | sed "s/\\s.*//"
EDIT2:
Now I have this reasonable command:
ifconfig wlan0 | grep -w inet | sed "s/.*inet addr:\([^ ]*\).*.*/\1/"
Last edited by skorpan (2009-06-07 16:44:19)
Offline
Does this work?
tail -n1 /proc/net/arp | cut -d' ' -f1
Offline
Does this work?
tail -n1 /proc/net/arp | cut -d' ' -f1
Unfortunately, that's not my IP address (at least not on my machine), but the IP address of the gateway.
Offline
Ah yeah, I didn't look carefully at it.
BTW sed can do something cool with the t command, if the substitution failed it can print something else
ifconfig eth0 | sed -n '1{s/.*inet addr:\([0-9.]*\).*/\1/p;t;s/.*/Disconnected/p}'
Offline
[will@ghastly ~]$ ifconfig | grep inet | awk '{print $2}' | cut -d':' -f2
192.168.1.12
127.0.0.1
Offline