LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 01-18-2010, 05:51 PM   #1
dEM0nsTAr
LQ Newbie
 
Registered: Nov 2009
Posts: 28

Rep: Reputation: 15
Question 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.
 
Old 01-18-2010, 06:14 PM   #2
nimnull22
Senior Member
 
Registered: Jul 2009
Distribution: OpenSuse 11.1, Fedora 14, Ubuntu 12.04/12.10, FreeBSD 9.0
Posts: 1,571

Rep: Reputation: 92
I think, may be some IP was resolved like WLAN_INFRA, and instead to write IP address like 192.xxx.xxx.xxx it saves WLAN_INFRA.
 
Old 01-18-2010, 06:24 PM   #3
dEM0nsTAr
LQ Newbie
 
Registered: Nov 2009
Posts: 28

Original Poster
Rep: Reputation: 15
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...
 
Old 01-18-2010, 06:35 PM   #4
nimnull22
Senior Member
 
Registered: Jul 2009
Distribution: OpenSuse 11.1, Fedora 14, Ubuntu 12.04/12.10, FreeBSD 9.0
Posts: 1,571

Rep: Reputation: 92
Do you have command: iwconfig wlan0?
 
Old 01-18-2010, 06:39 PM   #5
dEM0nsTAr
LQ Newbie
 
Registered: Nov 2009
Posts: 28

Original Poster
Rep: Reputation: 15
No, only ifconfig wlan0
 
Old 01-18-2010, 06:51 PM   #6
nimnull22
Senior Member
 
Registered: Jul 2009
Distribution: OpenSuse 11.1, Fedora 14, Ubuntu 12.04/12.10, FreeBSD 9.0
Posts: 1,571

Rep: Reputation: 92
Quote:
Originally Posted by dEM0nsTAr View Post
Do you know a better way to get the status of wlan0?
What kind of status do you need?
 
Old 01-18-2010, 06:55 PM   #7
dEM0nsTAr
LQ Newbie
 
Registered: Nov 2009
Posts: 28

Original Poster
Rep: Reputation: 15
A reliable way to determine if wlan0 is active (and maybe has an internet connection).
 
Old 01-18-2010, 07:19 PM   #8
nimnull22
Senior Member
 
Registered: Jul 2009
Distribution: OpenSuse 11.1, Fedora 14, Ubuntu 12.04/12.10, FreeBSD 9.0
Posts: 1,571

Rep: Reputation: 92
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.

I hope it helps.
 
Old 01-18-2010, 08:17 PM   #9
dEM0nsTAr
LQ Newbie
 
Registered: Nov 2009
Posts: 28

Original Poster
Rep: Reputation: 15
Thanks I will try that.

You were wrong in your first answer:

"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?
 
Old 01-18-2010, 08:31 PM   #10
nimnull22
Senior Member
 
Registered: Jul 2009
Distribution: OpenSuse 11.1, Fedora 14, Ubuntu 12.04/12.10, FreeBSD 9.0
Posts: 1,571

Rep: Reputation: 92
Have a look:
http://stefans.datenbruch.de/nokia770/
 
Old 01-18-2010, 08:43 PM   #11
dEM0nsTAr
LQ Newbie
 
Registered: Nov 2009
Posts: 28

Original Poster
Rep: Reputation: 15
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.
 
Old 01-18-2010, 08:53 PM   #12
nimnull22
Senior Member
 
Registered: Jul 2009
Distribution: OpenSuse 11.1, Fedora 14, Ubuntu 12.04/12.10, FreeBSD 9.0
Posts: 1,571

Rep: Reputation: 92
Tell,please, what is the full output you get for "ifconfig", when you are connected through wifi and gprs.

Connect with wifi and execute "ifconfig" (just ifconfig without anything)
Connect with gprs and do the same.

Post output here, if it is not a secret.
 
Old 01-19-2010, 04:32 AM   #13
dEM0nsTAr
LQ Newbie
 
Registered: Nov 2009
Posts: 28

Original Poster
Rep: Reputation: 15
GPRS active:

Quote:
gprs0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:10.247.71.71 P-t-P:10.247.71.71 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MTU:1400 Metric:1
RX packets:1737 errors:0 dropped:0 overruns:0 frame:0
TX packets:2043 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:10
RX bytes:322662 (315.0 KiB) TX bytes:259376 (253.2 KiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:993 errors:0 dropped:0 overruns:0 frame:0
TX packets:993 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:55503 (54.2 KiB) TX bytes:55503 (54.2 KiB)

phonet0 Link encap:UNSPEC HWaddr 15-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
UP POINTOPOINT RUNNING NOARP MTU:4000 Metric:1
RX packets:20339 errors:0 dropped:0 overruns:0 frame:0
TX packets:10046 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:1021316 (997.3 KiB) TX bytes:565527 (552.2 KiB)
WLAN:

Quote:
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:1059 errors:0 dropped:0 overruns:0 frame:0
TX packets:1059 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:58299 (56.9 KiB) TX bytes:58299 (56.9 KiB)

phonet0 Link encap:UNSPEC HWaddr 15-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
UP POINTOPOINT RUNNING NOARP MTU:4000 Metric:1
RX packets:20417 errors:0 dropped:0 overruns:0 frame:0
TX packets:10078 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:1022948 (998.9 KiB) TX bytes:566361 (553.0 KiB)

wlan0 Link encap:Ethernet HWaddr 00:BD:3A:88:3C:52
inet addr:10.0.0.8 Bcast:10.0.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:80148 errors:0 dropped:0 overruns:0 frame:0
TX packets:43259 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:62533156 (59.6 MiB) TX bytes:4227359 (4.0 MiB)

wmaster0 Link encap:UNSPEC HWaddr 00-1F-DF-FD-38-2C-00-00-00-00-00-00-00-00-00-00
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Thanks
 
Old 01-19-2010, 08:52 AM   #14
nimnull22
Senior Member
 
Registered: Jul 2009
Distribution: OpenSuse 11.1, Fedora 14, Ubuntu 12.04/12.10, FreeBSD 9.0
Posts: 1,571

Rep: Reputation: 92
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.
 
Old 01-19-2010, 04:32 PM   #15
dEM0nsTAr
LQ Newbie
 
Registered: Nov 2009
Posts: 28

Original Poster
Rep: Reputation: 15
Yeah I know. I have done that already. Look at the second line in my first post:

interface=`ifconfig | grep -iE "(wlan0|gprs0)" | awk '{print $1}'`

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?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to switch interfaces jasonenid Linux - Networking 2 10-04-2008 03:22 AM
How to switch user via shell script cool_anupam Linux - General 7 05-29-2008 10:01 PM
How do i switch users in a shell script? koobi Programming 8 01-05-2008 02:19 PM
ifconfig+no connection established coolirc Slackware 2 03-04-2007 02:12 PM
Time out in Connection established state if no Data flows on that connection asurya Linux - Networking 2 04-10-2005 03:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 09:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration