Skip to content

Instantly share code, notes, and snippets.

@SiddheshNan
Created September 10, 2019 18:54
Show Gist options
  • Save SiddheshNan/5d49bb92b9ee1e20d53d774044d0a945 to your computer and use it in GitHub Desktop.
Save SiddheshNan/5d49bb92b9ee1e20d53d774044d0a945 to your computer and use it in GitHub Desktop.
Share Internet using Ethernet on Arch Linux
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
echo Share Internet using Ethernet on Arch Linux - https://wiki.archlinux.org/index.php/Internet_sharing
echo ""
echo "(Tip) Run: 'ip addr' to get interface names"
echo ""
read -p "Enter ethernet interface name : " eth_interface
echo ""
if [[ -z "$eth_interface" ]]; then
#eth_interface="enp2s0f1"
echo "Please enter vaild ethernet interface name, Script exiting with error code 1 ..."
#echo "Entered Ethernet Interface Name Invaild.. Using default name : $eth_interface"
#echo ""
exit 1
fi
read -p "Enter WiFi interface name : " wifi_interface
echo ""
if [[ -z "$wifi_interface" ]]; then
#wifi_interface="wlp3s0"
echo "Please enter vaild WiFi interface name, Script exiting with error code 1 ..."
#echo "Entered WiFi Interface Name Invaild.. Using default name : $wifi_interface"
#echo ""
exit 1
fi
default_cidr="192.168.123.100/24"
read -p "Enter CIDR (default is 192.168.123.100/24) : " new_cidr
if [[ -z "$new_cidr" ]]; then
new_cidr=$default_cidr
echo ""
echo Entered CIDR is empty, using default CIDR : "$new_cidr"
else
echo ""
echo Continuing with CIDR: "$new_cidr"
fi
echo ""
echo "---------------------"
echo ""
echo "Entered Info :"
echo ""
echo "Ethernet Interface: $eth_interface"
echo ""
echo "WiFi Interface: $wifi_interface"
echo ""
echo "CIDR : $new_cidr"
echo ""
echo "The script will now start and will DISTROY any configs for $eth_interface in NetworkManager"
echo ""
echo "---------------------"
echo ""
read -p "Press any key to continue OR Ctrl+C to Cancel..."
echo ""
echo "Starting Script...."
echo ""
{ # try
echo "Configuring Interface..."
echo ""
ip link set up dev $eth_interface
} || { # catch
echo "Failed to Configure Interface.. exiting with error code 1"
exit 1
}
{ # try
echo "Configuring CIDR..."
echo ""
ip addr add $new_cidr dev $eth_interface
} || { # catch
echo ""
echo "Failed to Configure CIDR.. exiting with error code 1"
exit 1
}
{ # try
echo "Adding Packet forwording..."
echo ""
sysctl net.ipv4.ip_forward=1 > /dev/null 2>&1
echo ""
} || { # catch
echo ""
echo "Failed to Configure Packet forwording.. exiting with error code 1"
exit 1
}
{ # try
echo "Enableing NAT..."
echo ""
iptables -t nat -A POSTROUTING -o $wifi_interface -j MASQUERADE
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i $eth_interface -o $wifi_interface -j ACCEPT
} || { # catch
echo ""
echo "Failed to Configure NAT.. exiting with error code 1"
exit 1
}
{ # try
echo "Assigning IP addresses to the client PC(s) ..."
echo ""
iptables -I INPUT -p udp --dport 67 -i $eth_interface -j ACCEPT
iptables -I INPUT -p udp --dport 53 -s 192.168.123.0/24 -j ACCEPT
iptables -I INPUT -p tcp --dport 53 -s 192.168.123.0/24 -j ACCEPT
} || { # catch
echo ""
echo "Failed to Assign IP addresses.. exiting with error code 1"
exit 1
}
echo ""
echo "Setup Complete!"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment