Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Linux and Unix Linux and Unix
Search Search
Search for:
Tech Support Guy > > >

bash script with windows?


(!)

Bigjohn123's Avatar
Bigjohn123 Bigjohn123 is offline
Member with 30 posts.
THREAD STARTER
 
Join Date: Jul 2007
14-Oct-2007, 11:16 AM #1
bash script with windows?
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's Avatar
Member with 20,583 posts.
 
Join Date: Sep 2003
Location: -71.45091, 42.27841
14-Oct-2007, 11:47 AM #2
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
__________________
The independence created by philosophical insight is - in my opinion - the mark of distinction
between a mere artisan or specialist and a real seeker after truth. - Einstein 1944
Imagination is more important than knowledge. - Einstein
Bigjohn123's Avatar
Bigjohn123 Bigjohn123 is offline
Member with 30 posts.
THREAD STARTER
 
Join Date: Jul 2007
14-Oct-2007, 12:56 PM #3
thanks
It worked, thanks! I have cygwin, but I never knew where to issue the chmod command. Now I know, thanks again.
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑