Skip to content

Instantly share code, notes, and snippets.

@KevinRohn
Last active December 19, 2023 10:30
Show Gist options
  • Save KevinRohn/363d74ffdbe38d516bc7f044dcf92456 to your computer and use it in GitHub Desktop.
Save KevinRohn/363d74ffdbe38d516bc7f044dcf92456 to your computer and use it in GitHub Desktop.
setup ap
#!/bin/bash
# Check if running as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
ITF_NAME=$1
# Check for network interface argument
if [ -z "$ITF_NAME" ]; then
echo "No interface name provided. Usage: $0 [interface_name]"
exit 1
fi
# Check if network interface exists
if ! ip link show $ITF_NAME > /dev/null 2>&1; then
echo "Network interface $ITF_NAME does not exist."
exit 1
fi
readonly HOSTAPD_CONF_PATH="/etc/hostapd/hostapd.conf"
readonly DNSMASQ_CONF_PATH="/etc/dnsmasq.conf"
# Setup install directory
if [ ! -d "download_tmp" ]; then
mkdir download_tmp
fi
cd download_tmp
# Function to check if package is installed
is_installed() {
dpkg -l "$1" &> /dev/null
}
declare -A packages=(
["libnl-genl-3-200_3.4.0-1+b1_armhf.deb"]="http://ftp.de.debian.org/debian/pool/main/libn/libnl3/libnl-genl-3-200_3.4.0-1+b1_armhf.deb"
["iw_5.9-3_armhf.deb"]="http://ftp.debian.org/debian/pool/main/i/iw/iw_5.9-3_armhf.deb"
["libnl-route-3-200_3.4.0-1+b1_armhf.deb"]="http://ftp.debian.org/debian/pool/main/libn/libnl3/libnl-route-3-200_3.4.0-1+b1_armhf.deb"
["hostapd_2.9.0-21_armhf.deb"]="http://ftp.debian.org/debian/pool/main/w/wpa/hostapd_2.9.0-21_armhf.deb"
["libnl-3-200_3.4.0-1+b1_armhf.deb"]="http://ftp.debian.org/debian/pool/main/libn/libnl3/libnl-3-200_3.4.0-1+b1_armhf.deb"
["dnsmasq-base_2.85-1_armhf.deb"]="http://ftp.de.debian.org/debian/pool/main/d/dnsmasq/dnsmasq-base_2.85-1_armhf.deb"
["dnsmasq_2.85-1_all.deb"]="http://ftp.de.debian.org/debian/pool/main/d/dnsmasq/dnsmasq_2.85-1_all.deb"
)
for package in "${!packages[@]}"; do
if ! is_installed $package; then
echo "Downloading $package"
wget "${packages[$package]}"
else
echo "$package is already installed."
fi
done
# Install packages
echo "Installing packages..."
dpkg -i *.deb || { echo "Failed to install packages"; exit 1; }
# prepare hostapd service
systemctl unmask hostapd.service
systemctl start hostapd.service
# Set IP address
ip a a 10.10.10.10/29 dev $ITF_NAME
# configure hostapd config
echo -e "interface=${ITF_NAME}\nssid=access-point\nhw_mode=g\nchannel=1\nown_ip_addr=10.10.10.10" | sudo tee $HOSTAPD_CONF_PATH
# configure dnsmasq config
echo -e "port=0\ninterface=${ITF_NAME}\ndhcp-range=10.10.10.11,10.10.10.14,10m\ndhcp-option=3\ndhcp-option=6\n#dhcp-option=121,10.10.10.8/29,10.10.10.10\nlog-facility=/var/log/dnsmasq.log\nlog-async\nlog-queries\nlog-dhcp" | sudo tee $DNSMASQ_CONF_PATH
# Clean up
cd ..
rm -rf download_tmp
echo "Setup complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment