[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Linux raspberry experts (awk and shellscripting)

Posted on 2014-01-26
7
Medium Priority
?
480 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
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 40

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 38

Expert Comment

by:Gerwin Jansen, EE MVE
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 40

Accepted Solution

by:
noci earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

650 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