03 noviembre 2014

Script para crear un AP

- Add stop option
- Add option to exit without stopping
- Add restart option
- Add rfkill option
- Add connected clients list
- Automatically find network adapter information
- Change interface menus (A little)
- Change directory for config files from "/temp/"
- Combines "Password" and "No Password" into one script
- Fixed possible reconnect problems after Stopping
- Re-asks options if submitted options are blank
- Re-asks channel if any character other than a number is entered
- Re-asks password if less than 8 characters
- Restart Wicd after stopping (If installed)
- Show loading percentages

Future Plans
- Start Wicd tray icon when script is stopped (If Wicd is installed)
- Add menu option to Add User to blacklist / Edit blacklist
- Save/Load saved config from menu

Run this to install the required packages, remove them from startup, and create the required directory

sudo apt-get install -y arp-scan rfkill dhcpd hostapd; sudo update-rc.d -f hostapd remove
sudo mkdir -p /root/.Acess_Point/

After that edit /etc/init.d/hostapd. Change DAEMON_CONF= to DAEMON_CONF=/etc/hostapd/hostapd.conf
Or paste these lines into the terminal. (Don't do both)

sudo cp  /etc/init.d/hostapd  /etc/init.d/hostapd.orig
OldHostApd="DAEMON_CONF="
NewHostApd="DAEMON_CONF=/etc/hostapd/hostapd.conf"
AddHostApd=$(sed "s~$OldHostApd~$NewHostApd~" /etc/init.d/hostapd)
sudo echo "$AddHostApd" > /etc/init.d/hostapd

This script does NOT work with "sudo". In order to run the script type "su" then ./Access_Point.sh
To enable "su" > "sudo passwd root"

To enable rfkill - change the line rfkill="no" to rfkill="yes"

The Script

#!/bin/bash

######################################
##     User Options

rfkill="no"
#     yes        Diable wifi after stopping  -  Requires >> "sudo rfkill unblock wifi" to renable wireless. This script renables it at the begging of the script, but will disable wifi again if "yes" is entered.
#     no        Do not disable wifi after stopping

######################################


#Make Sure Script Is Ran As Root
clear; if [ $(id -u) != 0 ]; then echo; echo; echo; echo "          Please run as root"; echo; echo; echo; sleep 2s; exit 1; fi

function Get_Interfaces
{
echo "$(ls /sys/class/net/ | grep -Fv lo)" > /root/.Acess_Point/1.txt
cat /root/.Acess_Point/1.txt | sed ':a;N;$!ba;s/\n//g' > /root/.Acess_Point/AvailableConnectionsList.txt
  if [[ $(cat /root/.Acess_Point/AvailableConnectionsList.txt) == "eth0wlan0" ]]; then LanInt="eth0"
WifiInt="wlan0"
 elif [[ $(cat /root/.Acess_Point/AvailableConnectionsList.txt) == "eth1wlan1" ]]; then LanInt="eth1"
WifiInt="wlan1"
 elif [[ $(cat /root/.Acess_Point/AvailableConnectionsList.txt) == "eth0wlan1" ]]; then LanInt="eth0"
WifiInt="wlan1"
 elif [[ $(cat /root/.Acess_Point/AvailableConnectionsList.txt) == "eth1wlan0" ]]; then LanInt="eth1"
WifiInt="wlan0"
 elif [[ $(cat /root/.Acess_Point/AvailableConnectionsList.txt) == "eth0mon.wlan0wlan0" ]]; then LanInt="eth0"
WifiInt="wlan0"
 elif [[ $(cat /root/.Acess_Point/AvailableConnectionsList.txt) == "eth1mon.wlan1wlan1" ]]; then LanInt="eth1"
WifiInt="wlan1"
 elif [[ $(cat /root/.Acess_Point/AvailableConnectionsList.txt) == "eth1mon.wlan0wlan0" ]]; then LanInt="eth1"
WifiInt="wlan0"
 elif [[ $(cat /root/.Acess_Point/AvailableConnectionsList.txt) == "eth0mon.wlan1wlan1" ]]; then LanInt="eth0"
WifiInt="wlan1"
  fi
rm -fr /root/.Acess_Point/1.txt
StatusCheck
}
function StatusCheck                    #  Determin If Ap Is Running    >>  Up/Down Menu
{
if [ ! -z "$(pidof hostapd)" ]; then Running_Menu
 else Down_Menu
fi
}


function Down_Menu
{
Down_Menu=""
while [ "$Down_Menu" != "x" ]
do clear; echo; echo
echo "               Access Point"
echo -e "       \e[00;34m============================\e[00m"
echo "               ESSID:"
echo "             Channel:"
echo "            Password:"
echo "             Network:"
echo "         MAC Address:"
echo -e "       \e[00;34m============================\e[00m"
echo "              Status: DOWN!"
echo; echo
echo "      (1) Create Access Point"
echo "      (2) Create With Password"
echo
echo "      (X) Exit"
echo
read -p "     > " MainMenuOpts
  if [ "$MainMenuOpts" = "1" ]; then echo "NoPass" > /root/.Acess_Point/ApType.txt; Input_Without_Pass_ESSID
 elif [ "$MainMenuOpts" = "2" ]; then echo "Pass" > /root/.Acess_Point/ApType.txt; Input_With_Pass_ESSID
 elif [ "$MainMenuOpts" = "x" ]; then exit
  fi
done
}
function Running_Menu
{
Running_Menu=""
while [ "$Running_Menu" != "x" ]
do MacAddress="$(ifconfig $WifiInt | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')"
clear; echo; echo
echo "               Access Point"
echo -e "       \e[00;34m============================\e[00m"
echo "               ESSID: $(cat /root/.Acess_Point/ESSID.txt)"
echo "             Channel: $(cat /root/.Acess_Point/Chn.txt)"
if [ ! -z "$(pidof hostapd)" ]; then
 if [ ! -z "$Pass" ]; then echo "            Password: $(cat /root/.Acess_Point/Pass.txt)"; fi
fi
echo "             Network: 10.0.0.X"
echo "         MAC Address: $MacAddress"
echo -e "       \e[00;34m============================\e[00m"
echo "              Status: Up"
echo; echo
echo "      (1) Stop Transmitting"
echo "      (2) Connected Users"
echo "      (3) Restart"
echo
echo "      (X) Exit"
echo
read -p "     > " MainMenuOpts
  if [ "$MainMenuOpts" = "1" ]; then Stop_Ap
 elif [ "$MainMenuOpts" = "2" ]; then Connected_Users_Check
 elif [ "$MainMenuOpts" = "3" ]; then Restart_Ap
 elif [ "$MainMenuOpts" = "x" ]; then exit
  fi
done
}



function Input_Without_Pass_ESSID
{
clear; echo; echo; read -p "        Broadcast Name: " ESSID
if [ ! -z "$ESSID" ]; then echo "$ESSID" > /root/.Acess_Point/ESSID.txt; Input_Without_Pass_Chn
 else Input_Without_Pass_ESSID
fi
}
function Input_Without_Pass_Chn
{
read -p "        Channel Number: " Chn
if [ ! -z "$Chn" ]; then
   if [ $Chn -eq $Chn 2>/dev/null ]; then echo "$Chn" > /root/.Acess_Point/Chn.txt; Without_Password     # Continue
   else clear; echo; echo; echo; echo "            Channel must be a number!"; sleep 2.5s; clear; echo; echo; echo "        Broadcast Name: $ESSID"; Input_Without_Pass_Chn
   fi
else  #Retry - Blank
clear; echo; echo; echo "        Broadcast Name: $ESSID"; Input_Without_Pass_Chn
fi
}
function Without_Password                # Start - No Password
{
clear; echo; echo; echo; echo "          Starting Access Point - 5%"; echo
sudo rfkill unblock wifi
service wicd stop >/dev/null 2>/dev/null
killall wicd-client >/dev/null 2>/dev/null
service network-manager stop >/dev/null 2>/dev/null
killall dhclient >/dev/null 2>/dev/null
killall dhcpd >/dev/null 2>/dev/null
killall wpa_supplicant >/dev/null 2>/dev/null
killall dhcp >/dev/null 2>/dev/null
killall hostapd >/dev/null 2>/dev/null
mv /etc/hostapd/hostapd.conf.backup /etc/hostapd/hostapd.conf 2>/dev/null
mv /etc/hostapd/hostapd.conf /etc/hostapd/hostapd.conf.backup 2>/dev/null
echo "interface=$WifiInt
driver=nl80211
ssid=$ESSID
hw_mode=g
channel=$Chn
wmm_enabled=0" > /etc/hostapd/hostapd.conf
echo "authoritative;
default-lease-time 600;
max-lease-time 7200;
subnet 10.0.0.0 netmask 255.255.255.0 {
option routers 10.0.0.1;
option subnet-mask 255.255.255.0;
option domain-name-servers 10.0.0.1;
range 10.0.0.20 10.0.0.50;
}" > /root/.Acess_Point/dhcpd.conf
clear; echo; echo; echo; echo "          Starting Access Point - 50%"; echo
ifconfig $WifiInt up 10.0.0.1 netmask 255.255.255.0
iptables --flush
iptables --table nat --flush
iptables --delete-chain
iptables --table nat --delete-chain
iptables --table nat --append POSTROUTING --out-interface $LanInt -j MASQUERADE
iptables --append FORWARD --in-interface $WifiInt -j ACCEPT
bash -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'
sleep 2s; clear; echo; echo; echo; echo "          Starting Access Point - 77%"; echo
/etc/init.d/hostapd start >/dev/null 2>/dev/null
sleep 2s; clear; echo; echo; echo; echo "          Starting Access Point - 97%"; echo
route add -net 10.0.0.0 netmask 255.255.255.0 gw 10.0.0.1 $WifiInt
iptables -t nat -A PREROUTING -p udp -j DNAT --to $(route -n|grep ^0.0.0.0|cut -d' ' -f 10)
iptables -P FORWARD ACCEPT
dhcpd -cf /root/.Acess_Point/dhcpd.conf $WifiInt >/dev/null 2>/dev/null
sleep 1s; StatusCheck
}


function Input_With_Pass_ESSID
{
clear; echo; echo; read -p "        Broadcast Name: " ESSID
if [ ! -z "$ESSID" ]; then echo "$ESSID" > /root/.Acess_Point/ESSID.txt; Input_With_Pass_Chn
 else Input_With_Pass_ESSID
fi
}
function Input_With_Pass_Chn
{
read -p "        Channel Number: " Chn
if [ ! -z "$Chn" ]; then
   if [ $Chn -eq $Chn 2>/dev/null ]; then echo "$Chn" > /root/.Acess_Point/Chn.txt; Input_With_Pass_Pass     # Continue
   else clear; echo; echo; echo; echo "            Channel must be a number!"; sleep 2.5s; clear; echo; echo; echo "        Broadcast Name: $ESSID"; Input_With_Pass_Chn
   fi
else  #Retry - Blank
clear; echo; echo; echo "        Broadcast Name: $ESSID"; Input_With_Pass_Chn
fi
}
function Input_With_Pass_Pass
{
read -p "              Password: " Pass
len=${#Pass}
if (( len > 7 )); then echo "$Pass" > /root/.Acess_Point/Pass.txt; With_Password
 else
clear; echo; echo; echo; echo "            Password must be at least 8 characters!"; echo; sleep 2.5s; clear; echo; echo; echo "        Broadcast Name: $ESSID"; echo "        Channel Number: $Chn"; Input_With_Pass_Pass
fi
}
function With_Password                    # Start - With Password
{
clear; echo; echo; echo; echo "          Starting Access Point - 5%"; echo
sudo rfkill unblock wifi
service wicd stop >/dev/null 2>/dev/null
killall wicd-client >/dev/null 2>/dev/null
service network-manager stop >/dev/null 2>/dev/null
killall dhclient >/dev/null 2>/dev/null
killall dhcpd >/dev/null 2>/dev/null
killall wpa_supplicant >/dev/null 2>/dev/null
killall dhcp >/dev/null 2>/dev/null
killall hostapd >/dev/null 2>/dev/null
mv /etc/hostapd/hostapd.conf.backup /etc/hostapd/hostapd.conf 2>/dev/null
mv /etc/hostapd/hostapd.conf /etc/hostapd/hostapd.conf.backup 2>/dev/null
echo "interface=$WifiInt
driver=nl80211
ssid=$ESSID
hw_mode=g
channel=$Chn
wpa=2
wmm_enabled=0
wpa_passphrase=$Pass
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
wpa_ptk_rekey=600" > /etc/hostapd/hostapd.conf
echo "authoritative;
default-lease-time 600;
max-lease-time 7200;
subnet 10.0.0.0 netmask 255.255.255.0 {
option routers 10.0.0.1;
option subnet-mask 255.255.255.0;
option domain-name-servers 10.0.0.1;
range 10.0.0.20 10.0.0.50;
}" > /root/.Acess_Point/dhcpd.conf
clear; echo; echo; echo; echo "          Starting Access Point - 50%"; echo
ifconfig $WifiInt up 10.0.0.1 netmask 255.255.255.0
iptables --flush
iptables --table nat --flush
iptables --delete-chain
iptables --table nat --delete-chain
iptables --table nat --append POSTROUTING --out-interface $LanInt -j MASQUERADE
iptables --append FORWARD --in-interface $WifiInt -j ACCEPT
bash -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'
sleep 2s; clear; echo; echo; echo; echo "          Starting Access Point - 76%"; echo
/etc/init.d/hostapd start >/dev/null 2>/dev/null
sleep 2s; clear; echo; echo; echo; echo "          Starting Access Point - 98%"; echo
route add -net 10.0.0.0 netmask 255.255.255.0 gw 10.0.0.1 $WifiInt
iptables -t nat -A PREROUTING -p udp -j DNAT --to $(route -n|grep ^0.0.0.0|cut -d' ' -f 10)
iptables -P FORWARD ACCEPT
dhcpd -cf /root/.Acess_Point/dhcpd.conf $WifiInt >/dev/null 2>/dev/null
sleep 1s; StatusCheck
}


function Restart_Ap
{
clear; echo; echo
echo "               ${bold}Access Point"
echo -e "       \e[00;34m============================\e[00m${normal}"
echo "               ESSID: $ESSID"
echo "             Channel: $Chn"
if [ ! -z "$(pidof hostapd)" ]; then
 if [ ! -z "$Pass" ]; then echo "            Password: $Pass"; fi
fi
echo "             Network: 10.0.0.X"
echo "         MAC Address: $MacAddress"
echo -e "       \e[00;34m${bold}============================\e[00m${normal}"
clear; echo; echo; echo; echo "          Restarting Access Point..."; echo
sudo rfkill unblock wifi
service wicd stop >/dev/null 2>/dev/null
killall wicd-client >/dev/null 2>/dev/null
killall dhcp >/dev/null 2>/dev/null
killall dhcpd >/dev/null 2>/dev/null
killall hostapd >/dev/null 2>/dev/null
iptables --flush >/dev/null 2>/dev/null
iptables --table nat --flush >/dev/null 2>/dev/null
iptables --delete-chain >/dev/null 2>/dev/null
iptables --table nat --delete-chain >/dev/null 2>/dev/null
rm -fr /root/.Acess_Point/Connected_Users.txt >/dev/null 2>/dev/null
rm -fr /root/.Acess_Point/Connected_Users2.txt >/dev/null 2>/dev/null
Chn="$(cat /root/.Acess_Point/Chn.txt)"        
ESSID="$(cat /root/.Acess_Point/ESSID.txt)"
MacAddress="$(ifconfig $WifiInt | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')"
Pass="$(cat /root/.Acess_Point/Pass.txt)"
if [ "$(cat /root/.Acess_Point/ApType.txt)" = "NoPass" ]; then Without_Password
else With_Password; fi
}
function Stop_Ap
{
clear; killall dhcp >/dev/null 2>/dev/null
killall dhcpd >/dev/null 2>/dev/null
killall hostapd >/dev/null 2>/dev/null
mv /etc/hostapd/hostapd.conf.backup /etc/hostapd/hostapd.conf >/dev/null 2>/dev/null
rm -fr /root/.Acess_Point/dhcpd.conf >/dev/null 2>/dev/null
rm -fr /root/.Acess_Point/1.txt >/dev/null 2>/dev/null
rm -fr /root/.Acess_Point/ApType.txt >/dev/null 2>/dev/null
rm -fr /root/.Acess_Point/ESSID.txt >/dev/null 2>/dev/null
rm -fr /root/.Acess_Point/Chn.txt >/dev/null 2>/dev/null
rm -fr /root/.Acess_Point/Pass.txt >/dev/null 2>/dev/null
rm -fr /root/.Acess_Point/AvailableConnectionsList.txt >/dev/null 2>/dev/null
rm -fr /root/.Acess_Point/Connected_Users.txt >/dev/null 2>/dev/null
rm -fr /root/.Acess_Point/Connected_Users2.txt >/dev/null 2>/dev/null
iptables --flush >/dev/null 2>/dev/null
iptables --table nat --flush >/dev/null 2>/dev/null
iptables --delete-chain >/dev/null 2>/dev/null
iptables --table nat --delete-chain >/dev/null 2>/dev/null
bash -c 'echo 0 > /proc/sys/net/ipv4/ip_forward'
service wicd start >/dev/null 2>/dev/null
service network-manager stop >/dev/null 2>/dev/null
service network-manager start >/dev/null 2>/dev/null
ifconfig $WifiInt down
ifconfig $LanInt down
clear; echo; echo; echo; echo "          Stopping Access Point - 74%"; echo; sleep 1s

# Disable Wifi or not - Rfkill option
if [[ "$rfkill" == "yes" ]] || [[ "rfkil" == "Yes" ]]; then ifconfig $WifiInt down; rfkill block wifi
else rfkill unblock wifi; ifconfig $WifiInt up 0.0.0.0
dhclient -r $WifiInt >/dev/null 2>/dev/null
fi

ifconfig $LanInt up
dhclient -r $LanInt >/dev/null 2>/dev/null
clear; echo; echo; echo; echo "          Stopping Access Point - 87%"; echo; sleep 1s

# Disable Wifi or not - Rfkill option (Part 2)
if [[ "$rfkill" == "yes" ]] || [[ "rfkil" == "Yes" ]]; then rfkill block wifi
else
dhclient -nw $WifiInt >/dev/null 2>/dev/null
fi

dhclient -nw $LanInt >/dev/null 2>/dev/null
StatusCheck
}


################################  USER LIST  #####################################

#Get list & check if list is blank --> Show list/Try again
function Connected_Users_Check
{
rm -fr /root/.Acess_Point/Connected_Users.txt >/dev/null 2>/dev/null
rm -fr /root/.Acess_Point/Connected_Users2.txt >/dev/null 2>/dev/null
clear; echo; echo; echo "            Getting Client List..."; echo
echo "$(arp-scan --interface=$WifiInt --retry=4 -l | grep -Fv Ending | grep -Fv received | grep -Fv type: | grep -Fv arp-scan)" > /root/.Acess_Point/Connected_Users.txt

#If no users - Show message, else show list
if [ ! -z "$(cat /root/.Acess_Point/Connected_Users.txt)" ]; then clear
else sleep 2s; Connected_Users_Check_Two; fi

#Indent lines
sed  's/^/  /'  /root/.Acess_Point/Connected_Users.txt > /root/.Acess_Point/Connected_Users2.txt
Connected_Users_Menu
}

#Try again if list is empty (Better Results)
function Connected_Users_Check_Two
{
rm -fr /root/.Acess_Point/Connected_Users.txt >/dev/null 2>/dev/null
rm -fr /root/.Acess_Point/Connected_Users2.txt >/dev/null 2>/dev/null
clear; echo; echo; echo "            Getting Client List..."; echo
echo "$(arp-scan --interface=$WifiInt -l | grep -Fv Ending | grep -Fv received | grep -Fv type: | grep -Fv arp-scan)" > /root/.Acess_Point/Connected_Users.txt
if [ ! -z "$(cat /root/.Acess_Point/Connected_Users.txt)" ]; then clear
else clear; echo; echo; echo; echo "          No Users Connected"; echo; echo; sleep 2.2s; Running_Menu; fi
sed  's/^/  /'  /root/.Acess_Point/Connected_Users.txt > /root/.Acess_Point/Connected_Users2.txt
Connected_Users_Menu
}


#Show User List / Give options
function Connected_Users_Menu
{
Connected_Users_Menu=""
while [ "$Connected_Users_Menu" != "x" ]
do clear; echo; echo; cat /root/.Acess_Point/Connected_Users2.txt    #Display Connected Users List
echo -e "       \e[00;32m================================================\e[00m"
echo
echo "                  (1) Refresh"
echo
echo "                  (X) Back"
echo
read -p "                 > " UserListOpts
if [ "$UserListOpts" = "1" ]; then Connected_Users_Check      #Refresh
 elif [ "$UserListOpts" = "x" ]; then StatusCheck              #Back to running menu (Check Status First)
fi
done
}


Get_Interfaces


To add these to the OpenBox menu

terminator --command="sudo bash /Path/To/File/Access_Point.sh"

No hay comentarios:

Publicar un comentario