Bash script to see if PPP link is up...

Discussion in 'Linux Networking' started by Gabriel Michael, Aug 20, 2003.

  1. Hello,

    I'm looking for a way to have a bash shell script check to see if a
    particular ppp link is up (ppp0), and if so, output "1", if not,
    output "0"... my (messy) idea was:

    ping -c 1 host.i.want.to.ping | grep -c "0% loss"

    This outputs a "1" if the host is up... then I'd assign that value to
    a variable, and test the variable, and output 1 or 0 based on that.
    I'm sure there's a better way to do this, but my mind is blanking...

    Thanks in advance,

    Gabe
     
    Gabriel Michael, Aug 20, 2003
    #1
    1. Advertisements

  2. Gabriel Michael

    Bit Twister Guest

    Well if that is the default gateway, it tests routing, DNS, and connectivity
    through your isp gateway in one shot.

    I would add -w 2 or something to prevent longer time outs.
    Maybe increasing the -c and test for 3 good in 5 pings just incase
    there is an odd ping failure or two.

    What do you feel is wrong with it?
     
    Bit Twister, Aug 20, 2003
    #2
    1. Advertisements

  3. Read the man page for ping.

    #!/bin/sh
    if ping -qnw1 -c1 ip_address 2>&1 >/dev/null
    then
    echo "Link is up"
    else
    echo "Link is down"
    fi
     
    Floyd Davidson, Aug 20, 2003
    #3
  4. Gabriel Michael

    Alan Connor Guest


    Here's the script I use to post news with, using a ppp link:



    #!/bin/bash

    pon
    while true ; do sleep 1 ; if tail -1 /var/log/syslog \
    | grep IP 1> /dev/null ; then sleep 2 ; slrnpull --post-only ; poff ; exit 0 ; else continue ; fi ; done

    There are a lot of other ways, of course.


    Alan C
     
    Alan Connor, Aug 20, 2003
    #4
  5. Unless you have a flaky ISP with routing problems, it would be better to
    just determine if pppd is connected or not instead of constantly pinging
    an outside host (which may not like that). You cannot use ifconfig
    output to tell status of demand pppd (since ppp0 is up whether connected
    or waiting to be triggered).

    When I used demand dialup pppd, I wrote write my local IP to a file
    (/etc/ppp/stat) from /etc/ppp/ip-ip (or ip-up.local) and similarly wrote a
    zero to that file from ip-down (or ip-down.local). In SuSE ip-down.local
    was a symlink to ip-up.local and it used a case to determine if it was
    coming up or down. Then I simply checked if the contents of the
    /etc/ppp/stat file was greater than zero to tell if I was connected.

    For example /etc/ppp/ip-up.local (and ip-down.local symlink):

    #!/bin/sh
    BASENAME=${0##*/}
    INTERFACE=$1
    DEVICE=$2
    SPEED=$3
    LOCALIP=$4
    REMOTEIP=$5

    # demand pppd status
    case "$BASENAME" in
    ip-up*)
    echo $LOCALIP > /etc/ppp/stat
    # update dynamic DNS (older no-ip.com binary then)
    /usr/local/bin/noip
    ;;
    ip-down*)
    echo 0 > /etc/ppp/stat
    ;;
    *)
    ;;
    esac


    Sample shell script to tell if I was online:

    #!/bin/sh
    FILE="/etc/ppp/stat"
    if [ ! -r "$FILE" ]; then
    echo "$FILE not found or unreadable"
    exit 1
    fi
    STATUS=`cat "$FILE"`
    if [ "$STATUS" = "0" ]; then
    echo "pppd is not connected"
    else
    echo "pppd ip: $STATUS"
    fi
     
    David Efflandt, Aug 21, 2003
    #5
  6. Gabriel Michael

    JRH Guest

    This is what I use, based on script in ?Firewall or ?Masquerading HOWTO. I
    don't pretend to understand the details! It is said to work in all
    locales.

    EXTIF="ppp0" # external interface, may be empty
    IFCONFIG=/sbin/ifconfig
    AWK=/bin/awk

    # get the external IP address - it returns empty string if it is not up
    if [ -n "$EXTIF" ]; then
    EXTIP="`$IFCONFIG $EXTIF 2>/dev/null | $AWK \
    /$EXTIF/'{next}//{split($0,a,":");split(a[2],a," ");print a[1];exit}'`"
    fi
    echo " External IP: $EXTIP"

    ....John
     
    JRH, Aug 21, 2003
    #6
  7. Gabriel Michael

    Alan Connor Guest

    Wrong. I tested it before I posted.

    You should try it sometime.


    Alan C
     
    Alan Connor, Aug 22, 2003
    #7
  8. ifconfig may be able to tell if it is up, but can it tell if it is
    connected or not? For example when you use demand pppd, the ppp0
    interface appears in ifconfig, but it is not connected until there is
    traffic routed to it. Likewise if an idle time is set for a demand
    connection and it disconnects, ifconfig would still show it up. That is
    why I use ip-up & ip-down scripts (which are reliable even for demand and
    idle).
     
    David Efflandt, Aug 22, 2003
    #8
  9. Gabriel Michael

    Alan Connor Guest

    To repeat:

    If my ppp connection is not up, /sbin/ifconfig ppp0 exits with an error
    message and the exit code reads 1

    If the connection is up, then I get the full info and the exit code is 0.

    This info includes the local and remote IP addresses, which cannot be there
    until the connection is actually established, being as they are both dynamic.


    It seems that some folks have the mistaken notion that all programs and
    utilities with the same name behave just the same, even on differently
    configured OSs.

    This is not the case, and that asshole Floyd Davidson, whom I have just
    killfiled for the second time (everyone deserves a second chance, no?)

    knows this perfectly well.

    I recommend being very careful about his advice. His ego is bigger than
    his desire to accurately inform others.


    Alan C
     
    Alan Connor, Aug 22, 2003
    #9
  10. Gabriel Michael

    Lew Pitcher Guest

    Alan Connor wrote:
    [some disparaging words about another poster, which I have snipped]

    FWIW, I concur with Mr. Efflandt.

    I run a "demand connect" ppp on my home server, and your ifconfig returncode
    hack wouldn't (and doesn't) work[1] on it. In fact, the usual grep on
    ifconfig's report doesn't work[1] either. In both cases, the technique given
    falsly reports that I have a connection to the internet [2] when I do not.

    By far the best way to detect whether you have an IP connection is to
    a) place code in /etc/ppp/ip-up and /etc/ppp/ip/down that manages an
    external sentinal for the IP (and/or IPX) connections, and
    b) use external tools to test the sentinal

    This is a reliable technique that does not give false positives when using
    demand-dial ppp.



    [1] "work" as in "reliably indicate the state IP over PPP".
    [2] Notice that I said "have a connection to the internet" rather than "PPP
    is UP". PPP can be "UP" without having established /any/ connection at
    all ("Gee, I hear a dialtone on my telephone. How can ppp be 'up'?").
    Even when ppp is using the phone line, it can be "UP" without having
    established an IP link. In both cases, the suggested ifconfig techniques
    would truely report that ppp is UP, but falsly imply that IP
    connectivity had been established.

    --

    Lew Pitcher, IT Consultant, Application Architecture
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
     
    Lew Pitcher, Aug 22, 2003
    #10
  11. You are arrogant far beyond your means. I've finally put you in *my*
    kill file. Replying is futile.
     
    Clifford Kite, Aug 22, 2003
    #11
  12. Gabriel Michael

    /dev/rob0 Guest

    Please demonstrate that at the command line and paste it here. Perhaps
    try it both with and without using an intermediate "rmmod ppp_generic"
    command. (Understand also that those with static, non-modular kernels,
    cannot remove the driver.)
    I appreciate the fact that you qualified that with "it seems". I myself
    have the belief that /sbin/ifconfig on GNU/Linux of any flavour is
    likely to have been based on the same source code, and that its
    behaviour differences where seen are likely to have logical and definite
    causes.

    [Floyd Davidson]
    Mr. Davidson is not always cheerful ... possibly because he's fleeing
    hungry polar bears and losing sleep due to midnight sun[1] ... but he's
    always an excellent resource on Unix matters.
    Not having had the chance to get to know him personally, and lacking
    such extraordinary confidence in my telepathic abilities, I could not
    venture such an observation. But TBH I will say that I have no greater
    confidence in your ability to draw such a conclusion.

    You seem to be a poster who allows his anger to interfere in what
    otherwise could / should be a factual discussion. That, I would venture,
    could well be an issue of egotism ... or of something else. I'm not much
    into psychoanalysis ever since I discovered computers. :)

    My recommendation to Usenet readers WRT Mr. Davidson is the opposite of
    yours. Those who ignore him will miss out on a lot of valuable
    information and experience.


    [1] This is, of course, a joke. It never hurts to make that clear on
    Usenet.
     
    /dev/rob0, Aug 22, 2003
    #12
  13. Gabriel Michael

    Alan Connor Guest

    For the third time, for those who of you who apparently cannot read simple
    English or are foolish enough to believe that you know everything about linux,
    which is impossible.


    If my ppp connection is not up, ie I am not connected to my isp, then this is
    what happens:

    [email protected]:~$ /sbin/ifconfig ppp0
    ppp0: error fetching interface information: Device not found

    [email protected]:~$ echo $?
    1


    If the connection IS up:


    [email protected]:~$ /sbin/ifconfig ppp0
    ppp0 Link encap:point-to-Point Protocol
    inet addr:65.157.134.245 P-t-P:64.167.222.8 Mask:255.255.255.255
    UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1524 Metric:1
    RX packets:5 errors:1 dropped:0 overruns:0 frame:0
    TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:3
    RX bytes:710 (710.0 b) TX bytes:195 (195.0 b)


    [email protected]:~$ echo $?
    0

    I have altered the IP addresses.


    Apparently what is going on is that the ppp0 interface is being configured on
    demand, then un-configured when I am through with it.

    This makes it reliable for the sort of tests the OP was interested in on MY
    box.

    Eth0 and lo on the other hand, are permanently configured and always show
    up with ifconfig -a.


    Now. If anyone calls me a liar or an idiot one more time, their ass
    is killfiled, and good riddance to fools. I will be saved from bad advice in
    the future.


    Alan C
     
    Alan Connor, Aug 22, 2003
    #13
  14. Gabriel Michael

    Lew Pitcher Guest

    Alan Connor wrote:
    [snip]
    With my dial-on-demand ppp setup, /sbin/ifconfig ppp0 will show the ppp0
    details even when /not/ connected to my ISP. In fact, ifconfig shows my
    /disconnected/ ppp0 as having an IP address.
    I will post the same from /my/ system tomorrow (when I can get to it).
    You'll see a difference between what works on /my/ system (Slackware Linux
    9.0 with all current upgrades) and what works (or not) on yours.

    FWIW, I've been running ppp dial-on-demand since Slackware 7.0 and it has
    never behaved like you describe.
    Apparently, that behaviour is on your box. My box does different.
    But not on /my/ box.

    And not in the /general case/.
    Killfile me or not, I don't care about your opinion.

    I /do/ care that questions get answered properly, and system or
    configuration dependancies get identified. Obviously, you've got a system or
    configuration depencancy that behaves in a consistant testable manner for
    /you/. However, the behaviour you describe does not work for everyone. If
    you take someone saying that your procedure won't work in general (or even,
    won't work for them in particular) as being called a liar, then you are
    reading too much into the words entered here.

    --

    Lew Pitcher, IT Consultant, Application Architecture
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
     
    Lew Pitcher, Aug 22, 2003
    #14
  15. Gabriel Michael

    Alan Connor Guest


    welcome to my killfile, fool.


    Alan C
     
    Alan Connor, Aug 22, 2003
    #15
  16. Thanks, this is exactly what I needed. The ppp link is for a VPN, so
    it's not bad etiquette for me to incessantly pinging the other end ;-)
     
    Gabriel Michael, Aug 22, 2003
    #16
  17. Gabriel Michael

    Alan Connor Guest

    Oops! I should have pasted the script rather than relying on memory

    and, obviously, remove the useless "poff" from the bottom.


    And yes, for clarities' sake, I left out the parts that sends the stderr
    and stdout messages to /dev/null


    Alan C
     
    Alan Connor, Aug 22, 2003
    #17
  18. Gabriel Michael

    Alan Connor Guest

    Christ! I just got an email pointing out that I had made yet ANOTHER mistake
    in the script I posted.

    hehe. I am VERY busy at present and quite distracted. My apologies.

    Here's the script, cut and pasted, just like I use it.

    Works every time, and never gotten an error message from slrnpull.



    #!/bin/sh


    pon

    while true ; do

    sleep 1

    /sbin/ifconfig ppp0 &> /dev/null

    k=`echo $?`

    if [ "$k" = "0" ] ; then slrnpull --no-post && poff && exit 0


    else continue

    fi

    done



    Out of this wonderful medium for at LEAST a couple of hours.



    Alan C
     
    Alan Connor, Aug 22, 2003
    #18
  19. Gabriel Michael

    /dev/rob0 Guest

    I appreciate your patience and tolerance, and I apologise if I happened
    to miss something earlier in the thread.
    Indeed. I can explain this now. Shell clippings inside "#v" marks, with
    comments and explanations immediately following each block.

    #v+
    [email protected]:~$ /sbin/ifconfig lo ; echo $?
    lo Link encap:Local Loopback
    inet addr:127.0.0.1 Mask:255.0.0.0
    UP LOOPBACK RUNNING MTU:16436 Metric:1
    RX packets:27566 errors:0 dropped:0 overruns:0 frame:0
    TX packets:27566 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:0
    RX bytes:3511905 (3.3 Mb) TX bytes:3511905 (3.3 Mb)

    0
    #v-
    For reference, an active interface, exit code 0.

    #v+
    [email protected]:~$ /sbin/ifconfig dummy0 ; echo $?
    dummy0: error fetching interface information: Device not found
    1
    [email protected]:~$ sudo /sbin/modprobe dummy
    [email protected]:~$ /sbin/ifconfig dummy0 ; echo $?
    dummy0 Link encap:Ethernet HWaddr 00:00:00:00:00:00
    BROADCAST NOARP MTU:1500 Metric:1
    RX packets:0 errors:0 dropped:0 overruns:0 frame:0
    TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:0
    RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)

    0
    #v-
    dummy0 was inactive before, and the driver (a module in this kernel) was
    not loaded. Without the driver, exit code 1, just as you got with ppp0.
    *With* the driver but *no* IP assigned, exit code 0.

    #v+
    [email protected]:~$ sudo /sbin/ifconfig dummy0 192.168.192.168
    [email protected]:~$ /sbin/ifconfig dummy0 ; echo $?
    dummy0 Link encap:Ethernet HWaddr 00:00:00:00:00:00
    inet addr:192.168.192.168 Bcast:192.168.192.255 Mask:255.255.255.0
    UP BROADCAST RUNNING NOARP MTU:1500 Metric:1
    RX packets:0 errors:0 dropped:0 overruns:0 frame:0
    TX packets:21 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:0
    RX bytes:0 (0.0 b) TX bytes:2455 (2.3 Kb)

    0
    [email protected]:~$ sudo /sbin/ifconfig dummy0 down
    [email protected]:~$ /sbin/ifconfig dummy0 ; echo $?
    dummy0 Link encap:Ethernet HWaddr 00:00:00:00:00:00
    inet addr:192.168.192.168 Bcast:192.168.192.255 Mask:255.255.255.0
    BROADCAST NOARP MTU:1500 Metric:1
    RX packets:0 errors:0 dropped:0 overruns:0 frame:0
    TX packets:22 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:0
    RX bytes:0 (0.0 b) TX bytes:2710 (2.6 Kb)

    0
    #v-
    Assigned an IP to the interface, note now the "UP" flag, Still exit code
    0. Took the interface down, no "UP" flag, and still 0.

    #v+
    [email protected]:~$ sudo /sbin/modprobe -rv dummy
    # delete dummy
    [email protected]:~$ /sbin/ifconfig dummy0 ; echo $?
    dummy0: error fetching interface information: Device not found
    1
    #v-
    ifconfig exits with 0 if the interface exists, 1 if not. The interface
    exists whilst the driver is loaded.
    Yes, this is expected, because the interface can only be up if its
    driver is loaded.
    Save the trouble next time: the real one is in your NNTP-Posting-Host:
    header. :) (And if the IP is dynamic, who cares?)
    Your script or something being run by your distro is unloading the
    ppp_generic module when you disconnect. Mine has a cron.hourly job to
    remove unused modules, but I always disable that on my systems. Yours
    might be running that job (it's "/sbin/rmmod -as" BTW) more frequently,
    or perhaps the ip-down script explicitly removes ppp_generic.

    If you're interested in testing this, try "/sbin/lsmod" before you make
    the connection, after it comes up, and after it's terminated. Check the
    crontab for root and the /etc/cron.hourly directory.

    Also, try an explicit load of ppp_generic, and then check lsmod. Note
    the absence of the "(autoclean)" flag. From now on your script will
    probably fail (unless as noted, ip-down removes the module.) rmmod or
    modprobe will not automatically remove non-autoclean modules.
    Indeed, but it might well NOT be reliable on other machines, nor on
    differently-managed distros, as you mentioned before.
    If either one is a module, you could do the same with it as I just
    showed you with my dummy0 interface.
    I don't recall having called you anything of the sort. I do, however,
    believe that you misunderstood the reason why your script was working. I
    hope this helps to clear the confusion.
     
    /dev/rob0, Aug 23, 2003
    #19
  20. Still, you probably don't need to do it in a continious loop without
    sleeping for several seconds between pings.
     
    Floyd Davidson, Aug 23, 2003
    #20
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.