Tiny Core Linux
Tiny Core Extensions => TCE Tips & Tricks => Topic started by: coreplayer2 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
-
I'd check `cat /sys/class/net/eth0/carrier` instead, isn't eth0 still brought up even without a cable connected?
-
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..
-
Hi coreplayer2
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.
-
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
-
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.
-
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.
-
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..
-
Perhaps you mean flnotify.tcz? 4kb with only fltk as a dep.
-
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.
-
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
#!/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
-
Hi coreplayer2
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.
-
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