-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathwifi_manager_service.sh
More file actions
executable file
·362 lines (298 loc) · 9.34 KB
/
wifi_manager_service.sh
File metadata and controls
executable file
·362 lines (298 loc) · 9.34 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/bin/bash
# wifi_manager_service.sh
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
HOSTAPD_CONFIG="/tmp/ragnar/hostapd.conf"
DNSMASQ_CONFIG="/tmp/ragnar/dnsmasq.conf"
INTERFACE="wlan0"
AP_IP="192.168.4.1"
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
# Check if running as root
check_root() {
if [[ $EUID -ne 0 ]]; then
print_error "This script must be run as root (use sudo)"
exit 1
fi
}
# Start Access Point mode
start_ap() {
print_status "Starting Wi-Fi Access Point..."
# Check if interface exists
if ! ip link show "$INTERFACE" &>/dev/null; then
print_error "Interface $INTERFACE not found"
return 1
fi
# Stop NetworkManager management of the interface
nmcli dev set "$INTERFACE" managed no 2>/dev/null || true
# Configure interface
print_status "Configuring interface $INTERFACE..."
ip addr flush dev "$INTERFACE"
ip addr add "${AP_IP}/24" dev "$INTERFACE"
ip link set dev "$INTERFACE" up
if [ $? -eq 0 ]; then
print_success "Interface configured"
else
print_error "Failed to configure interface"
return 1
fi
# Start hostapd if config exists
if [ -f "$HOSTAPD_CONFIG" ]; then
print_status "Starting hostapd..."
hostapd "$HOSTAPD_CONFIG" -B
if [ $? -eq 0 ]; then
print_success "hostapd started"
else
print_error "Failed to start hostapd"
cleanup_ap
return 1
fi
else
print_warning "hostapd config not found at $HOSTAPD_CONFIG"
fi
# Start dnsmasq if config exists
if [ -f "$DNSMASQ_CONFIG" ]; then
print_status "Starting dnsmasq..."
dnsmasq -C "$DNSMASQ_CONFIG"
if [ $? -eq 0 ]; then
print_success "dnsmasq started"
else
print_error "Failed to start dnsmasq"
cleanup_ap
return 1
fi
else
print_warning "dnsmasq config not found at $DNSMASQ_CONFIG"
fi
# Set up NAT rules
setup_nat
print_success "Wi-Fi Access Point started successfully"
print_status "AP Details:"
print_status " Interface: $INTERFACE"
print_status " IP Address: $AP_IP"
print_status " DHCP Range: 192.168.4.2 - 192.168.4.20"
}
# Stop Access Point mode
stop_ap() {
print_status "Stopping Wi-Fi Access Point..."
# Stop services
pkill hostapd 2>/dev/null || true
pkill dnsmasq 2>/dev/null || true
# Clean up interface
cleanup_ap
print_success "Wi-Fi Access Point stopped"
}
# Cleanup interface configuration
cleanup_ap() {
print_status "Cleaning up interface configuration..."
# Flush IP address
ip addr flush dev "$INTERFACE" 2>/dev/null || true
# Return interface to NetworkManager
nmcli dev set "$INTERFACE" managed yes 2>/dev/null || true
# Clear NAT rules
cleanup_nat
print_success "Interface cleanup completed"
}
# Set up NAT and forwarding rules
setup_nat() {
print_status "Setting up NAT rules..."
# Enable IP forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
# Set up iptables rules for NAT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE 2>/dev/null || \
iptables -t nat -A POSTROUTING -o wlan1 -j MASQUERADE 2>/dev/null || \
iptables -t nat -A POSTROUTING -s 192.168.4.0/24 ! -d 192.168.4.0/24 -j MASQUERADE
iptables -A FORWARD -i "$INTERFACE" -o eth0 -j ACCEPT 2>/dev/null || \
iptables -A FORWARD -i "$INTERFACE" -o wlan1 -j ACCEPT 2>/dev/null || true
iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
print_success "NAT rules configured"
}
# Clean up NAT rules
cleanup_nat() {
print_status "Cleaning up NAT rules..."
# Remove specific rules (this is a simplified cleanup)
iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE 2>/dev/null || true
iptables -t nat -D POSTROUTING -o wlan1 -j MASQUERADE 2>/dev/null || true
iptables -t nat -D POSTROUTING -s 192.168.4.0/24 ! -d 192.168.4.0/24 -j MASQUERADE 2>/dev/null || true
print_success "NAT rules cleaned up"
}
# Check Wi-Fi connection status
check_connection() {
print_status "Checking Wi-Fi connection status..."
# Check if connected to a network
if nmcli -t -f ACTIVE,SSID dev wifi | grep -q '^yes:'; then
CURRENT_SSID=$(nmcli -t -f ACTIVE,SSID dev wifi | grep '^yes:' | cut -d: -f2)
print_success "Connected to: $CURRENT_SSID"
# Test internet connectivity
if ping -c1 8.8.8.8 &>/dev/null; then
print_success "Internet connectivity: OK"
return 0
else
print_warning "Internet connectivity: FAILED"
return 1
fi
else
print_warning "Not connected to any Wi-Fi network"
return 1
fi
}
# Scan for available networks
scan_networks() {
print_status "Scanning for Wi-Fi networks..."
# Trigger a fresh scan
nmcli dev wifi rescan
sleep 3
# List available networks
print_status "Available networks:"
nmcli -t -f SSID,SIGNAL,SECURITY dev wifi | while IFS=: read -r ssid signal security; do
if [ -n "$ssid" ]; then
printf " %-30s Signal: %3s%% Security: %s\n" "$ssid" "$signal" "$security"
fi
done
}
# Connect to a network
connect_network() {
local ssid="$1"
local password="$2"
if [ -z "$ssid" ]; then
print_error "SSID is required"
echo "Usage: $0 connect <SSID> [password]"
return 1
fi
print_status "Connecting to network: $ssid"
if [ -n "$password" ]; then
nmcli dev wifi connect "$ssid" password "$password"
else
nmcli dev wifi connect "$ssid"
fi
if [ $? -eq 0 ]; then
print_success "Successfully connected to $ssid"
# Wait a moment and check connection
sleep 3
check_connection
else
print_error "Failed to connect to $ssid"
return 1
fi
}
# Disconnect from current network
disconnect_network() {
print_status "Disconnecting from current network..."
# Get current connection
CONNECTION=$(nmcli -t -f NAME,TYPE con show --active | grep ':802-11-wireless' | cut -d: -f1)
if [ -n "$CONNECTION" ]; then
nmcli con down "$CONNECTION"
print_success "Disconnected from $CONNECTION"
else
print_warning "No active Wi-Fi connection found"
fi
}
# Restart networking services
restart_networking() {
print_status "Restarting networking services..."
# Stop any running AP mode
stop_ap
# Restart NetworkManager
systemctl restart NetworkManager
# Wait for service to start
sleep 5
print_success "Networking services restarted"
}
# Show service status
show_status() {
print_status "Wi-Fi Service Status:"
echo
print_status "Interface Status:"
ip addr show "$INTERFACE" 2>/dev/null || print_warning "Interface $INTERFACE not found"
echo
print_status "Network Connections:"
nmcli con show --active
echo
print_status "Running Processes:"
pgrep -l hostapd || print_status "hostapd: not running"
pgrep -l dnsmasq || print_status "dnsmasq: not running"
echo
print_status "NetworkManager Status:"
systemctl status NetworkManager --no-pager -l | head -10
}
# Show help
show_help() {
echo "ragnar Wi-Fi Manager Service Script"
echo "Usage: $0 <command> [options]"
echo
echo "Commands:"
echo " start-ap Start Access Point mode"
echo " stop-ap Stop Access Point mode"
echo " check Check Wi-Fi connection status"
echo " scan Scan for available networks"
echo " connect <SSID> [password] Connect to a network"
echo " disconnect Disconnect from current network"
echo " restart Restart networking services"
echo " status Show service status"
echo " help Show this help message"
echo
echo "Examples:"
echo " $0 start-ap # Start AP mode"
echo " $0 scan # Scan for networks"
echo " $0 connect MyWiFi password123 # Connect with password"
echo " $0 connect OpenWiFi # Connect to open network"
}
# Main script logic
main() {
local command="$1"
shift
case "$command" in
start-ap)
check_root
start_ap
;;
stop-ap)
check_root
stop_ap
;;
check)
check_connection
;;
scan)
scan_networks
;;
connect)
connect_network "$1" "$2"
;;
disconnect)
disconnect_network
;;
restart)
check_root
restart_networking
;;
status)
show_status
;;
help|--help|-h)
show_help
;;
*)
print_error "Unknown command: $command"
show_help
exit 1
;;
esac
}
# Run main function with all arguments
main "$@"