You are not logged in.

#1 2009-06-07 16:16:43

skorpan
Member
Registered: 2007-11-09
Posts: 81

Fast command-line program which displays connectivity

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

#2 2009-06-07 16:23:48

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: Fast command-line program which displays connectivity

What's so wrong with ifconfig itself then?


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

#3 2009-06-07 16:30:22

skorpan
Member
Registered: 2007-11-09
Posts: 81

Re: Fast command-line program which displays connectivity

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

#4 2009-06-07 16:35:26

madalu
Member
Registered: 2009-05-05
Posts: 217

Re: Fast command-line program which displays connectivity

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

#5 2009-06-07 16:37:30

skorpan
Member
Registered: 2007-11-09
Posts: 81

Re: Fast command-line program which displays connectivity

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

#6 2009-06-07 17:14:28

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Fast command-line program which displays connectivity

Does this work?

tail -n1 /proc/net/arp | cut -d' ' -f1

Offline

#7 2009-06-07 17:17:45

skorpan
Member
Registered: 2007-11-09
Posts: 81

Re: Fast command-line program which displays connectivity

Procyon wrote:

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

#8 2009-06-07 19:05:18

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Fast command-line program which displays connectivity

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

#9 2009-06-07 21:01:56

whipsch
Member
Registered: 2009-06-04
Posts: 4

Re: Fast command-line program which displays connectivity

[will@ghastly ~]$ ifconfig | grep inet | awk '{print $2}' | cut -d':' -f2
192.168.1.12
127.0.0.1

Offline

Board footer

Powered by FluxBB