-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_hotspot.sh
More file actions
154 lines (127 loc) · 3.81 KB
/
Copy pathsetup_hotspot.sh
File metadata and controls
154 lines (127 loc) · 3.81 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
#!/bin/bash
# Hotspot Setup Script
# This script stops NetworkManager and creates a WiFi hotspot using hostapd and dnsmasq
set -e
# Configuration variables
INTERFACE="wlan0" # WiFi interface
SSID="LaserUI-Hotspot" # Network name
PASSWORD="laserui123" # WPA password (min 8 characters)
CHANNEL="6" # WiFi channel
IP_ADDRESS="192.168.50.1" # Hotspot IP address
NETMASK="255.255.255.0" # Network mask
DHCP_RANGE_START="192.168.50.10"
DHCP_RANGE_END="192.168.50.50"
DHCP_LEASE_TIME="12h"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}Error: This script must be run as root${NC}"
echo "Please run: sudo $0"
exit 1
fi
echo -e "${GREEN}=== WiFi Hotspot Setup ===${NC}"
# Step 1: Stop NetworkManager
echo -e "${YELLOW}[1/7] Stopping NetworkManager...${NC}"
systemctl stop NetworkManager
systemctl disable NetworkManager
echo -e "${GREEN}✓ NetworkManager stopped${NC}"
# Step 2: Bring down the interface
echo -e "${YELLOW}[2/7] Bringing down interface ${INTERFACE}...${NC}"
ip link set $INTERFACE down
sleep 2
echo -e "${GREEN}✓ Interface down${NC}"
# Step 3: Configure the interface
echo -e "${YELLOW}[3/7] Configuring interface ${INTERFACE}...${NC}"
ip addr flush dev $INTERFACE
ip addr add ${IP_ADDRESS}/24 dev $INTERFACE
ip link set $INTERFACE up
echo -e "${GREEN}✓ Interface configured with IP ${IP_ADDRESS}${NC}"
# Step 4: Create hostapd configuration
echo -e "${YELLOW}[4/7] Creating hostapd configuration...${NC}"
cat > /etc/hostapd/hostapd.conf <<EOF
# Interface configuration
interface=$INTERFACE
driver=nl80211
# WiFi configuration
ssid=$SSID
hw_mode=g
channel=$CHANNEL
ieee80211n=1
wmm_enabled=1
# Security configuration
auth_algs=1
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_passphrase=$PASSWORD
rsn_pairwise=CCMP
# Logging
logger_syslog=-1
logger_syslog_level=2
logger_stdout=-1
logger_stdout_level=2
EOF
echo -e "${GREEN}✓ hostapd configuration created${NC}"
# Step 5: Create dnsmasq configuration
echo -e "${YELLOW}[5/7] Creating dnsmasq configuration...${NC}"
# Backup original dnsmasq.conf if it exists
if [ -f /etc/dnsmasq.conf ]; then
cp /etc/dnsmasq.conf /etc/dnsmasq.conf.backup
fi
cat > /etc/dnsmasq.conf <<EOF
# Interface configuration
interface=$INTERFACE
bind-interfaces
# DHCP configuration
dhcp-range=$DHCP_RANGE_START,$DHCP_RANGE_END,$NETMASK,$DHCP_LEASE_TIME
dhcp-option=3,$IP_ADDRESS # Default gateway
dhcp-option=6,$IP_ADDRESS # DNS server
# DNS configuration
server=8.8.8.8
server=8.8.4.4
# Logging
log-queries
log-dhcp
EOF
echo -e "${GREEN}✓ dnsmasq configuration created${NC}"
# Step 6: Enable IP forwarding
echo -e "${YELLOW}[6/7] Enabling IP forwarding...${NC}"
echo 1 > /proc/sys/net/ipv4/ip_forward
# Make it persistent
if ! grep -q "net.ipv4.ip_forward=1" /etc/sysctl.conf; then
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
fi
echo -e "${GREEN}✓ IP forwarding enabled${NC}"
# Step 7: Start services
echo -e "${YELLOW}[7/7] Starting hostapd and dnsmasq...${NC}"
# Kill any existing instances
pkill hostapd 2>/dev/null || true
pkill dnsmasq 2>/dev/null || true
sleep 1
# Start hostapd
hostapd /etc/hostapd/hostapd.conf -B
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ hostapd started${NC}"
else
echo -e "${RED}✗ Failed to start hostapd${NC}"
exit 1
fi
# Start dnsmasq
dnsmasq -C /etc/dnsmasq.conf
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ dnsmasq started${NC}"
else
echo -e "${RED}✗ Failed to start dnsmasq${NC}"
exit 1
fi
echo ""
echo -e "${GREEN}=== Hotspot Setup Complete ===${NC}"
echo -e "SSID: ${YELLOW}$SSID${NC}"
echo -e "Password: ${YELLOW}$PASSWORD${NC}"
echo -e "Gateway IP: ${YELLOW}$IP_ADDRESS${NC}"
echo ""
echo -e "To stop the hotspot, run: ${YELLOW}sudo ./stop_hotspot.sh${NC}"
echo ""