Test Net Connection without ping

Configuration wizards, scanners, remote desktop, etc.
Post Reply
Message
Author
stemsee

Test Net Connection without ping

#1 Post 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

sheldonisaac
Posts: 902
Joined: Mon 22 Jun 2009, 01:36
Location: Philadelphia, PA

Re: Test Net Connection without ping

#2 Post by sheldonisaac »

stemsee (in part) wrote:Simple script to test connection without resorting to ping.
Thank you, stemsee!

Sheldon
Dell E6410: BusterPup, BionicPup64, Xenial, etc
Intel DQ35JOE, Dell Vostro 430
Dell Inspiron, Acer Aspire One, EeePC 1018P

stemsee

Re: Test Net Connection without ping

#3 Post 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!

stemsee

#4 Post 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

stemsee

#5 Post 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 &

User avatar
spiritwild
Posts: 181
Joined: Mon 03 Oct 2016, 10:06

#6 Post by spiritwild »

Thanks, cut the YAD part and added to my conky

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#7 Post 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.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

stemsee

#8 Post 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

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#9 Post 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.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

stemsee

#10 Post 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

gyro
Posts: 1798
Joined: Tue 28 Oct 2008, 21:35
Location: Brisbane, Australia

#11 Post 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

stemsee

#12 Post by stemsee »

retracted
Last edited by stemsee on Mon 05 Feb 2018, 14:02, edited 1 time in total.

stemsee

#13 Post by stemsee »

Hi @Gyro

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

stemsee

#14 Post by stemsee »

checking ability to post

Post Reply