christianix.de

-> Main -> Access Point Tutorial -> accesspoint.sh

#!/bin/bash

# LAN static
waniface=eth0

## WLAN static
wlaniface=wlan0
wlanaddr="192.168.1.1/24"

start() {
	HOSTAPD_PID=$(pidof hostapd)

	if [ ! -f hostapd.conf ]; then
		echo "ERROR: Config file for hostapd not found"
		return 1
	fi

	# Setup interfaces
	/usr/sbin/rfkill unblock wlan
	if [ $HOSTAPD_PID ]; then
		/bin/kill -TERM $HOSTAPD_PID
	fi
	/usr/sbin/hostapd -B -P /var/run/hostapd.pid hostapd.conf >/dev/null
	/sbin/ip address add $wlanaddr brd + dev $wlaniface

	# Prepare routing
	/sbin/iptables -t nat -A POSTROUTING -o $waniface -j MASQUERADE
	/sbin/sysctl -q -w net.ipv4.ip_forward=1

	# Start dnsmasq
	/usr/sbin/dnsmasq --interface=$wlaniface --dhcp-range=192.168.1.100,192.168.1.200

	return 0
}

stop() {
	# Stop dnsmasq
	DNSMASQ_PID=$(pidof dnsmasq)
	if [ $DNSMASQ_PID ]; then
		kill -TERM $DNSMASQ_PID
	fi

	# Disable routing
	/sbin/sysctl -q -w net.ipv4.ip_forward=0
	/sbin/iptables -t nat -F

	# Deconfigure interfaces
	HOSTAPD_PID=$(pidof hostapd)
	/sbin/ip address del $wlanaddr dev $wlaniface
	if [ $HOSTAPD_PID ]; then
		/bin/kill -TERM $HOSTAPD_PID
	fi

	/usr/sbin/rfkill block wlan
	return 0
}

usage() {
	echo "Usages: $0 {start, restart, stop}"
	echo "        $0 -h"
	return 0
}

COMMAND=${1:-"-h"}
case "$COMMAND" in
	"-h")
		usage
		exit 0
		;;

	"start")
		echo -n "Starting WLAN access point..."
		start
		if [ $? = "0" ]; then
			echo "done."
		else
			echo "failed."
		fi
		;;
	"stop")
		echo -n "Stopping WLAN access point..."
		stop
		if [ $? = "0" ]; then
			echo "done."
		else
			echo "failed."
		fi
		;;
	"restart")
		echo -n "Stopping WLAN access point..."
		stop
		if [ $? = "0" ]; then
			echo "done."
			echo -n "Starting WLAN access point..."
			start
			if [ $? = "0" ]; then
				echo "done."
			else
				echo "failed."
			fi
		else
			echo "failed."
		fi
		;;
	*)
		echo "ERROR: Unknown parameter $COMMAND"
		usage
		;;
esac

exit 0

Verantwortlich/Responsible: Martin Christian (martin at christianix dot de)