-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshare_wifi.sh
47 lines (37 loc) · 1.51 KB
/
share_wifi.sh
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
#!/bin/bash
# Define interfaces
WIFI_INTERFACE="wlan0"
ETH_INTERFACE="eth0"
# Check if script is run as root
if [ "$(id -u)" -ne 0 ]; then
echo "Please run as root or with sudo"
exit 1
fi
# Enable IP forwarding
echo "Enabling IP forwarding..."
sysctl -w net.ipv4.ip_forward=1
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
# Set up NAT with iptables
echo "Setting up iptables for NAT..."
iptables --table nat -A POSTROUTING -o $WIFI_INTERFACE -j MASQUERADE
iptables -A FORWARD -i $ETH_INTERFACE -o $WIFI_INTERFACE -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i $WIFI_INTERFACE -o $ETH_INTERFACE -j ACCEPT
# Save iptables rules so they persist after reboot - Uncomment if needed!
#echo "Saving iptables rules..."
#iptables-save > /etc/iptables/rules.v4
# Configure dnsmasq for DHCP on Ethernet
echo "Configuring dnsmasq..."
cat <<EOL > /etc/dnsmasq.conf
interface=$ETH_INTERFACE # Listen on the Ethernet interface
dhcp-range=192.168.137.50,192.168.137.150,12h # DHCP range for Ethernet clients
EOL
# Restart dnsmasq service
echo "Restarting dnsmasq..."
systemctl restart dnsmasq
# Assign a static IP to the Ethernet interface (optional)
echo "Assigning static IP to Ethernet interface..."
ifconfig $ETH_INTERFACE 192.168.137.1 netmask 255.255.255.0 up
# Print success message
echo "WiFi-to-Ethernet sharing is now enabled!"
echo "Clients connected to the Ethernet port will receive DHCP and have internet access."
# To revert the changes, just reboot (unless you've uncommented iptables-save).