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
Solved

Linux raspberry experts (awk and shellscripting)

Posted on 2014-01-26
7
439 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 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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 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

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
winscp where are logs stored 3 51
Powershell command 2 37
replacing file name with spaces in ibm aix. 2 23
AWK: Pythagoras bp script 18 27
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 …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

808 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