Created
December 22, 2020 18:30
-
-
Save c4mx/1cad677b469939f9191b25d043222903 to your computer and use it in GitHub Desktop.
Set WiFi Hotspot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# by c4mx | |
set_wifi_hotspot () { | |
if [ "$#" -ne 2 ]; then | |
echo "Usage: $0 [start|stop] interface" | |
return 1 | |
fi | |
action=$1 | |
iface=$2 | |
ip link show "$iface" | |
if [ "$?" -ne 0 ]; then | |
echo "[-] Network interface \"$iface\" not found" | |
return 1 | |
fi | |
case $action in | |
"start") | |
echo "[+] Cleaning env..." | |
sudo airmon-ng check kill | |
echo "===================" | |
echo "[+] Configuring IP address for $iface..." | |
#sudo ifconfig $iface 192.168.150.1 || echo "[-] Cannot configure IP address"; return 1 | |
ip a show "$iface" | grep "192.168.150.1" | |
if [ "$?" -ne 0 ]; then | |
sudo ip addr flush dev "$iface" | |
sudo ip addr add 192.168.150.1/24 dev "$iface" | |
if [ "$?" -ne 0 ]; then | |
echo "[-] Cannot configure IP address" | |
return 1 | |
fi | |
fi | |
ip a show $iface | |
echo "===================" | |
echo "[+] Starting dnsmasq server..." | |
sudo service dnsmasq restart | |
if [ "$?" -ne 0 ]; then | |
echo "[-] dnsmasq error"; | |
return 1 | |
fi | |
echo "===================" | |
echo "[+] Enabling kernel ip forwarding..." | |
sudo sysctl net.ipv4.ip_forward=1 | |
echo "===================" | |
echo "[+] Setting iptables NAT and forwarding..." | |
sudo iptables -F | |
sudo iptables -F -t nat | |
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE | |
sudo iptables -A FORWARD -i "$iface" -o eth0 -j ACCEPT | |
sudo iptables -L -t nat -v | |
sudo iptables -L -v | |
echo "===================" | |
echo "[+] Running hostapd daemon in the background..." | |
sudo killall hostapd 2>/dev/null | |
#sudo hostapd -d /etc/hostapd.conf | |
sleep 3 | |
sudo hostapd -B /etc/hostapd/hostapd.conf | |
if [ "$?" -ne 0 ]; then | |
echo "[-] hostapd error"; | |
return 1 | |
fi | |
echo "===================" | |
;; | |
"stop") | |
echo "[+] Stopping dnsmasq server..." | |
sudo service dnsmasq stop | |
echo "[+] Disabling kernel ip forwarding..." | |
sudo sysctl net.ipv4.ip_forward=0 | |
echo "[+] Flushing iptables..." | |
sudo iptables -F | |
sudo iptables -F -t nat | |
echo "[+] Killing hostapd daemon..." | |
sudo killall hostapd 2>/dev/null | |
;; | |
esac | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment