network auto (re)connect script

Under development: PCMCIA, wireless, etc.
Post Reply
Message
Author
Softwaregurl
Posts: 49
Joined: Sun 23 Jan 2011, 20:53
Location: a state of mind

network auto (re)connect script

#1 Post by Softwaregurl »

this code is part of a larger daemon that handles several things. I wrote it because I got frustrated with manually reconnecting via the gui every time something caused a failure of my wireless connection. It also extends the amount of time spent on the internet before the battery runs out by saving me from taking time to realize I'm disconnected then reconnect manually.

Code: Select all

netcheck() {
	NETERR="Oops"; NETDC="CONN"; touch /tmp/network.nslo
#	if [ -e /tmp/network.off ]; then
	nice -n 2 nslookup www.google.com >> /tmp/network.nslo &
	nice -n 2 nslookup www.yahoo.com >> /tmp/network.nslo &
	nice -n 2 nslookup www.facebook.com >> /tmp/network.nslo &
#	fi
sleep 5
		cat /tmp/network.nslo | grep -ve "0.0.0.0" | grep -E "[0-9]+" && NETERR="muchOK"
		echo -n "$NETERR ------ "
		rm /tmp/network.nslo
	[ -e /tmp/network.disconnect ] && NETDC="NNDC"
	pidof firefox-bin && NETDC="NNff"
	if [ "$NETERR" == "Oops" ] && [ "$NETDC" != "NNDC" ]; then
		L_TITLE_S="Network"; L_MESSAGE_S="XXXXXX Network DisConnected XXXXXXX";  L_BG_S="red"; sleep 1
		gxmessage -center -geometry 250x50 -timeout 2 -bg "$L_BG_S" -buttons "" -title "$L_TITLE_S" "$L_MESSAGE_S"
		pidof rc.network || /etc/rc.d/rc.network stop
	sleep 5
		pidof rc.network || /etc/rc.d/rc.network start | tee /tmp/network.rc; sleep 1
		grep -e Success /tmp/network.rc > /dev/null && NETERR="CONOK"
		rm /tmp/network.rc
		if [ "$NETERR" == "CONOK" ]; then
		L_MESSAGE_S="   ++  Network     Connected  ++";  L_BG_S="green"; sleep 1;
		gxmessage -center -geometry 250x50 -timeout 1 -bg "$L_BG_S" -buttons "" -title "$L_TITLE_S" "$L_MESSAGE_S"
		rm /tmp/network.off
		fi
	fi
	if [ "$NETERR" != "Oops" ] && [ "$NETDC" == "NNDC" ]; then
		L_TITLE_S="Network"; L_MESSAGE_S="XXXXXX DisConnectIng Network XXXXXXX";  L_BG_S="yellow"; sleep 1
		gxmessage -center -geometry 250x50 -timeout 5 -bg "$L_BG_S" -buttons "" -title "$L_TITLE_S" "$L_MESSAGE_S" &
		sleep 1; /etc/rc.d/rc.network stop
	touch /tmp/network.off
	fi
}
The [ -e /tmp/network.off ] was behaving oddly using both that and [ ! -e /tmp/network.off ]. The pidof firefox-bin && NETDC="NNff" saves battery power. I will change that at some point to look at network activity and disconnect when it falls to the level of "noise" (ARP, etc...) for a certain length of time.

Have fun with this and hope it helps someone else.

Edit: I should have mentioned this is on Lucid-Puppy 5.11

User avatar
jemimah
Posts: 4307
Joined: Wed 26 Aug 2009, 19:56
Location: Tampa, FL
Contact:

#2 Post by jemimah »

Hiya and welcome to the forums,

Perhaps you'd like to help me with the Frisbee network manager, which is not yet available for LuPu, but you can test it in Fluppy011.
http://murga-linux.com/puppy/viewtopic.php?t=56156

Frisbee is totally different architecturally from SNS and network wizard and I intend it to be a replacement once it's ready. It won't drop your connections because it uses wpa_supplicant and dhcpcd as background daemons that know when the connection is dropped and reconnect you automatically. (Wpa_supplicant can handle WEP and open connections too, which make most of the complexity of managing connections magically disappear,) In addition, wpa_supplicant can automatically handle roaming if one network disappears and another becomes available - or in the case that you suspended in one location and resumed in another.

Frisbee is intended to be userfriendly and behaves much more like the network managers found in other operating systems.

Softwaregurl
Posts: 49
Joined: Sun 23 Jan 2011, 20:53
Location: a state of mind

#3 Post by Softwaregurl »

Thanks for the welcome jemimah.

I took a peek at the thread this morning. I'll download a copy and check it out when I have a chance.

tommy
Posts: 133
Joined: Tue 04 Oct 2005, 20:21
Location: Italy

#4 Post by tommy »

Let me say AN ENORMOUS THANK YOU to softwaregurl who provided this wonderful piece of bash script.

MY PROBLEM WAS:
I have a cheap dikom wifi dongle using zd1211rw module. It is very uneasy and it drops wifi connection every now and then (it can work rock solid for 1 hour to 9 hours, then it loses the access point association). This is very frustrating, especially when using Transmission bittorrent, as I leave it to run overnight or when I'm not at home.

MY SOLUTION:
I slightly modified softwaregurl's script to suit my network and my needs:

Code: Select all

#!/bin/sh
while true
do

   NETERR="Oops"; NETDC="CONN"; touch /tmp/network.nslo
#   if [ -e /tmp/network.off ]; then
   ping -c 1 192.168.2.1 >> /tmp/network.nslo &
#   fi
sleep 40
      cat /tmp/network.nslo | grep -c "100% packet loss" | grep -E "[0]+" && NETERR="muchOK"
      cat /tmp/network.nslo | wc -c < /tmp/network.nslo | grep -c "46" | grep -E "[1]+" && NETERR="Oops"
      echo -n "$NETERR ------ "
      rm /tmp/network.nslo
 #  [ -e /tmp/network.disconnect ] && NETDC="NNDC"
 #  pidof seamonkey-bin && NETDC="NNff"
 #  if [ "$NETERR" == "Oops" ] && [ "$NETDC" != "NNDC" ]; then
    if [ "$NETERR" == "Oops" ]; then
	  L_TITLE_S="Network"; L_MESSAGE_S="XXXXXX Network DisConnected XXXXXXX";  L_BG_S="red"; sleep 1
      gxmessage -center -geometry 250x50 -timeout 2 -bg "$L_BG_S" -buttons "" -title "$L_TITLE_S" "$L_MESSAGE_S"
      pidof rc.network || /etc/rc.d/rc.network stop
   sleep 5
      pidof rc.network || /etc/rc.d/rc.network start | tee /tmp/network.rc; sleep 5
      grep -e Success /tmp/network.rc > /dev/null && NETERR="CONOK"
      rm /tmp/network.rc
      if [ "$NETERR" == "CONOK" ]; then
      L_MESSAGE_S="   ++  Network     Connected  ++";  L_BG_S="green"; sleep 1;
      gxmessage -center -geometry 250x50 -timeout 1 -bg "$L_BG_S" -buttons "" -title "$L_TITLE_S" "$L_MESSAGE_S"
      rm /tmp/network.off
      fi
   fi
 #  if [ "$NETERR" != "Oops" ] && [ "$NETDC" == "NNDC" ]; then
 #     L_TITLE_S="Network"; L_MESSAGE_S="XXXXXX DisConnectIng Network XXXXXXX";  L_BG_S="yellow"; sleep 1
 #     gxmessage -center -geometry 250x50 -timeout 5 -bg "$L_BG_S" -buttons "" -title "$L_TITLE_S" "$L_MESSAGE_S" &
 #     sleep 1; /etc/rc.d/rc.network stop
 #  touch /tmp/network.off
 #  fi

done

Note:
-I am not a bash programmer so I know it can be improved/ it sucks
- note that I ping my router (192.168.2.1) , not the web (because sometimes my router drops ADSL connection so it isn't wifi fault).
- I had to add this line

Code: Select all

cat /tmp/network.nslo | wc -c < /tmp/network.nslo | grep -c "46" | grep -E "[1]+" && NETERR="Oops"
because sometimes my usb dongle goes down and / or network becomes unreachable, so the file /tmp/network.nslo becomes a 46-bytes-long file like this:

PING 192.168.2.1 (192.168.2.1): 56 data bytes

and lacks the "100% packets loss" part.
-I commented out various things in softwaregurl's script such as the 'stay disconnected' lines at the end of his script. I always need to be connected!!
- ping interval is 40 seconds (sleep 40), I think it's ok to not clug my network with many packets. Change at your needs.
- this script output can be read in /tmp/xerrs.log
- I use it on Puppy Turbopup Extreme ( 4.1.2 puplet) so it should work on every puppy I know of. I have put it on /root/Startup foder so it runs at every reboot automatically.

@ jemimah: I don't have a modern Puppy, so I can't use frisbee. Also I DON'T WANT DHCP. Can frisbee connect with fixed IPs?

Any thoughts welcome.

Happy new Year!![/code]

Post Reply