Page 1 of 1

Wait on NetworkManager for wifi connection

Posted: Tue May 15, 2012 7:06 am
by AlbertP
Hi all,

Is it possible to wait till the wifi (TP-Link USB dongle with Atheros chip) is connected in a bash script, which starts a program that requires network access?

Re: Wait on NetworkManager for wifi connection

Posted: Tue May 15, 2012 9:40 am
by xenopeek
This may work: http://stackoverflow.com/questions/9458 ... 30#9458730

The command "ip route" will show you the active routing. I'm hoping it won't show a route for wlan until the wlan is active, but am not sure. If it works, you could do something like:

Code: Select all

if [ $(ip route | grep wlan0 | wc -l) != 0 ]; then
	# wlan0 found in "ip route" output
else
	# wlan0 not found in "ip route" output
fi

Re: Wait on NetworkManager for wifi connection

Posted: Tue May 15, 2012 10:20 am
by remoulder
Could you test if the wlan has an active ip as in

Code: Select all

ifconfig wlan0 | grep -i 'inet addr'

Re: Wait on NetworkManager for wifi connection

Posted: Tue May 15, 2012 11:45 am
by AlbertP
Thanks. Is there a way in Bash to let the script wait until the command returns something?

Re: Wait on NetworkManager for wifi connection

Posted: Tue May 15, 2012 2:04 pm
by xenopeek
You can do so with an infinite while loop, for example as such (the colon after the while makes it infinite). Tests each time if the wlan0 has an IP address and if so exits the loop to continue your script. If it doesn't yet have an IP address, it sleeps for (for example) 1 second before testing again. You can make that 1 second longer if testing it so often is not needed.

Code: Select all

while : ; do
	if [ $(ifconfig wlan0 | grep -i 'inet addr' | wc -l) != 0 ]; then
		# wlan0 has IP address, exit loop
		break
	fi
	# sleep 1 second
	sleep 1
done