Wait on NetworkManager for wifi connection

Forum rules
Before you post please read this

Wait on NetworkManager for wifi connection

Postby AlbertP on Tue May 15, 2012 7:06 am

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?
Registered Linux User #528502
Image
Feel free to correct me if I'm trying to write in Spanish, French or German.
AlbertP
Level 16
Level 16
 
Posts: 6513
Joined: Sun Jan 30, 2011 12:38 pm
Location: The Netherlands

Linux Mint is funded by ads and donations.
 

Re: Wait on NetworkManager for wifi connection

Postby xenopeek on Tue May 15, 2012 9:40 am

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
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14505
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Wait on NetworkManager for wifi connection

Postby remoulder on Tue May 15, 2012 10:20 am

Could you test if the wlan has an active ip as in
Code: Select all
ifconfig wlan0 | grep -i 'inet addr'
[Edit] your original post and add [SOLVED] once your question is resolved.

“The people are my God” stressing the factor determining man’s destiny lies within man not in anything outside man, and thereby defining man as the dominator and remoulder of the world.
User avatar
remoulder
Level 17
Level 17
 
Posts: 7667
Joined: Fri May 09, 2008 1:14 pm

Re: Wait on NetworkManager for wifi connection

Postby AlbertP on Tue May 15, 2012 11:45 am

Thanks. Is there a way in Bash to let the script wait until the command returns something?
Registered Linux User #528502
Image
Feel free to correct me if I'm trying to write in Spanish, French or German.
AlbertP
Level 16
Level 16
 
Posts: 6513
Joined: Sun Jan 30, 2011 12:38 pm
Location: The Netherlands

Re: Wait on NetworkManager for wifi connection

Postby xenopeek on Tue May 15, 2012 2:04 pm

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
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14505
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 1 guest