-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhotspot.sh
More file actions
155 lines (133 loc) · 4.09 KB
/
Copy pathhotspot.sh
File metadata and controls
155 lines (133 loc) · 4.09 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/bash
# DeskClock hotspot manager — runs as root via sudo
# Usage: hotspot.sh start <iface> <ssid> <password>
# hotspot.sh stop <iface>
# hotspot.sh status <iface>
# hotspot.sh clients <iface>
# hotspot.sh check
# hotspot.sh list
AP_IP="192.168.4.1"
HOSTAPD_CONF="/tmp/deskclock_hostapd.conf"
DNSMASQ_CONF="/tmp/deskclock_dnsmasq.conf"
_pid_file() { echo "/tmp/deskclock_hostapd_${1}.pid"; }
_dpid_file() { echo "/tmp/deskclock_dnsmasq_${1}.pid"; }
case "$1" in
start)
IFACE="$2"; SSID="$3"; PASS="$4"
if [ -z "$IFACE" ] || [ -z "$SSID" ] || [ -z "$PASS" ]; then
echo "ERROR: usage: start <iface> <ssid> <password>" >&2; exit 1
fi
HOSTAPD_PID=$(_pid_file "$IFACE")
DNSMASQ_PID=$(_dpid_file "$IFACE")
# Write hostapd config
cat > "$HOSTAPD_CONF" <<HEOF
interface=$IFACE
driver=nl80211
ssid=$SSID
hw_mode=g
channel=6
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=$PASS
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP
HEOF
# Write dnsmasq config
cat > "$DNSMASQ_CONF" <<DEOF
interface=$IFACE
bind-interfaces
dhcp-range=192.168.4.2,192.168.4.20,12h
dhcp-option=3,$AP_IP
dhcp-option=6,8.8.8.8,8.8.4.4
DEOF
# If using primary interface, stop wpa_supplicant client on it
if [ "$IFACE" = "wlan0" ]; then
wpa_cli -i "$IFACE" terminate 2>/dev/null || true
sleep 1
fi
# Configure static IP
ip addr flush dev "$IFACE" 2>/dev/null || true
ip addr add "${AP_IP}/24" dev "$IFACE"
ip link set "$IFACE" up
# Start hostapd
hostapd -B -P "$HOSTAPD_PID" "$HOSTAPD_CONF" 2>/tmp/hostapd_err.txt
if [ $? -ne 0 ]; then
echo "ERROR: hostapd failed — $(cat /tmp/hostapd_err.txt)" >&2; exit 1
fi
# Start dnsmasq (kill stale instance first)
pkill -f "dnsmasq.*deskclock" 2>/dev/null || true
sleep 0.3
dnsmasq --conf-file="$DNSMASQ_CONF" --pid-file="$DNSMASQ_PID"
echo "OK $AP_IP"
;;
stop)
IFACE="$2"
if [ -z "$IFACE" ]; then echo "ERROR: iface required" >&2; exit 1; fi
HOSTAPD_PID=$(_pid_file "$IFACE")
DNSMASQ_PID=$(_dpid_file "$IFACE")
# Kill hostapd
if [ -f "$HOSTAPD_PID" ]; then
kill "$(cat "$HOSTAPD_PID")" 2>/dev/null || true
rm -f "$HOSTAPD_PID"
fi
pkill -f "hostapd.*deskclock" 2>/dev/null || true
# Kill dnsmasq
if [ -f "$DNSMASQ_PID" ]; then
kill "$(cat "$DNSMASQ_PID")" 2>/dev/null || true
rm -f "$DNSMASQ_PID"
fi
pkill -f "dnsmasq.*deskclock" 2>/dev/null || true
sleep 0.5
ip addr flush dev "$IFACE" 2>/dev/null || true
# Resume WiFi client only if we took over the primary interface
if [ "$IFACE" = "wlan0" ]; then
systemctl restart wpa_supplicant 2>/dev/null || true
sleep 2
wpa_cli -i "$IFACE" reconfigure 2>/dev/null || true
fi
echo "OK stopped"
;;
status)
IFACE="$2"
if [ -z "$IFACE" ]; then echo "inactive"; exit 0; fi
HOSTAPD_PID=$(_pid_file "$IFACE")
if [ -f "$HOSTAPD_PID" ] && kill -0 "$(cat "$HOSTAPD_PID")" 2>/dev/null; then
echo "active $AP_IP"
else
echo "inactive"
fi
;;
clients)
IFACE="$2"
if [ -z "$IFACE" ]; then exit 0; fi
iw dev "$IFACE" station dump 2>/dev/null \
| awk '/^Station/{mac=$2} /signal:/{sig=$2} mac && sig{printf "%s %s dBm\n",mac,sig; mac=""; sig=""}'
;;
check)
MISSING=""
command -v hostapd >/dev/null 2>&1 || MISSING="$MISSING hostapd"
command -v dnsmasq >/dev/null 2>&1 || MISSING="$MISSING dnsmasq"
if [ -n "$MISSING" ]; then echo "missing:$MISSING"; exit 1; fi
echo "ok"
;;
list)
# Print available wlan interfaces and whether they are USB-attached
for sysdir in /sys/class/net/wlan*; do
[ -e "$sysdir" ] || continue
iface=$(basename "$sysdir")
subsys=$(readlink -f "$sysdir/device/subsystem" 2>/dev/null || echo "")
if echo "$subsys" | grep -qi "usb"; then
echo "$iface usb"
else
echo "$iface builtin"
fi
done
;;
*)
echo "Usage: $0 start <iface> <ssid> <pass> | stop <iface> | status <iface> | clients <iface> | check | list"
exit 1
;;
esac