Link to home
Start Free TrialLog in
Avatar of magento
magento

asked on

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

Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

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.
Avatar of magento
magento

ASKER

Hi ,

Thanks Gerwin .

So there is no need to modify the above script ?
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").
Avatar of magento

ASKER

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.
>> 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.
Avatar of magento

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of noci
noci

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial