Results 1 to 7 of 7

Thread: Detecting if an ubuntu system is connected to the network

  1. #1
    Join Date
    Apr 2009
    Beans
    4

    Detecting if an ubuntu system is connected to the network

    Hello,

    I am trying to detect if my network is up and i have an internet connection.
    I have been reading about dbus, but i couldnt find enough materials to accomplish what i want.

    Any idea on this subject would be appriciated

    Thanks

  2. #2
    Join Date
    Oct 2006
    Location
    Tucson, AZ
    Beans
    1,420
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: Detecting if an ubuntu system is connected to the network

    Quote Originally Posted by barantamer View Post
    Hello,

    I am trying to detect if my network is up and i have an internet connection.
    I have been reading about dbus, but i couldnt find enough materials to accomplish what i want.

    Any idea on this subject would be appriciated

    Thanks
    It's relatively simple to write a script for this purpose
    Code:
    #!/bin/bash
    
    if ping -c 1 www.yahoo.com ; then
      echo "The Internet is available"
    else
      echo "No Internet available"
    fi
    You can easily add other commands into the "then" clause of that script to perform certain actions if the Internet is there.

    Lloyd B.
    Don't tell me to get a life.
    I had one once.
    It sucked.

  3. #3
    Join Date
    Apr 2009
    Beans
    4

    Re: Detecting if an ubuntu system is connected to the network

    thanks for the quick reply,

    I was wondering if i can do it independent from a remote server such as yahoo,google,etc..
    Checking a flag or something in the operating system maybe ?

    And i am thinking how pidgean or msn detects internet connection immediately?

  4. #4
    Join Date
    Feb 2008
    Location
    Hamilton, Canada
    Beans
    Hidden!

    Re: Detecting if an ubuntu system is connected to the network

    Pidgin and msn detect an internet connection by trying to establish a connection to their host server...

    So it's the same thing.

  5. #5
    Join Date
    Oct 2006
    Location
    Tucson, AZ
    Beans
    1,420
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: Detecting if an ubuntu system is connected to the network

    Quote Originally Posted by barantamer View Post
    thanks for the quick reply,

    I was wondering if i can do it independent from a remote server such as yahoo,google,etc..
    Checking a flag or something in the operating system maybe ?

    And i am thinking how pidgean or msn detects internet connection immediately?
    You can test whether or not your machine is getting a valid IP address (which indicates that DHCP is working), but this only tells you that you have LAN connectivity. The *only* way to verify Internet connectivity is a test such as the one I suggested. After all, even if you have LAN connectivity, and you can reach your ISP, it doesn't automatically guarantee that you can reach the Internet (Your ISP could be having problems of their own).

    Lloyd B.
    Don't tell me to get a life.
    I had one once.
    It sucked.

  6. #6
    Join Date
    Apr 2009
    Beans
    4

    Re: Detecting if an ubuntu system is connected to the network

    Thanks everybody i guess i am going to follow your way.

    But i thought about an other way which doesn`t require a remote server. I want to hear your thoughts about it.

    What if i take the output of ifconfig command and check for the keyword "Scope:link" which seems to be the difference between the results of ifconfig command when the computer is connected to the internet or not.

    eth4 Link encap:Ethernet HWaddr 00:1f:e2:4e:d0:f8
    inet addr:172.16.1.1 Bcast:172.16.1.255 Mask:255.255.255.0
    inet6 addr: fe80::21f:e2ff:fe4e:d0f8/64 Scope:Link
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:528889 errors:0 dropped:0 overruns:0 frame:0
    TX packets:203302 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:344663274 (328.6 MB) TX bytes:24042389 (22.9 MB)
    Interrupt:220 Base address:0x6000

    lo Link encap:Local Loopback
    inet addr:127.0.0.1 Mask:255.0.0.0
    inet6 addr: ::1/128 Scope:Host
    UP LOOPBACK RUNNING MTU:16436 Metric:1
    RX packets:28389 errors:0 dropped:0 overruns:0 frame:0
    TX packets:28389 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:0
    RX bytes:22615039 (21.5 MB) TX bytes:22615039 (21.5 MB)
    Any thoughts ?
    Last edited by barantamer; April 22nd, 2009 at 09:20 AM.

  7. #7
    Join Date
    Oct 2006
    Location
    Tucson, AZ
    Beans
    1,420
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: Detecting if an ubuntu system is connected to the network

    Quote Originally Posted by barantamer View Post
    Thanks everybody i guess i am going to follow your way.

    But i thought about an other way which doesn`t require a remote server. I want to hear your thoughts about it.

    What if i take the output of ifconfig command and check for the keyword "Scope:link" which seems to be the difference between the results of ifconfig command when the computer is connected to the internet or not.



    Any thoughts ?
    That isn't telling you anything out an *Internet* connection. "Scope:Link" will appear regardless of what you're connected to, be it your ISP, or just your local network.

    For example:
    Code:
    eth0      Link encap:Ethernet  HWaddr 00:16:76:93:db:f3  
              inet addr:192.168.0.75  Bcast:192.168.0.255  Mask:255.255.255.0
              inet6 addr: fe80::216:76ff:fe93:dbf3/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:1257 errors:0 dropped:0 overruns:0 frame:0
              TX packets:1122 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:1197365 (1.1 MB)  TX bytes:225013 (225.0 KB)
    In this case, I do not connect *directly* to the internet, but just to my LAN. I have to route through a gateway in order to reach the Internet. The output from ifconfig doesn't change if I disconnect the DSL cable - my machine simply has no way to see if there's a valid internet connection other than actually testing it (such as by sending a ping).

    A simpler way than "scraping" the data from ifconfig would be to check the values in "/var/run/network/ifstate" - if the interface (eth4, in your case) appears in this file, then that interface is up.

    Lloyd B.
    Don't tell me to get a life.
    I had one once.
    It sucked.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •