Networking Forums

Networking Forums > Computer Networking > Linux Networking > Bash script to see if PPP link is up...

Reply
Thread Tools Display Modes

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

 
 
Gabriel Michael
Guest
Posts: n/a

 
      08-20-2003, 01:51 AM
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
 
Reply With Quote
 
 
 
 
Bit Twister
Guest
Posts: n/a

 
      08-20-2003, 02:16 AM
On 19 Aug 2003 18:51:35 -0700, Gabriel Michael wrote:
> 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...


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?

 
Reply With Quote
 
Floyd Davidson
Guest
Posts: n/a

 
      08-20-2003, 02:17 AM
(E-Mail Removed) (Gabriel Michael) wrote:
>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


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 L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) (E-Mail Removed)
 
Reply With Quote
 
Alan Connor
Guest
Posts: n/a

 
      08-20-2003, 03:21 AM
On 19 Aug 2003 18:51:35 -0700, Gabriel Michael <(E-Mail Removed)> wrote:
>
>
> 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



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


 
Reply With Quote
 
David Efflandt
Guest
Posts: n/a

 
      08-21-2003, 12:04 AM
On 19 Aug 2003 18:51:35 -0700, Gabriel Michael <(E-Mail Removed)> wrote:
> 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...


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 - All spam ignored http://www.de-srv.com/
http://www.autox.chicago.il.us/ http://www.berniesfloral.net/
http://cgi-help.virtualave.net/ http://hammer.prohosting.com/~cgi-wiz/
 
Reply With Quote
 
JRH
Guest
Posts: n/a

 
      08-21-2003, 08:54 PM
Alan Connor wrote:

> On 19 Aug 2003 18:51:35 -0700, Gabriel Michael <(E-Mail Removed)>
> wrote:

[..]
>> I'm looking for a way to have a bash shell script check to see if a
>> particular ppp link is up (ppp0)

[..]
>> Gabe

[..]
> There are a lot of other ways, of course.
> Alan C


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
--
---------------------------------------------------
(Remove digits from my email address before use ;-)

 
Reply With Quote
 
Alan Connor
Guest
Posts: n/a

 
      08-22-2003, 01:21 AM
On 21 Aug 2003 15:39:54 -0800, Floyd Davidson <(E-Mail Removed)> wrote:
>
>
> Alan Connor <(E-Mail Removed)> wrote:
>>
>>But that script is grossly over-complex for his purposes . All you need to do
>>is run:
>>
>>
>>/sbin/ifconfig ppp*
>>
>>and look at the exit code to see if it is up:
>>
>>echo $?
>>
>>0 for yes and 1 for no.

>
> Wrong.
>
> It returns 1 if the interface has never been up. It returns 0
> for an interface that is up, or that has been configured and is
> currently down.
>


Wrong. I tested it before I posted.

You should try it sometime.


Alan C




 
Reply With Quote
 
David Efflandt
Guest
Posts: n/a

 
      08-22-2003, 03:53 AM
On Thu, 21 Aug 2003 21:28:57 GMT, Alan Connor <(E-Mail Removed)> wrote:
> On Thu, 21 Aug 2003 21:54:51 +0100, JRH <(E-Mail Removed)> wrote:
>>
>>
>> Alan Connor wrote:
>>
>>> On 19 Aug 2003 18:51:35 -0700, Gabriel Michael <(E-Mail Removed)>
>>> wrote:

>> [..]
>>>> I'm looking for a way to have a bash shell script check to see if a
>>>> particular ppp link is up (ppp0)

>> [..]
>>>> Gabe

>> [..]
>>> There are a lot of other ways, of course.
>>> Alan C

>>
>> 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

>
>
> Yeh. Read from ifconfig instead of /var/log/syslog. Good idea.
>
> But that script is grossly over-complex for his purposes . All you need to do
> is run:
>
>
> /sbin/ifconfig ppp*
>
> and look at the exit code to see if it is up:
>
> echo $?
>
> 0 for yes and 1 for no.


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 - All spam ignored http://www.de-srv.com/
http://www.autox.chicago.il.us/ http://www.berniesfloral.net/
http://cgi-help.virtualave.net/ http://hammer.prohosting.com/~cgi-wiz/
 
Reply With Quote
 
Alan Connor
Guest
Posts: n/a

 
      08-22-2003, 04:20 AM
On Fri, 22 Aug 2003 03:53:41 +0000 (UTC), David Efflandt <(E-Mail Removed)> wrote:
>
>
> On Thu, 21 Aug 2003 21:28:57 GMT, Alan Connor <(E-Mail Removed)> wrote:
>> On Thu, 21 Aug 2003 21:54:51 +0100, JRH <(E-Mail Removed)> wrote:

>
> 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).
>


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


 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a

 
      08-22-2003, 12:22 PM
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)

 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy files using filenames from text files with shell script or bash script altariamx2003@gmail.com Linux Networking 4 11-23-2006 08:27 AM
Linux bash syntax Q: Coenraad Loubser Linux Networking 4 01-16-2005 05:31 PM
zmienna bash Zenon Linux Networking 1 05-07-2004 11:21 AM
Setting $TERM when using rsh and ssh with bash waterbottleboodle Linux Networking 1 11-08-2003 10:29 PM
Bash script via inetd = no joy tylernt Linux Networking 10 09-05-2003 08:53 PM



1 2 3 4 5 6 7 8 9 10 11