How can I determine from a script that there is a Network

For example, to see that my mobile network is active I check for the
presence of “/var/run/ppp0.pid”, which works fine. But now I need
checking for wlan0 or eth0, and I don’t know how.


Cheers / Saludos,

Carlos E. R.
(from 11.4, with Evergreen, x86_64 “Celadon” (Minas Tirith))

On 2013-01-19 21:13, Carlos E. R. wrote:
> For example, to see that my mobile network is active I check for the
> presence of “/var/run/ppp0.pid”, which works fine. But now I need
> checking for wlan0 or eth0, and I don’t know how.
>

I found a way:


#!/bin/bash
FOUND=`grep "eth0:\|wlan0" /proc/net/dev`

if   -n "$FOUND" ] ; then
echo yes
else
echo no
fi


Cheers / Saludos,

Carlos E. R.
(from 11.4, with Evergreen, x86_64 “Celadon” (Minas Tirith))

On Sat 19 Jan 2013 08:13:26 PM CST, Carlos E. R. wrote:

For example, to see that my mobile network is active I check for the
presence of “/var/run/ppp0.pid”, which works fine. But now I need
checking for wlan0 or eth0, and I don’t know how.

Hi
ifplugstatus… or ifstatus <interface>


/usr/sbin/ifplugstatus

eth0: unplugged
lo: link beat detected
wlan0: link beat detected

Are you using Traditional or NetworkManager?


Cheers Malcolm °¿° (Linux Counter #276890)
openSUSE 12.2 (x86_64) Kernel 3.4.11-2.16-desktop
up 2 days 23:27, 4 users, load average: 0.06, 0.06, 0.05
CPU Intel® i5 CPU M520@2.40GHz | GPU Intel® Ironlake Mobile

On 2013-01-19 21:51, malcolmlewis wrote:

> Hi
> ifplugstatus… or ifstatus <interface>
>


> /usr/sbin/ifplugstatus
>
> eth0: unplugged
> lo: link beat detected
> wlan0: link beat detected
> 

Are you using Traditional or NetworkManager?

Both. At this moment, NM, so ifstatus doesn’t fulfill the requirements.
ifplugstatus looks promising, but it needs parsing the output. The hack
I found with “/proc/net/dev” is working right now.

I’ll see if I can code it with ifplugstatus


ETH0=`/sbin/ifplugstatus wlan0`

if  "wlan0: link beat detected"="$WLAN0" ] ; then
echo si
else
echo no
fi

Like that :slight_smile:

However…


ETH0=`/sbin/ifplugstatus wlan0`
WLAN0=`/sbin/ifplugstatus eth0`
PPP0=`/sbin/ifplugstatus ppp0`

if  "wlan0: link beat detected"="$WLAN0" -o "eth0: link beat
detected"="ETH0"
echo si
else
echo no
fi

echo $PPP0

produces an error if the interface does not currently exist:


cer@minas-tirith:~/bin> test_network
Failure (No such device)
si
ppp0: not supported (Retry as root?)


Cheers / Saludos,

Carlos E. R.
(from 11.4, with Evergreen, x86_64 “Celadon” (Minas Tirith))

On Sat 19 Jan 2013 09:18:28 PM CST, Carlos E. R. wrote:

On 2013-01-19 21:51, malcolmlewis wrote:

> Hi
> ifplugstatus… or ifstatus <interface>
>

/usr/sbin/ifplugstatus

eth0: unplugged
lo: link beat detected
wlan0: link beat detected

Are you using Traditional or NetworkManager?

Both. At this moment, NM, so ifstatus doesn’t fulfill the requirements.
ifplugstatus looks promising, but it needs parsing the output. The hack
I found with “/proc/net/dev” is working right now.

I’ll see if I can code it with ifplugstatus

ETH0=/sbin/ifplugstatus wlan0

if “wlan0: link beat detected”=“$WLAN0” ] ; then
echo si
else
echo no
fi

Like that :slight_smile:

However…

ETH0=/sbin/ifplugstatus wlan0
WLAN0=/sbin/ifplugstatus eth0
PPP0=/sbin/ifplugstatus ppp0

if “wlan0: link beat detected”=“$WLAN0” -o “eth0: link beat
detected”=“ETH0”
echo si
else
echo no
fi

echo $PPP0

produces an error if the interface does not currently exist:

cer@minas-tirith:~/bin> test_network
Failure (No such device)
si
ppp0: not supported (Retry as root?)

Hi
A one-liner;


/usr/sbin/ifplugstatus -a |grep -E 'ppp0|eth0|wlan0' |grep -e ": link beat detected"


Cheers Malcolm °¿° (Linux Counter #276890)
openSUSE 12.2 (x86_64) Kernel 3.4.11-2.16-desktop
up 3 days 0:35, 4 users, load average: 0.00, 0.02, 0.05
CPU Intel® i5 CPU M520@2.40GHz | GPU Intel® Ironlake Mobile

Take a look at the top 2 lines:


ETH0=`/sbin/ifplugstatus wlan0`
WLAN0=`/sbin/ifplugstatus eth0`

On 2013-01-19 23:16, Knurpht wrote:
>
> Take a look at the top 2 lines:
>
> Code:
> --------------------
>
> ETH0=/sbin/ifplugstatus wlan0
> WLAN0=/sbin/ifplugstatus eth0
>
> --------------------

Yes, I swapped them, I noticed later.


Cheers / Saludos,

Carlos E. R.
(from 11.4, with Evergreen, x86_64 “Celadon” (Minas Tirith))

On 2013-01-19 22:56, malcolmlewis wrote:
> Hi
> A one-liner;
>


> /usr/sbin/ifplugstatus -a |grep -E 'ppp0|eth0|wlan0' |grep -e ": link beat detected"
> 

Not bad… :slight_smile:


Cheers / Saludos,

Carlos E. R.
(from 11.4, with Evergreen, x86_64 “Celadon” (Minas Tirith))

Please define what “there is a network” means. So far it appears you are after “adapter detected link”. This is very far from having working network, but may be that is what you meant.

if nm-online; then
  echo Network is up
fi

On 2013-01-20 08:16, arvidjaar wrote:
>
> Please define what “there is a network” means. So far it appears you are
> after “adapter detected link”. This is very far from having working
> network, but may be that is what you meant.

Yes, that is correct. It is a cronjob that fetchs news (nntp), and I run
it when there should be a network. If the interface is there but there
is no connectivity the fetch will fail with typical errors, but that is
acceptable.

> robin_listas Wrote:
>> At this moment, NM
>
> Code:
> --------------------
> if nm-online; then
> echo Network is up
> fi
> --------------------

Yes, I found that command. But this laptop uses profiles, and sometimes
it is setup to use traditional networking.


Cheers / Saludos,

Carlos E. R.
(from 11.4, with Evergreen, x86_64 “Celadon” (Minas Tirith))

So, if you want to make it independent of the profile selected, the script first would have to find out which profile is active, then invoke the commands needed to check the network status.
Another thing I thought of is about what happens if a static config is active.

On 2013-01-20 14:16, Knurpht wrote:

> So, if you want to make it independent of the profile selected, the
> script first would have to find out which profile is active, then invoke
> the commands needed to check the network status.
> Another thing I thought of is about what happens if a static config is
> active.

Yes, grepping “/proc/net/dev” had that advantage. I hope that ifplugd
runs as well with traditional networking, I will check that in a few days.


Cheers / Saludos,

Carlos E. R.
(from 11.4, with Evergreen, x86_64 “Celadon” (Minas Tirith))

ifup runs ifplugd by default.

On 2013-01-26 18:46, arvidjaar wrote:
>
> robin_listas;2520303 Wrote:
>> I hope that ifplugd
>> runs as well with traditional networking
>
> ifup runs ifplugd by default.

Yes, it is working. I had no problems when switching, so I forgot about it.


Cheers / Saludos,

Carlos E. R.
(from 12.1 x86_64 “Asparagus” at Telcontar)