• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 486
  • Last Modified:

Linux raspberry experts (awk and shellscripting)

Hi ,

I need help on modifying the script to connect WIFI.

I googled few sites and make a script but not able to complete it so seek your help on getting this completed.

Need the following to add to the script and where , i have lost.

1. Without reboot the connection should happen  (restart wpa_supplicant or restart networking should be fine)
2. Automatically reconnect when connection lost.
3. If different SSID /password specified- connect to the new SSID

Please find my script below.

#killall -9 wpa_supplicant

iwlist wlan0 scanning > /tmp/wifiscan #save scan results to a temp file
scan_ok=$(grep "wlan" /tmp/wifiscan) #check if the scanning was ok with wlan0
if [ -z "$scan_ok" ]; then
    killall -9 wpa_supplicant
    iwlist wlan0-1 scanning > /tmp/wifiscan
scan_ok=$(grep "wlan" /tmp/wifiscan) #check if the scanning was ok
if [ -z "$scan_ok" ]; then #if scan was not ok, finish the script
    echo -n "
WIFI scanning failed.
if [ -f /tmp/ssids ]; then
    rm /tmp/ssids
n_results=$(grep -c "ESSID:" /tmp/wifiscan) #save number of scanned cell
while [ "$i" -le "$n_results" ]; do
        if [ $i -lt 10 ]; then
                cell=$(echo "Cell 0$i - Address:")
                cell=$(echo "Cell $i - Address:")
        j=`expr $i + 1`
        if [ $j -lt 10 ]; then
                nextcell=$(echo "Cell 0$j - Address:")
                nextcell=$(echo "Cell $j - Address:")
        awk -v v1="$cell" '$0 ~ v1 {p=1}p' /tmp/wifiscan | awk -v v2="$nextcell" '$0 ~ v2 {exit}1' > /tmp/onecell #store only one cell info in a temp file

        ## Uncomment following line to show mac address ##

        #oneaddress=$(grep " Address:" /tmp/onecell | awk '{print $5}')

        onessid=$(grep "ESSID:" /tmp/onecell | awk '{ sub(/^[ \t]+/, ""); print }' | awk '{gsub("ESSID:","");print}')
        oneencryption=$(grep "Encryption key:" /tmp/onecell | awk '{ sub(/^[ \t]+/, ""); print }' | awk '{gsub("Encryption key:on", "on");print}' | awk '{gsub("Encryption key:off", "off");print}')
        onepower=$(grep "Quality=" /tmp/onecell | awk '{ sub(/^[ \t]+/, ""); print }' | awk '{gsub("Quality=", "");print}' | awk -F '/70' '{print $1}')
        onepower=$(awk -v v3=$onepower 'BEGIN{ print v3 * 10 / 7}')
        if [ -n "$oneaddress" ]; then                                                                                                            
                echo "$onessid  $oneaddress $oneencryption $onepower" >> /tmp/ssids                                                              
                echo "-d SSID$i=$onessid -d Pass$i=$oneencryption -d Quality$i=$onepower -d Timestamp$i=$(date +'%T')" >> /tmp/ssids                                                                          
        i=`expr $i + 1`
rm /tmp/onecell
#awk '{printf("%5d %s\n",NR,$0)}' /tmp/ssids > /tmp/sec_ssids #add numbers at beginning of line
#grep ESSID /tmp/wifiscan | awk '{ sub(/^[ \t]+/, ""); print }' | awk '{printf("%5d : %s\n", NR,$0)}' | awk '{gsub("ESSID:", "");print}' > /tmp/ssids #generate file with only numbers and names

#replace new line in file with ampersands and then replace spaces  in network names with a dash (need to change this)
URL_string=$(sed ':a;N;$!ba;s/\n/ /g' < /tmp/ssids | sed s/\"//g) 
#| sed 's/ /-/g')

#echo "$URL_string"
#rm /tmp/ssids1
#echo $URL_string >> /tmp/ssids1

#cat /tmp/ssids1  #show ssids list

#echo "curl --silent -v -X POST $URL_string 'http://mysite/settings/wifi.php'"

Result=$(curl --silent -X POST $URL_string  'http://mysite/settings/get_wifi.php')
#curl --silent -v -X POST $URL_String  'http://mysite/settings/wifi.php'
#Result=$(curl --silent 'http://mysite/settings/wifi.php?'$URL_string)
echo $Result

Open in new window

  • 3
  • 2
  • 2
1 Solution
Gerwin Jansen, EE MVETopic Advisor Commented:
Hi, think you maybe won't have to figure this out yourself. When checking some existing  solutions at a few rpi sites I found this:

Go to /etc/ifplugd/action.d/ and rename the ifupdown file to ifupdown.original
Then do: cp /etc/wpa_supplicant/ifupdown.sh ./ifupdown
Finally: sudo reboot
That's all. Test this by turning off/on your AP; you should see that your Raspberry Pi properly reconnects.

I cannot test this for you because my rpi is wired. Please let me know if the above suggestion works for you. Thanks.
magentoAuthor Commented:
Hi ,

Thanks Gerwin .

So there is no need to modify the above script ?
nociSoftware EngineerCommented:
wpa_supplicant is capable of finding the right entry on it's own. If entries are in the /etc/wpa_supplicant/wpa_supplicant.conf file.

They can be prioritized, wpa_supplicant will find the right one for you when it's running.
(The AP's do need to send out the Beacon frames though, they need to be "visible").
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

magentoAuthor Commented:
Hi Noci,

I am sorry . I think i explained them not clear in my question.

I have 2 wifi , each has ssid and password.

I need a  bash script using Using wicd-cli  to do it not via manually.

-connect to WiFi using SSID and password provided as parameters
-check for wifi connection and reconnect if wifi is dropped
-connect to a different wifi if different SSID and password are specified

Thank you and your advice is much appreciated.
Gerwin Jansen, EE MVETopic Advisor Commented:
>> So there is no need to modify the above script ?
Correct, notice that I cannot test this for you.

>> I have 2 wifi , each has ssid and password.
Hmm. Besides the 2 WiFi connections, you still want to provide another one with SSID and password? How would you provide the 'other' SSID and password then?

As Nocis is mentioning above, wpa_supplicant can manage multiple entries.
magentoAuthor Commented:
Hi Experts,

I found this post , but not sure how to convert it into a script.


nociSoftware EngineerCommented:
First you need to find the config file for wpa_supplicant.
one of:
- /etc/wpa_supplicant.conf
- /etc/wpa_supplicant/wpa_supplicant.conf

then you can add entries:

wpa_passphrase SSID1 PassPhrase1 >>wpa_config_file
wpa_passphrase SSID2 PassPhrase2 >>wpa_config_file

then restart/reload wpa_supplicant.

After that it should connect to one of both. It should stick with one of them until a connection is lost, then it will hunt again for available AP's.
If you like one to connect in preference then add a line
priority=n to each entry, between the curly brackets.

 The higher the number the more important.

with wpa_cli it is possible to disconnect/connect if you like. For graphical desktops there also is a wpa_gui that can help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now