Skip to content

Instantly share code, notes, and snippets.

@EnigmaCurry
Last active December 30, 2023 10:52
Show Gist options
  • Save EnigmaCurry/34fd778ad8108b2212a4e0547a51fe5c to your computer and use it in GitHub Desktop.
Save EnigmaCurry/34fd778ad8108b2212a4e0547a51fe5c to your computer and use it in GitHub Desktop.
#!/bin/bash
## This is a setup script for temporary sharing of an internet connection
## Configure the variables at the top of this file, then run:
## sudo ./internet-sharing.sh setup
## And optionally start the DHCP server:
## sudo ./internet-sharing.sh dhcp
## Connect your other computer or LAN to the second network device.
## Dnsmasq will output the IP address(es) of connected clients as they request them.
## Press Ctrl-C to quit dnsmasq when you no longer need DHCP.
## When all done, run teardown:
## sudo ./internet-sharing.sh teardown
### Configure these variables for yourself:
## The name of your network device connected to internet:
INTERNET_DEV=wlp3s0
## The name of the secondary network device to share with:
SHARE_DEV=enp0s25
## The new IP address of this computer, on the sharing network:
SHARE_GATEWAY=192.168.123.1
## Subnet:
SHARE_SUBNET_CIDR=24
SHARE_SUBNET=192.168.123.0/24
## DHCP:
DNSMASQ=$(which dnsmasq)
DHCP_DNS=1.0.0.1
DHCP_RANGE=192.168.123.50,192.168.123.100,12h
exe() { ( echo "## $*"; $*; ) }
error() {
printf '\E[31m'; echo "$@"; printf '\E[0m'
}
setup() {
## Assign IP address
exe ip link set up dev ${SHARE_DEV}
exe ip addr add ${SHARE_GATEWAY}/${SHARE_SUBNET_CIDR} dev ${SHARE_DEV}
## Enable IP forwarding:
exe sysctl net.ipv4.ip_forward=1
exe iptables -t nat -A POSTROUTING -o ${INTERNET_DEV} -j MASQUERADE
exe iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
exe iptables -A FORWARD -i ${SHARE_DEV} -o ${INTERNET_DEV} -j ACCEPT
## Enable access for DHCP server:
exe iptables -I INPUT -p udp --dport 67 -i ${SHARE_DEV} -j ACCEPT
exe iptables -I INPUT -p udp --dport 53 -s ${SHARE_SUBNET} -j ACCEPT
exe iptables -I INPUT -p tcp --dport 53 -s ${SHARE_SUBNET} -j ACCEPT
}
teardown() {
(
set +e
exe ip link set down dev ${SHARE_DEV}
exe ip addr del ${SHARE_GATEWAY}/${SHARE_SUBNET_CIDR} dev ${SHARE_DEV}
exe iptables -t nat -D POSTROUTING -o ${INTERNET_DEV} -j MASQUERADE
exe iptables -D FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
exe iptables -D FORWARD -i ${SHARE_DEV} -o ${INTERNET_DEV} -j ACCEPT
exe iptables -D INPUT -p udp --dport 67 -i ${SHARE_DEV} -j ACCEPT
exe iptables -D INPUT -p udp --dport 53 -s ${SHARE_SUBNET} -j ACCEPT
exe iptables -D INPUT -p tcp --dport 53 -s ${SHARE_SUBNET} -j ACCEPT
)
}
dhcp() {
if [[ $DNSMASQ == "" ]]; then
error "Could not find dnsmasq installed. Please install dnsmasq and try again."
exit 1
fi
## Start dnsmasq:
echo "Starting dnsmasq DHCP server, press Ctrl-C to quit when done."
exe ${DNSMASQ} -i ${SHARE_DEV} --port 0 --bind-interfaces --dhcp-option=3,${SHARE_GATEWAY} \
--dhcp-option=6,${DHCP_DNS} --dhcp-range=${DHCP_RANGE} --no-daemon
}
main() {
set -e
if [[ $EUID -ne 0 ]]; then
error "This script must be run as root."
exit 1
fi
if [[ $# -gt 0 ]]; then
$*
else
echo "## Must specify a command: setup, dhcp, teardown"
fi
}
main $*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment