# bash script with windows?



## Bigjohn123

I didn't know where to post this, since it deals with both UNIX & windows.

Anyway, is there a way to run this script on windows xp?


Code:


#!/bin/bash
# Finds the strongest unencrypted AP and tries to connect to it via dhcp
# Call this script like "wifi.sh wlan0"
TEMP=/tmp/bestap.tmp
LOCK=/var/lock/bestap.lock
if [ `whoami` != "root" ];then
        echo "Sorry, you need to be root to run this program"
        exit 1
fi

if [[ -z $1 ]];then
        echo "USAGE: $0 device"
        exit 1
else
        interface=$1
fi

# Checking for lock
if [[ -e $LOCK ]];then
        exit 1; # Too simply nothing to do here :)
else
        touch $TEMP $LOCK
fi


# Proggy
iwlist $interface scan > $TEMP
NumAPs=`cat $TEMP | grep ESSID | wc -l`
BestAP=0
BestQuality=-1
for i in `seq 1 $NumAPs`;
do
   # Check if AP is encrypted
   Encryption=`cat $TEMP | grep Encryption | head -n$i | tail -n1 | cut -d":" -f2`
   if [ $Encryption = "off" ]; then
      # Find AP with the highest quality
      QUALITY=`cat $TEMP | grep Quality | head -n$i | tail -n1 | cut -d":" -f2 | cut -d"/" -f1 | sed 's/ //g'`
      if [ "$QUALITY" -gt "$BestQuality" ]; then
         BestQuality=$QUALITY
         BestAP=$i
      fi
   fi
done
if [ $BestAP -gt 0 ]; then
   # Yay, we found an unencrypted AP:
   echo Connecting to...
   ESSID=`cat $TEMP | grep ESSID | head -n$BestAP | tail -n1 | cut -d""" -f2`
   echo ESSID=$ESSID
   MODE=`cat $TEMP | grep Mode | head -n$BestAP | tail -n1 | cut -d":" -f2`
   echo Mode=$MODE
   CHANNEL=`cat $TEMP | grep Channel | head -n$BestAP | tail -n1 | cut -d"(" -f2 | sed 's/Channel //g' | sed 's/)//g'`
   echo Channel=$CHANNEL
   # Connect
   iwconfig $interface essid $ESSID mode $MODE channel $CHANNEL
   if [ -e /etc/dhcpc/dhcpcd-${interface}.pid ]; then
      rm /etc/dhcpc/dhcpcd-${interface}.pid
   fi
   dhcpcd $interface
   # Cleanup
fi
rm -f $TEMP $LOCK

Also, would anyone know how to make this file executable? I know the code to do it, but I do not know where to place it in the script.

thanks!


----------



## lotuseclat79

If you download and install the Cygwin environment for Windows you may be able to execute the script.

Within a Cygwin terminal window you might be able to issue the chmod command to make the script executable: 
# chmod 700 scriptname
i.e. in a Unix-like environment, the chmod command is issued to make a shell file executable, i.e. which you could do from within a script that you generate, or issue a command like: 
# sh scriptname
where the 'sh' script is a link to the 'bash' shell executable.

I base these statements on:
1) I think Cygwin has a bash shell - but, I do not know for sure.
2) If Cygwin has a bash shell, it might have a chmod command as well - but, I do not know for sure.

Note: The Cygwin gcc compiler would have to be able to compile those commands for this to work, i.e. the bash shell, and the chmod command.

Here is a link to Cygwin where you may be able to find the answers to those questions in the FAQ: http://www.cygwin.com

-- Tom


----------



## Bigjohn123

It worked, thanks! I have cygwin, but I never knew where to issue the chmod command. Now I know, thanks again.


----------

