Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Test internet connection existence

  1. #1
    Join Date
    Mar 2005
    Beans
    51

    Test internet connection existence

    I'm looking for a standard way to test the existance of an internet connection under Linux. Is anyone aware of any standard methods?

  2. #2
    Join Date
    Apr 2007
    Location
    std::usa::ar::bentonville
    Beans
    175
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Test internet connection existence

    You could always ping a website like google.
    We must be the change we wish to see - Gandhi

  3. #3
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Test internet connection existence

    Usually, I just ping a site. Actually, I use google because it is always there.

  4. #4
    Join Date
    Mar 2005
    Beans
    51

    Re: Test internet connection existence

    Yea, I guess that's an option. I was wondering because I want to pop up a dialog if an internet connection isn't detected.

  5. #5
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Test internet connection existence

    Quote Originally Posted by Mlehliw View Post
    Yea, I guess that's an option. I was wondering because I want to pop up a dialog if an internet connection isn't detected.
    I wrote a program that did exactly that. I'll go look for it.

  6. #6
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Test internet connection existence

    Better solution in Bash (the old program was for Windows)
    Code:
    #! /bin/bash
    
    main()
    {
        ping -c 64.233.169.103 > /dev/null
        if [ "$?" -eq 0 ] 
        then
            zenity --info --text "Internet Lost";
        else
            sleep 30
            main
        fi
    }
    
    main
    You can tweak this to your liking. It pings google, and if it fails, it will give a popup notifying you, if it suceeds, it waits 30 seconds and does it again.
    Last edited by LaRoza; March 22nd, 2008 at 05:16 AM.

  7. #7
    Join Date
    Apr 2009
    Beans
    24

    Re: Test internet connection existence

    Quote Originally Posted by LaRoza View Post
    Better solution in Bash (the old program was for Windows)
    Code:
    #! /bin/bash
    
    main()
    {
        ping -c 64.233.169.103 > /dev/null
        if [ "$?" -eq 0 ] 
        then
            zenity --info --text "Internet Lost";
        else
            sleep 30
            main
        fi
    }
    
    main
    You can tweak this to your liking. It pings google, and if it fails, it will give a popup notifying you, if it suceeds, it waits 30 seconds and does it again.
    actually the line ping -c 64.233.169.103

    gives you the usage because you need to specify the limit of pings to the -c flag ie:
    ping -c 1 64.233.169.103

    so in total it should look like:
    Code:
    #! /bin/bash
    
    main()
    {
        if ping -c 1 64.233.169.103 > /dev/null;then
            zenity --info --text "Internet Lost";
        else
            sleep 30
            main
        fi
    }
    
    main
    Last edited by alex_o; December 31st, 2009 at 02:31 PM. Reason: forgot code tags DOH!

  8. #8
    Join Date
    Jun 2010
    Beans
    1

    Re: Test internet connection existence

    @alex_o actually your code above does alert the user when the connection is working! The instructions in the if and else statements should be switched.

    Fabio Varesano

  9. #9
    Join Date
    Dec 2009
    Location
    germany
    Beans
    1,020
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Test internet connection existence

    hi
    you have to use: netstat -punt | grep -i VERBUNDEN ( in english i think it is CONNECTED ) in a selfmade script.
    and then, if $? is zero --> do your stuff ( 0 == VERBUNDEN || CONNECTED - depends on your language)
    --> -punt will give you the name and the process-id of the process.
    ciao
    Last edited by rnerwein; June 14th, 2010 at 07:47 PM.
    "What is the robbing of a bank compared to the FOUNDING of a bank?" Berthold Brecht

  10. #10
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: Test internet connection existence

    Quote Originally Posted by rnerwein View Post
    you have to use: netstat -punt | grep -i
    Uuuh, I don't think that this is a good idea. First of all, even if there is a connection, if there is no process at the point of time of checking this, you will receive a false negative. Then, intranet connections are also taken into account. Finally, there are pay-per-use internet connection providers which simply reroute all port-80 traffic through the connection to their payment site and block all other connections until you have bought some connectivity. Here, you get a false positive with your approach.

    Finally, for getting rid of the language problem, you can always set "LANG=C" before calling the tool to make sure that the result is in english: "LANG=C netstat -punt"

Page 1 of 2 12 LastLast

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
  •