Skip to content

Instantly share code, notes, and snippets.

@wizard97
Created August 15, 2017 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wizard97/6695cd3e68ccb712a3eea6e6f9defb7e to your computer and use it in GitHub Desktop.
Save wizard97/6695cd3e68ccb712a3eea6e6f9defb7e to your computer and use it in GitHub Desktop.
A script for sharing an internet connection in Linux, everyday it will use a wpa_supplicant conf
#!/bin/bash
PATH=/home/aaron/bin:/home/aaron/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
wlan="wlp1s0"
en="enp2s0"
# Array of confs
wpa_confs[0]="/home/aaron/wifi/wpa_aaron.conf"
#wpa_confs[0]='wpa_empty.conf'
nconfs=${#wpa_confs[@]}
DD=$(date +%d)
index=$((DD%nconfs))
conf=${wpa_confs[$index]}
echo "Using conf: $conf"
# select new conf
cp "$conf" /etc/wpa_supplicant/wpa_supplicant.conf
#wpa_supplicant -B -i "$wlan" -c /etc/wpa_supplicant/wpa_supplicant.conf
wpa_cli reconfigure
echo "Restarting WLAN interface..."
ip addr flush dev "$wlan"
ifconfig "$wlan" down
# Use antenna 0 in 1x1
iw phy phy0 set antenna 1 1
ifconfig "$wlan" up
#service networking restart
#/etc/init.d/networking restart
# get new dhcp lease
echo "Requesting DHCP lease..."
dhclient "$wlan"
# Share interface
sysctl -w net.ipv4.ip_forward=1
echo "Configuring NAT..."
iptables -t nat -A POSTROUTING -o $wlan -j MASQUERADE
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i $en -o $wlan -j ACCEPT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment