Page 1 of 1

Test Net Connection without ping

Posted: Tue 23 Jan 2018, 13:48
by stemsee
Simple script to test connection without resorting to ping.

Code: Select all

#!/bin/sh
# Test for network conection without ping
# adapted from https://bbs.archlinux.org/viewtopic.php?id=55485
#
for interface in $(ls /sys/class/net/ | grep -v lo);
do
	if [[ $(cat /sys/class/net/$interface/carrier) = 1 ]]; then 
		OnLine="Online"
	elif [[  $(cat /sys/class/net/$interface/carrier) = 0 ]]; then
		OnLine="Not Online"
	fi
done
export OnLine
echo "$OnLine"
yad --text="$OnLine" --timeout=1

Re: Test Net Connection without ping

Posted: Tue 23 Jan 2018, 14:23
by sheldonisaac
stemsee (in part) wrote:Simple script to test connection without resorting to ping.
Thank you, stemsee!

Sheldon

Re: Test Net Connection without ping

Posted: Tue 23 Jan 2018, 15:44
by stemsee
sheldonisaac wrote:
stemsee (in part) wrote:Simple script to test connection without resorting to ping.
Thank you, stemsee!

Sheldon
Sheldon, You're welcome!

Posted: Wed 24 Jan 2018, 08:29
by stemsee
Improvement

Code: Select all

#!/bin/sh
# Test for network conection without ping
# adapted from https://bbs.archlinux.org/viewtopic.php?id=55485
#
j=0
for interface in $(ls /sys/class/net/ | grep -v lo);
do
	if [[ $(cat /sys/class/net/$interface/carrier) = 1 ]]; then 
		OnLine="$interface is Online"
	elif [[  $(cat /sys/class/net/$interface/carrier) = 0 ]]; then
		OnLine="$interface is Not Online"
	fi
echo "$OnLine"
yad --text="$OnLine" --timeout=1 &
done

Posted: Sat 03 Feb 2018, 16:34
by stemsee
So again updateing as really this only shows a connection to a network but not necessarily to the wan/internet

Code: Select all

for inter in $(ls /sys/class/net/ | grep -v -e 'lo'); 
do 
gotip=`ifconfig $inter | grep 'inet addr' | awk '{print $2}' | cut -f2 -d':'`
   if [[ $(cat /sys/class/net/$inter/carrier) = 1 ]]; then
		if [[ ! -z "$gotip" ]]; then
			OnLine="Connected with $gotip"
		else
			OnLine="$inter is Not Connected"
		fi
   elif [[  $(cat /sys/class/net/$inter/carrier) = 0 ]]; then
		if [[ -z "$gotip" ]]; then
			OnLine="$inter is Not Connected"
		else
			OnLine="Connected with $gotip"
		fi
   fi 
echo "$OnLine" 
yad --window-icon=/usr/share/pixmaps/wifi.png --title="$inter" \
--text="$OnLine" --no-buttons --skip-taskbar --timeout=2 &
done
curl ifconfig.me > /tmp/exip
yad --window-icon=/usr/share/pixmaps/wifi.png --title="$inter" \
--text="External ip `cat /tmp/exip` on internet" --no-buttons --skip-taskbar --timeout=2 &

Posted: Sat 03 Feb 2018, 20:52
by spiritwild
Thanks, cut the YAD part and added to my conky

Posted: Sat 03 Feb 2018, 22:26
by musher0
Hi stemsee.

Great find! Great idea! We needed something like this.

May I suggest that you cut to the bone:

Code: Select all

[ "`cat /sys/class/net/eth0/carrier`" -eq "1" ] && Status="On line" || Status="Off line"
echo "$Status"
Some convolutions you do not need, e.g. "export". And I'm assuming that "carrier"
has only two data positions: 0 or 1.

After that it's ok to use yad, bcm, or whatever, or even stay in the console ;) (yippee!)
to show the status.

BFN.

Posted: Sat 03 Feb 2018, 23:52
by stemsee
Hi musher0

When the network interface cuts off uncleanly, be it eth0 or wlan0, (in my case eth0 wlan1 wlan4 usb0) carrier may still be set as '1' (yes '0' or '1') so the script would say wlan0 is online, when it is not. That is why I added the gotip check ... two tests to be certain. The curl ifconfig.me gets the external ip address thus proving evidence of connection also to the wide area network, not only the local area network.
the format is due to this being a function in my TrayNet app.

adjust your code to include these options and I will accept it.

cheers
stemsee

Posted: Sun 04 Feb 2018, 00:59
by musher0
Hi stemsee.

Euh... it's your code, you do as you wish. It was just a suggestion.

BTW, anything wrong with using

Code: Select all

lsof -i | grep dhcpcd
Would it be reliable, you think?

BFN.

Posted: Sun 04 Feb 2018, 21:17
by stemsee
Hi musher0

My network scripts connect with udhcpc and/or dhcpcd. There is a return on dhcpcd 'IPv4'; but nothing for udhcpc!

stemsee

Posted: Sun 04 Feb 2018, 22:36
by gyro
I don't get it:

Code: Select all

curl ifconfig.me
is a connection to a web server.
Why is this better than a ping?
gyro

Posted: Mon 05 Feb 2018, 12:57
by stemsee
retracted

Posted: Mon 05 Feb 2018, 14:02
by stemsee
Hi @Gyro

No difference I guess. And 'curl ifconfig.me' is really slow!

Posted: Wed 21 Feb 2018, 00:21
by stemsee
checking ability to post