- Code: Select all
#!/bin/bash
# Custom wifi connexion by analyzer (requesthelp@free.fr).
############################################
# THIS CAN BE MODIFIED
interface="wlan0"
sleeptime="1"
MACDEFAULT="00:XX:E8:XX:EA:XX"
AP="any"
############################################
# Normal color
NORMAL='\033[0;39m'
# RED: Error message
RED='\033[1;31m'
# YELLOW: Warning message
YELLOW='\033[1;33m'
# GREEN: Success message
GREEN='\033[1;32m'
RES_COL=70
MOVE_TO_COL="\\033[${RES_COL}G"
[[ $# -ne 1 ]] && echo "USAGE: $0 [X1|X2|X3|none]" && exit 1
[[ $1 = "none" ]] && exit 2
NETWORK=$1
case $NETWORK in
"X1" )
ESSID="essid1"
KEY="XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX"
CHANNEL="10"
IP="192.168.0.250"
GATEWAY="192.168.0.254"
MACADDR="AA:13:37:13:37:AA"
;;
"X2" )
ESSID="essid2"
KEY="XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX"
CHANNEL="11"
IP="192.168.0.250"
GATEWAY="192.168.0.254"
MACADDR="XX:0E:XX:FD:XX:E8"
#AP="2A:XX:FD:XX:04:XX"
;;
"X3" )
ESSID="essid3"
KEY="XX:XX:XX:XX:XX"
CHANNEL="11"
IP="192.168.0.250"
GATEWAY="192.168.0.254"
;;
* )
echo "Unknown network ..."
echo "USAGE: $0 [X1|X2|X3|none]"
exit 3
;;
esac
[[ -n $IP ]] && eval `ipcalc --netmask $IP`
[[ -n $NETMASK ]] && eval `ipcalc --broadcast $IP $NETMASK`
# Asking sudo password if needed before execution
sudo echo "-----------------------------------"
echo "Switching to $NETWORK network ..."
echo -n "Shutting down interface $interface ... "
sleep $sleeptime
if [ `ifconfig $interface | grep -c "UP"` != "0" ] ; then
sudo ifconfig $interface down
if [ $? -eq 0 ] ; then
echo -e "$MOVE_TO_COL[ ${GREEN}OK${NORMAL} ]"
else
echo -e "$MOVE_TO_COL[ ${RED}KO${NORMAL} ]"
exit 4
fi
else
echo -e "$MOVE_TO_COL[ ${YELLOW}KO${NORMAL} ] => (Already down) => [ ${GREEN}OK${NORMAL} ]"
fi
echo -n "Using fake MAC address ... "
sleep $sleeptime
if [ -z $MACADDR ] ; then
#sudo ifconfig $interface hw ether 00:00:00:00:00:01
sudo ip link set dev $interface address $MACDEFAULT
if [ $? -eq 0 ] ; then
echo -e "$MOVE_TO_COL[ ${YELLOW}KO${NORMAL} ] => (None specified, using default) => [ ${GREEN}OK${NORMAL} ]"
else
echo -e "$MOVE_TO_COL[ ${RED}KO${NORMAL} ]"
exit 5
fi
else
#ifconfig $interface hw ether $MACADDR
sudo ip link set dev $interface address $MACADDR
if [ $? -eq 0 ] ; then
echo -e "$MOVE_TO_COL[ ${GREEN}OK${NORMAL} ] => ($MACADDR)"
else
echo -e "$MOVE_TO_COL[ ${RED}KO${NORMAL} ]"
exit 5
fi
fi
echo -n "Switching $interface to managed mode ... "
sleep $sleeptime
if [ `iwconfig $interface | grep -c -e "Monitor"` != "0" ] ; then
sudo iwconfig $interface mode Managed
if [ $? -eq 0 ] ; then
echo -e "$MOVE_TO_COL[ ${GREEN}OK${NORMAL} ]"
else
echo -e "$MOVE_TO_COL[ ${RED}KO${NORMAL} ]"
exit 6
fi
else
echo -e "$MOVE_TO_COL[ ${YELLOW}KO${NORMAL} ] => (Already managed) => [ ${GREEN}OK${NORMAL} ]"
fi
#echo "$PARAMWIFI"
sudo iwconfig $interface essid "$ESSID" channel $CHANNEL key $KEY
echo -n "Switching on interface $interface ... "
sleep $sleeptime
sudo ifconfig $interface up
if [ $? -eq 0 ] ; then
echo -e "$MOVE_TO_COL[ ${GREEN}OK${NORMAL} ]"
else
echo -e "$MOVE_TO_COL[ ${RED}KO${NORMAL} ]"
exit 7
fi
echo -n "No DHCP client running for this interface ... "
sleep $sleeptime
dhcp_client=`ps aux | grep -E "[d]hclient.*$interface" | sed 's/[[:space:]]\{1,\}/ /g' | cut -f2 -d" "`
if test -z "$dhcp_client"
then
echo -e "$MOVE_TO_COL[ ${GREEN}OK${NORMAL} ]"
else
sudo kill -9 $dhcp_client
echo -e "$MOVE_TO_COL[ ${YELLOW}KO${NORMAL} ] => (process $dhcp_client killed) => [ ${GREEN}OK${NORMAL} ]"
fi
if [ -z $IP ] ; then
echo -n "Trying to obtain DHCP lease (10 sec) ... "
sudo dhclient -1 -T 10 $interface &> /dev/null
if [ $? -eq 0 ] ; then
echo -e "$MOVE_TO_COL[ ${GREEN}OK${NORMAL} ]"
else
echo -e "$MOVE_TO_COL[ ${RED}KO${NORMAL} ] => (NO DHCP OFFER)"
exit 8
fi
else
echo -n "IP is provided, using static address ... "
sleep $sleeptime
sudo ifconfig $interface $IP netmask $NETMASK
if [ $? -eq 0 ] ; then
echo -e "$MOVE_TO_COL[ ${GREEN}OK${NORMAL} ] => ($IP/$NETMASK)"
else
echo -e "$MOVE_TO_COL[ ${RED}KO${NORMAL} ]"
exit 9
fi
sudo route -n | grep -E "UG(.)*$interface" &> /dev/null
if [ $? -eq 1 ] ; then
# No entry for this interface
echo -n "Routing table defined ... "
sleep $sleeptime
sudo /sbin/ip route append default metric 10 via $GATEWAY dev $interface
else
# Existing entry for this interface
echo -n "Routing table redefined ... "
sleep $sleeptime
sudo /sbin/ip route replace default metric 10 via $GATEWAY dev $interface
fi
#sudo /sbin/ip route replace 169.254.0.0/16 dev $interface
echo -e "$MOVE_TO_COL[ ${GREEN}OK${NORMAL} ]"
fi
sudo iwconfig $interface ap $AP
echo -n "Connexion to essid \"$ESSID\" ... "
sleep $(($sleeptime * 5))
if [ `ifconfig $interface | grep -c -E "inet(.)*addr"` != "0" ] ; then
if [ `iwconfig $interface | grep -c -e "Not-Associated"` != "0" ] ; then
echo -e "$MOVE_TO_COL[ ${YELLOW}KO${NORMAL} ] => (Failed to associate to AP \"$AP\")"
elif [ `iwconfig $interface | grep -c -e "Invalid"` != "0" ] ; then
echo -e "$MOVE_TO_COL[ ${YELLOW}KO${NORMAL} ] => (Invalid AP)"
else
echo -e "$MOVE_TO_COL[ ${GREEN}OK${NORMAL} ]"
fi
else
echo -e "$MOVE_TO_COL[ ${RED}KO${NORMAL} ]"
exit 10
fi
You will also need ipcalc to run this script, feel free to mail me if you don't find this executable

(I know that there are others ipcalc, but this one is from RedHat).