WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Starting wifi when no Ethernet exists by script.  (Read 9341 times)

Offline coreplayer2

  • Hero Member
  • *****
  • Posts: 3020
Starting wifi when no Ethernet exists by script.
« on: May 09, 2012, 09:54:04 PM »
So we fire up the notebook at home, work or hotel and have to go through a couple of steps to get connected.  Having gone through this several times a day, I can tell you it gets old fast.   I figured how about we make this task more automated, like make a script to check for an Ethernet connection (because sometimes there is a loose cable on the desk) then quit if true, or start the wifi.sh script if false.   

This script I place in my home/tc/scripts dir and start it from X.d with a small delay.   Now when my notebook starts I receive a selection of Access Points to choose from or use it as a reminder that I forgot to connect an Ethernet cable.  doh!

#!/bin/sh
#Checks for LAN connectivity then start wifi if needed

eth_f (){
if [ -d /sys/class/net/eth0 ]; then
ETH=`cat /sys/class/net/eth0/carrier`
fi
}
eth_f;
sleep 1
if [ -z "$ETH" ] || [ "$ETH" != 1 ]; then
        if [ -d /sys/class/net/wlan0 ]; then
      cliorx sudo wifi.sh
      exit 1
   fi
        exit 2
fi
exit 0



Ideally, I'd like to start the script outside of X  but am still perfecting that...  :|


ps. scrapped this script in favor of the one in last post
« Last Edit: May 18, 2012, 05:49:34 AM by coreplayer2 »

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11044
Re: Starting wifi when no Ethernet exists by script.
« Reply #1 on: May 10, 2012, 04:02:27 AM »
I'd check `cat /sys/class/net/eth0/carrier` instead, isn't eth0 still brought up even without a cable connected?
The only barriers that can stop you are the ones you create yourself.

Offline coreplayer2

  • Hero Member
  • *****
  • Posts: 3020
Re: Starting wifi when no Ethernet exists by script.
« Reply #2 on: May 10, 2012, 12:11:28 PM »
Cool thanks, that's exactly what I was looking for.  ;D  (Had previously tried ping without success.. lol)

Ethernet adapter without LAN connectivity, carrier contents = 0
Ethernet adapter with LAN connectivity, carrier contents = 1


This is perfect,  have modded script
 Thanks again

ps but wait this assumes an Ethernet adapter installed..
« Last Edit: May 10, 2012, 12:34:12 PM by coreplayer2 »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11594
Re: Starting wifi when no Ethernet exists by script.
« Reply #3 on: May 10, 2012, 12:49:29 PM »
Hi coreplayer2
Quote
but wait this assumes an Ethernet adapter installed..
You could set   ETH=0   and then test if   /sys/class/net/eth0/carrier   exists. If it does, you read it,
if it doesn't you don't.

Offline coreplayer2

  • Hero Member
  • *****
  • Posts: 3020
Re: Starting wifi when no Ethernet exists by script.
« Reply #4 on: May 10, 2012, 06:42:04 PM »
thanks guys,  I followed your suggestions and have come up with a new plan,  the short version above replaces the original and then this version with a notification if no wifi or eth exists,
just in case I wanted feedback..

#!/bin/sh
#Check for LAN connectivity then starts wifi if needed

eth_f (){
if [ -d /sys/class/net/eth0 ]; then
ETH=`cat /sys/class/net/eth0/carrier`
fi
}
wlan_f (){
if [ -d /sys/class/net/wlan0 ]; then
WLAN=`cat /sys/class/net/wlan0/carrier`
fi
}

eth_f;
wlan_f;
sleep 1
if [ -z "$ETH" ] || [ "$ETH" != 1 ]; then
   if [ -n "$WLAN" ] && [ "$WLAN" = 0 ]; then
      cliorx sudo wifi.sh
      exit 1
   fi
   popup "No Ethernet or Wireless connections available"
   exit 2
fi
exit 0


Speaking of feedback, I think someone mentioned notify or popup or something..?

ps. also scrapped this variation in favor of the script in last post
« Last Edit: May 18, 2012, 05:53:26 AM by coreplayer2 »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11594
Re: Starting wifi when no Ethernet exists by script.
« Reply #5 on: May 10, 2012, 07:11:26 PM »
Hi coreplayer2
popup TEXT  will put up a dialog box containing TEXT with an OK button.
popask TEXT does the same except it contains Yes and No buttons.
Clicking Yes returns 1 and No returns 0.

Offline coreplayer2

  • Hero Member
  • *****
  • Posts: 3020
Re: Starting wifi when no Ethernet exists by script.
« Reply #6 on: May 10, 2012, 07:33:46 PM »
ok thanks,   meanwhile I've been reading fltk basics here..   though was looking for a simple box and given your description buttons may not be quite the result I had in mind.  I think there was a notify, but the only notify I can find is heavy, though notify_osd is quite good looking  :(  I'll keep searching..

the short script now in the first post works in all scenario's started from .X.d so am happy about that.    Now If I could start the script asynchronously when mc boots and in text mode, though somehow I don';t think that's possible.
« Last Edit: May 10, 2012, 07:48:13 PM by coreplayer2 »

Offline coreplayer2

  • Hero Member
  • *****
  • Posts: 3020
Re: Starting wifi when no Ethernet exists by script.
« Reply #7 on: May 11, 2012, 02:19:57 AM »
You guessed it I'm sure,  didn't like the verbose script, so have reverted to a single popup notification only when there are no connections available.

but I hit a temporary snag..  two actually, first is am running out of coffee, and the second is the wifi.sh script runs if wlan0 is detected even if the beacon is off, where as it shouldn't..   

first the coffee..
« Last Edit: May 11, 2012, 02:24:12 AM by coreplayer2 »

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11044
Re: Starting wifi when no Ethernet exists by script.
« Reply #8 on: May 11, 2012, 05:45:52 AM »
Perhaps you mean flnotify.tcz? 4kb with only fltk as a dep.
The only barriers that can stop you are the ones you create yourself.

Offline coreplayer2

  • Hero Member
  • *****
  • Posts: 3020
Re: Starting wifi when no Ethernet exists by script.
« Reply #9 on: May 11, 2012, 06:14:45 AM »
That's the one thanks, been looking all day for that, am going to add flnotify.tcz right now :)

btw  modified code in post #4 is now working as desired at last.   Am on the connected lifestyle now
Thanks for your help guys.
« Last Edit: May 11, 2012, 06:20:15 AM by coreplayer2 »

Offline coreplayer2

  • Hero Member
  • *****
  • Posts: 3020
Re: Starting wifi when no Ethernet exists by script.
« Reply #10 on: May 18, 2012, 05:35:04 AM »
Had to go back to the drawing board with this one after realizing there are at least ten possible variations to test for despite only wlan0 and eth0 being in the equation.  So I built a script to test all options in a sandbox, and some options failed :(   
I scrapped the previous scripts and went with something more robust, which definitely holds up against any test I through at it.

flnotify is a nice touch when all attempts to start a connection fail, thanks curaga

Code: [Select]
#!/bin/sh
#Check for LAN connectivity then starts WIFI if needed

#Get network carrier file value
carrier_f (){
CARRIER=`cat /sys/class/net/$DEV/carrier`
for i in $CARRIER ; do
case $i in
null) CARRIER=`""` ;;
0) CARRIER=`expr $CARRIER + 7` ;;
1) CARRIER=`expr $CARRIER + 4` ;;
*) printf "Invalid connection, please try again \n"
esac
done
}

find /tmp -name "netcon.log" -print0 | xargs -0 rm -f;
###########Ethernet###########
DEV=eth0
#Test for valid Ethernet otherwise start wifi
if [ -d /sys/class/net/eth0 ]; then
carrier_f;
sleep 0.25
if [ $CARRIER -eq 5 ] 2>/dev/null; then
printf "$DEV $CARRIER \n" >> /tmp/netcon.log
printf "Ethernet connected already \n" >> /tmp/netcon.log
exit 0
elif [ $CARRIER -eq 7 ] 2>/dev/null; then
printf "$DEV $CARRIER \n" >> /tmp/netcon.log
printf "Ethernet available but not connected \n" >> /tmp/netcon.log
fi
else
printf "No Ethernet device discovered \n" >> /tmp/netcon.log
fi

###########Wireless###########
#No suitable Ethernet found, starting Wireless check
NUM=0
DEV=wlan0
if [ -d /sys/class/net/wlan0 ]; then
carrier_f 2>/dev/null;
sleep 0.25

if [ "$CARRIER" -eq "5" ]; then
printf "$DEV $CARRIER \n" >> /tmp/netcon.log
printf "Wifi connected already \n" >> /tmp/netcon.log
exit 1

elif [ "$CARRIER" -eq "7" ]; then
cliorx sudo wifi.sh
printf "$DEV $CARRIER \n" >> /tmp/netcon.log
printf "starting Wifi.sh \n" >> /tmp/netcon.log
exit 2

elif [ -z "$CARRIER" ]; then
NUM=5
while [ -z "$CARRIER" ] && [ $NUM -gt 0 ]; do
printf "$DEV $CARRIER \n" >> /tmp/netcon.log
printf "sending ifconfig UP \n" >> /tmp/netcon.log
clear
sudo ifconfig wlan0 up;
carrier_f;
let NUM-=1
sleep 0.25
done
if [ -n "$CARRIER" ]; then
cliorx sudo wifi.sh
printf "starting Wifi.sh \n" >> /tmp/netcon.log
exit 3
fi
fi
else
printf "No Wireless device discovered \n" >> /tmp/netcon.log
fi
printf "No Ethernet or Wireless connections available \n" >> /tmp/netcon.log
flnotify "No Ethernet or Wireless connections available"
exit 4
« Last Edit: May 18, 2012, 07:42:02 PM by coreplayer2 »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11594
Re: Starting wifi when no Ethernet exists by script.
« Reply #11 on: May 18, 2012, 07:47:32 AM »
Hi coreplayer2
Quote
null) CARRIER=`" "` ;;
If you are trying to set  CARRIER  to be an empty or zero length string, you need to remove the
space between the two quotes.

Offline coreplayer2

  • Hero Member
  • *****
  • Posts: 3020
Re: Starting wifi when no Ethernet exists by script.
« Reply #12 on: May 18, 2012, 05:58:29 PM »
Thanks for the feedback Rich.  will fix that.   
Actually had been attempting to set an integer, but have been unsuccessful at reliably achieving that.   so had removed the number and left a white space by default, But yes it is supposed to be detected by -z    having run the script many thousands of times in it's test environment without any negative effect I assumed it not to be detrimental. 

Everyday I learn something new, thanks