LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Shell script should switch if connection established but doesn't (https://www.linuxquestions.org/questions/linux-networking-3/shell-script-should-switch-if-connection-established-but-doesnt-783076/)

dEM0nsTAr 01-18-2010 05:51 PM

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.

nimnull22 01-18-2010 06:14 PM

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.

dEM0nsTAr 01-18-2010 06:24 PM

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...

nimnull22 01-18-2010 06:35 PM

Do you have command: iwconfig wlan0?

dEM0nsTAr 01-18-2010 06:39 PM

No, only ifconfig wlan0

nimnull22 01-18-2010 06:51 PM

Quote:

Originally Posted by dEM0nsTAr (Post 3831486)
Do you know a better way to get the status of wlan0?

What kind of status do you need?

dEM0nsTAr 01-18-2010 06:55 PM

A reliable way to determine if wlan0 is active (and maybe has an internet connection).

nimnull22 01-18-2010 07:19 PM

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.

dEM0nsTAr 01-18-2010 08:17 PM

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?

nimnull22 01-18-2010 08:31 PM

Have a look:
http://stefans.datenbruch.de/nokia770/

dEM0nsTAr 01-18-2010 08:43 PM

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.

nimnull22 01-18-2010 08:53 PM

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.

dEM0nsTAr 01-19-2010 04:32 AM

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 :)

nimnull22 01-19-2010 08:52 AM

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.

dEM0nsTAr 01-19-2010 04:32 PM

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?


All times are GMT -5. The time now is 12:50 AM.