Last active
June 17, 2018 09:28
-
-
Save m3adow/c15b158ec6fb1bf7e20ecc3163f93d4a to your computer and use it in GitHub Desktop.
Script to periodically change the MAC address of an interface an reconnect to WiFi in Ubuntu. Can be used for circumventing public WiFi limits. Has been tested with "WIFIonICE" WiFi from Deutsche Bahn.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
#set -x | |
change_mac(){ | |
OLDMAC=$(ip link show wlan0 |grep -oP '([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})'|head -1) | |
ifconfig ${1} down | |
set +e | |
while true | |
do | |
sudo macchanger -ea ${1} 2>/dev/null | |
if [ $? -eq 0 ] | |
then | |
break | |
else | |
printf "." | |
sleep 1 | |
fi | |
done | |
set -e | |
ifconfig ${1} up | |
} | |
reconnect(){ | |
set +e | |
while true | |
do | |
iwlist wlan0 scan > /dev/null 2>&1 | |
if [ $? -eq 0 ] | |
then | |
break | |
else | |
printf ":" | |
sleep 1 | |
fi | |
done | |
set -e | |
systemctl restart NetworkManager | |
set +e | |
while true | |
do | |
curl -s --connect-timeout 10 "http://www.wifionice.de/de/" \ | |
-H "Host: www.wifionice.de" -H "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0" \ | |
-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: en-US,en;q=0.5" \ | |
--compressed -H "Referer: http://www.wifionice.de/" -H "Content-Type: application/x-www-form-urlencoded" \ | |
-H "Cookie: csrf=42" -H "Connection: keep-alive" -H "Upgrade-Insecure-Requests: 1" \ | |
--data "login=true&CSRFToken=42&connect=" | |
if [ $? -eq 0 ] | |
then | |
break | |
else | |
printf "-" | |
sleep 1 | |
fi | |
done | |
set -e | |
} | |
validate(){ | |
if [ -z "${1}" ] | |
then | |
echo "Please add an interface with '-i'." | |
exit 1 | |
fi | |
if ! [[ $2 =~ ^[0-9]+$ ]] | |
then | |
echo "Rerun interval not numeric." | |
exit 1 | |
fi | |
if [ "$EUID" -ne 0 ] | |
then | |
echo "Please run as root." | |
exit 1 | |
fi | |
} | |
while getopts ":i:r:" opt; do | |
case $opt in | |
i) | |
MYIF=${OPTARG} | |
;; | |
r) | |
SLEEPTIME=${OPTARG} | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
exit 1 | |
;; | |
esac | |
done | |
SLEEPTIME=${SLEEPTIME:-0} | |
# Not always valid, works for my notebook | |
MYIF=${MYIF:-wlan0} | |
while true | |
do | |
validate "${MYIF}" "${SLEEPTIME}" | |
change_mac "${MYIF}" | |
reconnect | |
echo -e "\nMAC change successful." | |
if [ "${SLEEPTIME}" -gt "0" ] | |
then | |
echo "Now sleeping ${SLEEPTIME}s." | |
sleep ${SLEEPTIME} | |
else | |
break | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rev. 2: