Skip to content

Instantly share code, notes, and snippets.

@oldsharp
Last active July 23, 2022 03:10
Show Gist options
  • Save oldsharp/34600a524b0dd9ff0973 to your computer and use it in GitHub Desktop.
Save oldsharp/34600a524b0dd9ff0973 to your computer and use it in GitHub Desktop.
rtl8188eu access point control script
#!/bin/sh
# Script to start/stop a hostapd-based access point
#
# Sample start call "rtl8188eu-ap.sh start wlan0 eth0"
# Stop with "rtl8188eu-ap.sh stop"
#
# Get driver for rtl8188eu: https://github.com/lwfinger/rtl8188eu
case "$1" in
start)
if [ $# -ne 3 ]
then
echo "Usage: $0 start ap_iface net_iface"
exit 1
fi
;;
stop)
if [ $# -ne 1 ]
then
echo "Usage: $0 stop"
exit 1
fi
;;
*)
echo "Usage:"
echo "$0 start ap-iface net_iface"
echo "or"
echo "$0 stop"
exit 1
;;
esac
# Symbols for needed programs
IPTABLES=/sbin/iptables
IFCONFIG=/sbin/ifconfig
# apt-get install isc-dhcp-server
DHCPD=/usr/sbin/dhcpd
# Need a dedicated version of hostapd with Realtek's special driver.
# This is included in https://dn-gutspot.qbox.me/8188eu_USB_linux.zip
# Check also https://github.com/pritambaral/hostapd-rtl871xdrv for new
# version of hostapd (hostapd v2.5 tested under Ubuntu 14.04 LTS).
HOSTAPD=/home/ray/ap/hostapd
# Configure files
HOSTAPDCONF=/tmp/rtl_hostapd.conf
DHCPDCONF=/etc/dhcp/dhcpd-alt.conf
# Symbols for AP and external interfaces
NET_AP=$2
NET_EXT=$3
# First 3 octets of IP address for the AP
AP_ADDR=192.168.137
# IP address for nameserver
NAME_SERVER=223.5.5.5
# AP Channel, SSID, encryption method, driver and encryption secret
AP_CHANNEL=3
AP_SSID=octocat
WPA_SECRET=PASSWORD
ENCRYPT_MODE=2
MAX_NUM_STA=8
DRIVER=rtl871xdrv
case "$1" in
start)
echo "Starting AP mode for $NET_AP at address ${AP_ADDR}.1"
# Disable packet forwarding
echo 0 > /proc/sys/net/ipv4/ip_forward
# Stop any existing hostapd and dhcpd daemons
killall -q hostapd
killall -q dhcpd
# Set up forwarding
$IPTABLES -t nat -A POSTROUTING -o $NET_EXT -j MASQUERADE
$IPTABLES -A FORWARD -i $NET_EXT -o $NET_AP -m state \
--state RELATED,ESTABLISHED -j ACCEPT
$IPTABLES -A FORWARD -i $NET_AP -o $NET_EXT -j ACCEPT
# Get the AP interface in the right state
$IFCONFIG $NET_AP down
$IFCONFIG $NET_AP up
$IFCONFIG $NET_AP ${AP_ADDR}.1
# dhcpd needs to have a leases file available - create it if needed
if [ ! -f /var/lib/dhcp/db/dhcpd.leases ]; then
mkdir -p /var/lib/dhcp/db
touch /var/lib/dhcp/db/dhcpd.leases
fi
# Write the DHCP server configuration file
cat > $DHCPDCONF << EOF
option domain-name-servers $NAME_SERVER;
default-lease-time 600;
max-lease-time 7200;
ddns-update-style none; ddns-updates off;
subnet ${AP_ADDR}.0 netmask 255.255.255.0 {
range ${AP_ADDR}.100 ${AP_ADDR}.200;
option subnet-mask 255.255.255.0;
option broadcast-address ${AP_ADDR}.255;
option routers ${AP_ADDR}.1;
}
EOF
# Bring up the DHCP server
$DHCPD -cf $DHCPDCONF $NET_AP
# Write the hostapd configuration file
cat > $HOSTAPDCONF << EOF
##### hostapd configuration file ##############################################
interface=$NET_AP
ctrl_interface=/var/run/hostapd
ssid=$AP_SSID
channel=$AP_CHANNEL
wpa=$ENCRYPT_MODE
wpa_passphrase=$WPA_SECRET
##### Wi-Fi Protected Setup (WPS) #############################################
eap_server=1
# WPS state
# 0 = WPS disabled (default)
# 1 = WPS enabled, not configured
# 2 = WPS enabled, configured
wps_state=2
uuid=12345678-9abc-def0-1234-56789abcdef0
# Device Name
# User-friendly description of device; up to 32 octets encoded in UTF-8
device_name=RTL8188EU
# Manufacturer
# The manufacturer of the device (up to 64 ASCII characters)
manufacturer=Realtek
# Model Name
# Model of the device (up to 32 ASCII characters)
model_name=RTW_SOFTAP
# Model Number
# Additional device description (up to 32 ASCII characters)
model_number=WLAN_CU
# Serial Number
# Serial number of the device (up to 32 characters)
serial_number=12345
# Primary Device Type
# Used format: <categ>-<OUI>-<subcateg>
# categ = Category as an integer value
# OUI = OUI and type octet as a 4-octet hex-encoded value; 0050F204 for
# default WPS OUI
# subcateg = OUI-specific Sub Category as an integer value
# Examples:
# 1-0050F204-1 (Computer / PC)
# 1-0050F204-2 (Computer / Server)
# 5-0050F204-1 (Storage / NAS)
# 6-0050F204-1 (Network Infrastructure / AP)
device_type=6-0050F204-1
# OS Version
# 4-octet operating system version number (hex string)
os_version=01020300
# Config Methods
# List of the supported configuration methods
config_methods=label display push_button keypad
##### default configuration #######################################
driver=$DRIVER
beacon_int=100
hw_mode=g
ieee80211n=1
wme_enabled=1
ht_capab=[SHORT-GI-20][SHORT-GI-40]
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
max_num_sta=$MAX_NUM_STA
wpa_group_rekey=86400
EOF
# Enable packet forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
# Bring up hostapd
$HOSTAPD -dd $HOSTAPDCONF
;;
stop)
echo "Stopping AP mode"
# Stop hostapd and dhcpd daemons
killall hostapd
killall dhcpd
rm -f $HOSTAPDCONF
rm -f $DHCPDCONF
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment