#!/bin/bash # ifswitch - network interface configuration switching tool v0.9 # with this tool you can switch your w/lan config very fast # Copyright (c) 2005 by Stefan Behte # # ifswitch 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 2 of the License, or # (at your option) any later version. # # ifswitch 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 ifswitch; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # The following can be set in the config file: # - interface ip (eth0_ip=... or eth0_ip=random or eth0_ip=random#pre.fix) # - broadcast (eth0_broadcast=...) # - netmask (eth0_netmask=...) # - default gateway (defaultgate=...) # - dns server(s), you can specify multiple lines (dns=ip) # - mac address; complete, prefix or random (eth0_mac=aa:bb:cc:dd:ee:ff or eth0_mac=random or eth0_mac=random#pr:ef:ix) # - static arp entries to prevent arp spoofing attacks, you can specify multiple lines (static_arp=ip#mac) # - wlan ssid & wep key (eth0_ssid=ssid#key) # # Merged from llog and wlog (previously written for wlan and lan configuration switching) # # Changelog: # 12.12.2005 - Started coding, alpha ready # 13.12.2005 - Fixed bug in addconfig() # - added showconfig() to the menu # - added [k]ill dhcpcd to the menu # - rewritten randmac, now you can use a prefix for randmac -> randmac 00:00:86 # - added RANDOM / randip -> random ips possible, you can also use a prefix -> randip 192.168.0 # - changed parseconfig() and addconfig() to work with randmac/randip # 15.12.2005 - added rotate command line switch, useful if you're pentesting ;) # 18.12.2005 - confirmed that WLAN mode works fine :) # 27.12.2005 - fixed typo with wlan # - fixed severe error with using macs # 03.01.2005 - created killdhcpcd() # - CONFDIR will now be created if it does not exist and you try adding an entry # # TODO: # - exclude mode for RANDOM # - check if RANDOM ip/mac is already there # - IP, MAC-Ranges für RANDOM # - use netmask for calculation of random ip # - add WPA(2) support # # Known bugs: # - When creating config files via hand you can get into trouble. Just use the menu dialog to create configs. # # Written by Stefan Behte # # Please send bugs, comments, wishes and success stories to: # Stefan.Behte at gmx dot net # # Also have a look at my page: # http://ge.mine.nu/ # # JUST EDIT THE CONFDIR LINE, NO OTHER LINES NEED TO BE MODIFIED CONFDIR=/etc/ifswitch TOKEN="#" # check if tools are available if [ "`which ifconfig`" = "" ] > /dev/null 2>/dev/null then echo "Please check if iwconfig is installed." exit -1 fi if [ "`which route`" = "" ] > /dev/null 2>/dev/null then echo "Please check if route is installed." exit -1 fi ################################################################### # get random mac -> randmac # call it like this: newmac=`randmac` randmac() { if [ "$1" != "" ] then newmac="$1" j=2 else newmac= j=0 fi while [ 1 ] do if [ "${#newmac}" = "17" ] # a mac has 6 bytes + 5 delimiters -> 17 chars -> 00:11:22:33:44:55 then break fi if [ "$j" = "2" ] then newmac="${newmac}": j=0 fi nr=$[$RANDOM % 16] if [ "$nr" -lt 10 ]; then newmac="${newmac}${nr}"; fi if [ "$nr" = "10" ]; then newmac="${newmac}a"; fi if [ "$nr" = "11" ]; then newmac="${newmac}b"; fi if [ "$nr" = "12" ]; then newmac="${newmac}c"; fi if [ "$nr" = "13" ]; then newmac="${newmac}d"; fi if [ "$nr" = "14" ]; then newmac="${newmac}e"; fi if [ "$nr" = "15" ]; then newmac="${newmac}f"; fi j=$[$j + 1] done echo "$newmac" } ################################################################### randip() { if [ "$1" != "" ] then newip="$1" else newip= fi while [ 1 ] do nr=$[$RANDOM % 256] if [ "$newip" = "" ] then newip=$nr else newip=${newip}.$nr fi if [[ $newip == *.*.*.* ]] then break; fi done echo $newip } showconfig() { echo count=0 ls -1 $CONFDIR/* | while read line do count=$[$count +1] echo -n "[$count]: " grep "name=" $line | awk -F= '{print $2}' done } chooseconfig() { showconfig echo "[q]uit" echo -n ">" read nr if [ "$nr" != "q" ] && [ "$nr" != "e" ] then parseconfig "`ls -1 $CONFDIR/* | head -n $nr | tail -n 1`" read moo fi } addconfig() { if [ ! -e $CONFDIR ] then mkdir $CONFDIR && echo Created CONFDIR:$CONFDIR fi echo echo "Just press Enter if you do not wish to specify." echo -n "filename>" read FILE echo -n "description>" read desc echo "name=\"${desc}\"" >> $CONFDIR/$FILE echo -n "interface>" read iface echo -n "ssid#key>" read ssid if [ "$ssid" != "" ] then echo "${iface}_ssid=$ssid" >> $CONFDIR/$FILE fi echo -n "mac>" read mac if [ "$mac" != "" ] then if [ "$mac" = "RANDOM" ] || [ "$mac" = "random" ] then echo -n "mac-prefix>" read macpre if [ "$macpre" = "" ] then echo "${iface}_mac=${mac}" >> $CONFDIR/$FILE # no prefix, just random else echo "${iface}_mac=${mac}${TOKEN}${macpre}" >> $CONFDIR/$FILE # random with prefix fi else echo "${iface}_mac=$mac" >> $CONFDIR/$FILE # MAC was completely specified fi fi echo -n "ip>" read ip if [ "$ip" != "" ] then if [ "$ip" = "RANDOM" ] || [ "$ip" = "random" ] then echo -n "ip-prefix>" read ippre if [ "$ippre" = "" ] then echo "${iface}_ip=${ip}" >> $CONFDIR/$FILE # no prefix, just random else echo "${iface}_ip=${ip}${TOKEN}${ippre}" >> $CONFDIR/$FILE # random with prefix fi else echo "${iface}_ip=$ip" >> $CONFDIR/$FILE fi fi echo -n "netmask>" read netmask if [ "$netmask" != "" ] then echo "${iface}_netmask=$netmask" >> $CONFDIR/$FILE fi echo -n "broadcast>" read broadcast if [ "$broadcast" != "" ] then echo "${iface}_broadcast=$broadcast" >> $CONFDIR/$FILE fi echo -n "default gateway>" read defaultgate if [ "$defaultgate" != "" ] then echo "defaultgate=$defaultgate" >> $CONFDIR/$FILE fi dns=dummy while [ "$dns" != "" ] do echo -n "dns>" read dns if [ "$dns" != "" ] then echo "dns=$dns" >> $CONFDIR/$FILE fi done static=dummy while [ "$static" != "" ] do echo -n "static arp entry [IP#MAC]>" read static if [ "$static" != "" ] then echo "static_arp=$static" >> $CONFDIR/$FILE fi done } delconfig() { showconfig echo "[q]uit" echo -n ">" read nr if [ "$nr" != "q" ] && [ "$nr" != "e" ] then rm -f "`ls -1 $CONFDIR/* | head -n $nr | tail -n 1`" && echo "OK" || echo FAILED fi sleep 1 } parseconfig() { cat $1 | while read line do one=`echo $line | awk -F= '{print $1}'` two=`echo $line | awk -F= '{print $2}'` if [ "$one" = "defaultgate" ] then echo -n "Setting default gateway $two: " route add default gw $two &>/dev/null && echo OK || echo FAILED fi if [ "$one" = "dns" ] then echo -n "Setting dns server $two: " if [ "$dns" = "already" ] then echo "nameserver $two" >> /etc/resolv.conf && echo OK || echo FAILED else echo "nameserver $two" > /etc/resolv.conf && echo OK || echo FAILED dns=already fi fi if [[ "${one}" == *_netmask ]] then iface=`echo $one | awk -F_ '{print $1}'` echo -n "Setting netmask $two on interface $iface: " ifconfig $iface netmask $two &>/dev/null && echo OK || echo FAILED fi if [[ "${one}" == *_broadcast ]] then iface=`echo $one | awk -F_ '{print $1}'` echo -n "Setting broadcast $two on interface $iface: " ifconfig $iface broadcast $two &>/dev/null && echo OK || echo FAILED fi if [[ "${one}" == *_mac ]] then iface=`echo $one | awk -F_ '{print $1}'` if [[ "${two}" == RANDOM* ]] || [[ "${two}" == random* ]] # if iface_mac=RANDOM -> get random mac then smac=`echo $two | awk -F"$TOKEN" '{print $2}'` if [ "$smac" != "" ] then two=`randmac ${smac}` echo -n "Setting random mac $two (prefix $smac) on interface $iface: " else two=`randmac` echo -n "Setting random mac $two on interface $iface: " fi else echo -n "Setting mac $two on interface $iface: " fi ifconfig $iface down &>/dev/null && ifconfig $iface hw ether $two &>/dev/null && ifconfig $iface up &>/dev/null && echo OK || echo FAILED fi if [[ "${one}" == *_ip ]] then iface=`echo $one | awk -F_ '{print $1}'` if [[ "${two}" == RANDOM* ]] || [[ "${two}" == random* ]] # if iface_ip=RANDOM -> get random ip then sip=`echo $two | awk -F"$TOKEN" '{print $2}'` if [ "$sip" != "" ] then two=`randip ${sip}` echo -n "Setting random ip $two (prefix $sip) on interface $iface: " else two=`randip` echo -n "Setting random ip $two on interface $iface: " fi ifconfig $iface $two &>/dev/null && echo OK || echo FAILED else if [ "$two" = "dhcp" ] then echo -n "Using DHCP auto configuration: " dhcpcd &>/dev/null && echo OK || echo FAILED else echo -n "Setting ip $two on interface $iface: " ifconfig $iface $two &>/dev/null && echo OK || echo FAILED fi fi fi ## static arp entries, multiple are possible if [[ "${one}" == static_arp ]] then iface=`echo $one | awk -F_ '{print $1}'` staticip=`echo $two | awk -F"$TOKEN" '{print $1}'` staticarp=`echo $two | awk -F"$TOKEN" '{print $2}'` echo -n "Adding static arp entry $staticip/$staticarp : " arp -s $staticip $staticarp &>/dev/null && echo OK || echo FAILED fi ## WLAN # eth0_ssid=WLAN#12345 if [[ "${one}" == *_ssid ]] then iface=`echo $one | awk -F_ '{print $1}'` essid=`echo $two | awk -F"$TOKEN" '{print $1}'` key=`echo $two | awk -F"$TOKEN" '{print $2}'` if [ "$key" != "" ] then echo -n "Using essid $essid with key $key on interface $iface: " iwconfig $iface essid $essid key $key &>/dev/null && echo OK || echo FAILED else echo -n "Using essid $essid on interface $iface: " iwconfig $iface essid $essid &>/dev/null && echo OK || echo FAILED fi fi done } testinet() { echo dgw=`route -n | grep ^0.0.0.0 | awk '{print $2}'` printf "ping default gateway ($dgw)\t" ping -c1 -W 1 $dgw &>/dev/null if [ "$?" = "0" ] then echo -e "[\033[1;32mOK\033[0m]" else echo -e "[\033[1;31mFAILED\033[0m]" fi printf "resolve www.google.com\t\t\t" host www.google.de &>.dns if grep "has add" .dns &>/dev/null then echo -e "[\033[1;32mOK\033[0m]" else echo -e "[\033[1;31mFAILED\033[0m]" fi rm .dns printf "ping www.google.com\t\t\t" ping -c1 -W 1 www.google.com &>/dev/null if [ "$?" = "0" ] then echo -e "[\033[1;32mOK\033[0m]" else echo -e "[\033[1;31mFAILED\033[0m]" fi read moo } killdhcpcd() { killall -9 dhcpcd &>/dev/null rm -f /var/run/dhcpcd* &>/dev/null } menu() { clear echo "ifswitch - network interface config switcher v0.9" echo "" echo "[u]se existing config" echo "[s]how configs" echo "[a]dd network configuration" echo "[d]elete an existing config" echo "[t]est internet connectivity" echo "[k]ill running dhcpcd" echo "[r]estart pcmcia services" echo "[e]nd" echo -n ">" read line if [ "$line" = "" ] then line=dummy fi case $line in u) chooseconfig;; s) showconfig;read moo;; a) addconfig;; d) delconfig;; t) testinet;; k) killdhcpcd;; r) echo "";/etc/init.d/pcmcia restart;; e) echo "";clear && exit 1;; q) echo "";clear && exit 1;; esac } if [ "$1" != "" ] then if [ "$1" = "-h" ] || [ "$1" = "--help" ] then echo echo "ifswitch - network interface config switcher v0.9" echo "usage: ifswitch [Config-NR] [rotate] [time to wait between rotate]" echo " [-show]" echo " Command line mode is mostly only used for rotate-mode" echo " Call ifswitch without arguments to use the menu" echo exit -1 fi if [ "$2" = "rotate" ] && [ "$3" != "" ] then while [ 1 ] do parseconfig "`ls -1 $CONFDIR/* | head -n $1 | tail -n 1`" echo sleep $3 done fi if [ "$1" = "-show" ] || [ "$1" = "-s" ] then showconfig echo exit 1 fi parseconfig "`ls -1 $CONFDIR/* | head -n $1 | tail -n 1`" echo exit fi while [ 1 ] do clear menu done