summaryrefslogtreecommitdiff
path: root/multitech/recipes/ocg-scripts/ocg-scripts-1.0/ocg-wifi-ap
blob: d96d5973f967e3ede25c000371bd9e09f66994d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash

dhcpd_file=/etc/udhcpd.conf
hostapd_file=/etc/hostapd.conf
interface=wlan0

do_start() {
	if [ "$1" = "bridge" ]; then
		interface=br0
		if ! grep -E -q "^bridge=$interface\s*$" $hostapd_file; then
			echo "Enabling bridge=br0 in $hostapd_file"
			sed -r -i "s/^#?bridge=.*$/bridge=$interface/" $hostapd_file 
		fi
	fi

	echo "starting hostap daemon"
	/etc/init.d/hostapd start

	if [ "$1" != "bridge" ]; then
		echo "setting IP address of access point"
		ifconfig $interface $1
	fi

	if ! grep -E -q "^interface\s+$interface" $dhcpd_file; then
		echo "Changing dhcpd interface to $interface"
		sed -r -i "s/^interface\s+.*$/interface $interface/" $dhcpd_file 
	fi
	ocg-dhcpd start
}

do_stop() {
	echo "stopping hostap daemon"
	/etc/init.d/hostapd stop
	ocg-dhcpd stop
}

# main
if [[ $# -lt 1 ]]
then
	echo "usage: $0 start|stop|restart [access point IP address|\"bridge\"]"
	exit 1
elif [[ $# -eq 2 ]]
then
	if [ "$2" != "bridge" ]; then
		ret=1
		if [[ $2 =~ ^([0-9]{1,3}\.){3,3}[0-9]{1,3}$ ]]
		then
			OIFS=$IFS
			IFS='.'
			ip=($2)
			IFS=$OFIS
			[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
			ret=$?
		fi
		if [[ ret -ne 0 ]]
		then
			echo "invalid IP address"
			exit 1
		fi
	fi
	ip=$2
else
	ip="192.168.2.1"
fi

case $1 in
	start)
		do_start $ip
		;;
	stop)
		do_stop
		;;
	restart)
		do_stop
		sleep 1
		do_start $ip
		;;
esac

exit 0