All pastes #896384 Raw Edit

Someone

public text v1 · immutable
·published 2008-02-08 16:40 UTC
rendered paste body
#!/bin/sh
# runNetworking.sh: Bash Script for Detect and Configure Wireless Networks: 
#                   Using paired combinations of essid-key values
#
#                   (requires iwconfig,ifconfig and dhclient commands)
#                   (Tested on: Debian GNU/Linux 4.0)
#                               Copyright (C) 2008  J. Rivera Pineda
#				xtecuan (at) gmail (dot) com
#                               http://gente.debian.org.sv/~xtecuan/runNetworking/
#							 
#                    
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details:
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses/.


export ESSIDS_FILE=/tmp/essid.tmp
export INTERFACE=wlan0
export KNOWN_ESSIDS='2WIRE206-A1D2351733 casita-s:turbo'
export ON_STATIC_IP=192.168.1.85
export ON_STATIC_GW=192.168.1.254



sudo /sbin/iwlist wlan0 scanning | grep ESSID > $ESSIDS_FILE

#cessid=''
#cpassw=''

cIp=''

verificarIp(){

    result=`sudo /sbin/ifconfig $INTERFACE | grep "inet addr:" | awk '{print $2}'`
    address=${result:5}
    cIp=$address
	
    #return $address

}

imprimirMensajeVerIp(){

echo "Su interfaz $1 se le asigno la IP: $2"

}


configurarInterfaz(){

	sudo /sbin/iwconfig $INTERFACE essid $1 key $2
	sudo /sbin/dhclient $INTERFACE
	verificarIp
	address=$cIp
	if [ -n $address ]
	then
	   imprimirMensajeVerIp $INTERFACE $address
	else
	   ifconfig $INTERFACE $ON_STATIC_IP
           route add default gw $ON_STATIC_GW
	   verificarIp
           newAdd=$cIp
	   if [ -n $newAdd ]
	   then
		imprimirMensajeVerIp $INTERFACE $newAdd
	   else
		echo "Ya no se que hacer en esta situacion, contacte a su administrador de red!!!" 
	   fi
	fi
}

removerArchivoEssids(){

	sudo rm $ESSIDS_FILE

}


cat ${ESSIDS_FILE} |while read line; do

	newLine=${line#ESSID:\"}
	newLength=`expr length $newLine`
	newLength=`expr $newLength - 1`	
	cessid=${newLine:0:newLength}
	#echo $cessid

	for i in $KNOWN_ESSIDS
	do

		cpos=`expr index "$i" -`
		essid=${i:0:`expr $cpos - 1`}
		pass=${i:cpos}		
		#echo $essid $pass

		if [ $cessid = $essid ]
		then
		  echo "Se encontro la red $cessid"
		  configurarInterfaz $cessid $pass
		fi

	done
	
done

removerArchivoEssids