From 22b9a3440fad9cd48ed867d81ac686a452f039bc Mon Sep 17 00:00:00 2001 From: Adar Nimrod <nimrod@shore.co.il> Date: Sun, 12 Mar 2017 09:32:41 +0200 Subject: [PATCH] - The interface is passed, not the connection. --- Documents/train-wifi.sh | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/Documents/train-wifi.sh b/Documents/train-wifi.sh index a9a3287..5cb8df8 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" -- GitLab