Solved

Linux raspberry experts (awk and shellscripting)

Posted on 2014-01-26
7
419 Views
Last Modified: 2014-01-29
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
fi
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.
    
"
    exit
fi
if [ -f /tmp/ssids ]; then
    rm /tmp/ssids
fi
n_results=$(grep -c "ESSID:" /tmp/wifiscan) #save number of scanned cell
i=1
while [ "$i" -le "$n_results" ]; do
        if [ $i -lt 10 ]; then
                cell=$(echo "Cell 0$i - Address:")
        else
                cell=$(echo "Cell $i - Address:")
        fi
        j=`expr $i + 1`
        if [ $j -lt 10 ]; then
                nextcell=$(echo "Cell 0$j - Address:")
        else
                nextcell=$(echo "Cell $j - Address:")
        fi
        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}')
        onepower=${onepower%.*}
        
        if [ -n "$oneaddress" ]; then                                                                                                            
                echo "$onessid  $oneaddress $oneencryption $onepower" >> /tmp/ssids                                                              
        else                                                                                                                                     
                echo "-d SSID$i=$onessid -d Pass$i=$oneencryption -d Quality$i=$onepower -d Timestamp$i=$(date +'%T')" >> /tmp/ssids                                                                          
        fi
        i=`expr $i + 1`
done
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

0
Comment
Question by:magento
  • 3
  • 2
  • 2
7 Comments
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 39812367
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.
0
 
LVL 5

Author Comment

by:magento
ID: 39813726
Hi ,

Thanks Gerwin .

So there is no need to modify the above script ?
0
 
LVL 39

Expert Comment

by:noci
ID: 39814352
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").
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Author Comment

by:magento
ID: 39814447
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.
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 39814787
>> 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.
0
 
LVL 5

Author Comment

by:magento
ID: 39816882
Hi Experts,

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

http://askubuntu.com/questions/16584/how-to-connect-and-disconnect-to-a-network-manually-in-terminal

Thanks
0
 
LVL 39

Accepted Solution

by:
noci earned 500 total points
ID: 39817140
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now