diff --git a/Documents/train-wifi.sh b/Documents/train-wifi.sh index a9a328728cce1e125fc711671b67b6a51e99d748..5cb8df8842a4908dea1d408df74ee6acc86815b2 100755 --- a/Documents/train-wifi.sh +++ b/Documents/train-wifi.sh @@ -22,7 +22,7 @@ # SOFTWARE. # Logs in to the Israeli train's wifi. -# Requires: cURL, sh, awk, logger. +# Requires: cURL, sh, awk, logger, nmcli. # To install run: # sudo cp --preserve=mode train-wifi.sh /etc/NetworkManager/dispatcher.d/90trainwifi @@ -33,6 +33,15 @@ die () { exit 1 } +iswifi () { + # TODO: Check if a network interface is passed. + [ "$(nmcli --terse --fields GENERAL.TYPE device show $1 | awk -F: '{print $2}')" = 'wifi' ] +} + +wifi_connection () { + nmcli --terse --fields GENERAL.CONNECTION device show $1 | awk -F: '{print $2}' +} + if [ $# -ne 2 ] then echo "Usage: $0 interface action" @@ -44,18 +53,19 @@ fi which curl > /dev/null || die "Can't login to the train wifi, cURL is not installed." which awk > /dev/null || die "Can't login to the train wifi, awk is not installed." -which logger > /dev/null || dir "Can't login to the train wifi, logger is not installed." +which logger > /dev/null || die "Can't login to the train wifi, logger is not installed." +which nmcli > /dev/null || die "Can't login to the train wifi, nmcli is not installed." -if [ "$interface" = "ISRAEL-RAILWAYS" ] && [ "$action" = "up" ] -then - redirect_url="$(curl --output /dev/null --silent --write-out '%{redirect_url}' http://google.com/)" - logger -p user.debug "Train wifi redirect url: $redirect_url" - login_ip="$(echo "$redirect_url" | grep --only-matching '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*')" - logger -p user.debug "Train wifi login IP: $ip" - login_url="http://$ip/loginHandler.php?allowAccess=true" - logger -p user.debug "Train wifi login URL: $login_url" - http_code="$(curl --output /dev/null --silent --write-out '%{http_code}' "$login_url")" - logger -p user.debug "Train wifi login HTTP code: $http_code" -else - logger -p user.debug "Interface isn't ISRAEL-RAILWAYS or action isn't up, not signing in to the train wifi." -fi +[ "$action" = 'up' ] || die "Can't login to the train wifi, action $action isn't up." +iswifi "$interface" || die "Can't login to the train wifi, interface $interface isn't wifi." +connection="$(wifi_connection $interface)" +[ "$connection" = "ISRAEL-RAILWAYS" ] || die "Can't login to the train wifi, wifi network $connection isn't ISRAEL-RAILWAYS." + +redirect_url="$(curl --output /dev/null --silent --write-out '%{redirect_url}' http://google.com/)" +logger -p user.debug "Train wifi redirect url: $redirect_url" +login_ip="$(echo "$redirect_url" | grep --only-matching '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*')" || die "Can't login to the train wifi, redirect URL doesn't contain an IP." +logger -p user.debug "Train wifi login IP: $ip" +login_url="http://$ip/loginHandler.php?allowAccess=true" +logger -p user.debug "Train wifi login URL: $login_url" +http_code="$(curl --output /dev/null --silent --write-out '%{http_code}' "$login_url")" +logger -p user.debug "Train wifi login HTTP code: $http_code"