Shell script should switch if connection established but doesn't
Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Shell script should switch if connection established but doesn't
I have created the following script
Code:
#!/bin/sh
interface=`ifconfig | grep -iE "(wlan0|gprs0)" | awk '{print $1}'`
echo "$(date) - $interface $ICD_CONNECTION_TYPE" >> /home/user/apps/dyndns.log
sleep 10
echo "$(date) - $interface $ICD_CONNECTION_TYPE" >> /home/user/apps/dyndns.log
cd /home/user/apps/
case "$interface" in
gprs0)
while [ -z "$(ifconfig gprs0 | grep inet | awk '{print substr($2,6,length($2))}')" ]
do
echo "$(date) - No IP yet for gprs0" >> /home/user/apps/dyndns.log
sleep 2
done
echo "$(date) - gprs0 up" >> /home/user/apps/dyndns.log
;;
wlan0)
while [ -z "$(ifconfig wlan0 | grep inet | awk '{print substr($2,6,length($2))}')" ]
do
echo "$(date) - No IP yet for wlan0" >> /home/user/apps/dyndns.log
sleep 2
done
echo "$(date) - wlan0 up" >> /home/user/apps/dyndns.log
;;
*)
echo "$(date) - No interface found!" >> /home/user/apps/dyndns.log
;;
esac
echo " " >> /home/user/apps/dyndns.log
exit 0
Most of the times it works as it should (run a script if the connection changes from wlan0 to gprs0) but not always:
Today I found another output which makes no sense:
Fri Jan 8 16:38:30 GMT-11 2010 - gprs0
wlan0 WLAN_INFRA
Fri Jan 8 16:38:50 GMT-11 2010 - gprs0
wlan0 WLAN_INFRA
Fri Jan 8 16:38:50 GMT-11 2010 - No interface found!
I wonder why the script doesn't work because wlan0 is correct and should be identified as it is.
And also why do I have the output lines without a date (wlan0 WLAN_INFRA)? I don't have them added to the script and the "Fri Jan 8 16:38:50 GMT-11 2010 - gprs0" line is wrong because the connection changed to WLAN at this time.
The script runs on a Nokia N900 with Busybox ("small Debian").
Thanks for help.
Do you know a better way to get the status of wlan0? I want to use this script to toggle my online status of a VoIP account because I only want to be online with VoIP when I am on a WiFi connection...
I don't know exactly for Nokia, but if you are connected to AP somewhere has to be written its AP ESSID and when you are disconnected instead of MAC AP there is "Not Associated" (in linux). So if you know Nokia OS well you can search for that sentence.
"wlan0 WLAN_INFRA" is the output of "echo $interface $ICD_CONNECTION_TYPE" ($ICD_CONNECTION_TYPE is a global variable) and the strange thing is that I don't have a line like that in the script and at the same time the output of the line "echo $(date) - $interface $ICD_CONNECTION_TYPE" is "Fri Jan 8 16:38:30 GMT-11 2010 - gprs0 " which means that "$interface" is equal to "gprs0" and "$ICD_CONNECTION_TYPE" is empty. Do you know what the problem could be?
Thanks but I don't want wo have a daemon running all the time which waits for a special DBUS signal. I want to use a script in /etc/network/if-up.d/ because this will be executed every time a connection changes. As I mentioned before the "WLAN_INFRA" output is not reliable and because of that I have created my own variable "interface=`ifconfig | grep -iE "(wlan0|gprs0)" | awk '{print $1}'`" which is reliable but the rest of the script in my first post reacts strange sometimes if you look at my example and I would like to know how to solve this.
So, "ifconfig" by it self without additional arguments gives information ONLY about active interfaces.
If you will just parse "ifconfig" output, you will get exactly what you want, it will be only wlan1|gprs in it output.
But something in the script is not reliable because sometimes I can see the following in the logs:
Quote:
Fri Jan 8 16:38:30 GMT-11 2010 - gprs0
wlan0 WLAN_INFRA
Fri Jan 8 16:38:50 GMT-11 2010 - gprs0
wlan0 WLAN_INFRA
Fri Jan 8 16:38:50 GMT-11 2010 - No interface found!
This is strange because first it detects grps0 and in the next line (which I cannot find in my script!) wlan0.
Do you know what I mean and how I can solve it?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.