Active Topics

 



Notices


Reply
Thread Tools
frethop's Avatar
Posts: 283 | Thanked: 60 times | Joined on Nov 2005 @ It's dark in here. I hear laughing.
#1
Simple question: Is there a way to tell from the command shell if I a connected to a network or not?

It's probably something in /proc...but I'm not finding it. I'm hoping this works for both wireless and mobile phone connections.

Thanks.

Edit: BTW, I DID search through the forums, but did not find anything. I'll not be surprised if someone has asked this already.
 
brendan's Avatar
Posts: 531 | Thanked: 79 times | Joined on Oct 2006 @ This side of insane, that side of genius
#2
/sbin/ifconfig wlan0

will give you an IP address, if you are connected. also, the RX/TX counters will not be 0. you should not need root access for this.
__________________
Nokia n800
OS 2008
Pharos iGPS 360-BT
ElmScan 5 BlueTooth
BlackBerry Bold (9000)
AT&T Wireless
 

The Following User Says Thank You to brendan For This Useful Post:
Benson's Avatar
Posts: 4,930 | Thanked: 2,272 times | Joined on Oct 2007
#3
Originally Posted by brendan View Post
/sbin/ifconfig wlan0

will give you an IP address, if you are connected. also, the RX/TX counters will not be 0. you should not need root access for this.
Don't know what exact result you're after; if you want to use it in a shell conditional, grep it for UP, like this for wlan0:
Code:
if [ "x`/sbin/ifconfig wlan0 |grep UP`" != x ] ; then
  echo 'Connection'
else
  echo 'No connection'
fi
I don't know what interface BT connections show up as; but something like (if it were bt0):
Code:
if [ "x`/sbin/ifconfig wlan0 |grep UP`" != x ] || [ "x`/sbin/ifconfig bt0 |grep UP`" != x ] ; then
  echo 'Connection'
else
  echo 'No connection'
fi
 

The Following User Says Thank You to Benson For This Useful Post:
Posts: 474 | Thanked: 30 times | Joined on Jan 2006
#4
Originally Posted by brendan View Post
/sbin/ifconfig wlan0

will give you an IP address, if you are connected. also, the RX/TX counters will not be 0. you should not need root access for this.
This doesn't help you if you're not using wlan

Frethop, check out libconic. There's a DBUS call you can make that will report back what you're connected to (if anything). I'll try to get you a command line in a bit...
 

The Following User Says Thank You to aleksandyr For This Useful Post:
frethop's Avatar
Posts: 283 | Thanked: 60 times | Joined on Nov 2005 @ It's dark in here. I hear laughing.
#5
Thanks everyone!

Actually, I'm going to use it in a Python program --> do one thing if connected, store results for use later if not.
 
Posts: 118 | Thanked: 26 times | Joined on Feb 2008
#6
netstat -rn | grep '0\.0\.0\.0'

should work too. Basically, it checks the kernel's routing tables to see if there's a default gateway set. Without a default gateway, you can't connect to any systems outside your local ethernet segment, hence no connection. This is indifferent to the kind of connection you use - BT, WLAN or whatever.

Martin
 

The Following User Says Thank You to m_stolle For This Useful Post:
Reply


 
Forum Jump


All times are GMT. The time now is 16:48.