From 1d983b34fd93e30c18d71bd616fd73f0304a57da Mon Sep 17 00:00:00 2001 From: UTkarsh Date: Fri, 10 Feb 2017 20:57:41 +0530 Subject: [PATCH] Shift everything to util instead of main script --- bitsnet | 31 +++++++++++-------------------- util.sh | 30 +++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/bitsnet b/bitsnet index 34dfe16..55636a6 100755 --- a/bitsnet +++ b/bitsnet @@ -24,7 +24,7 @@ while getopts "u:p:dhUo" o; do update ;; o) - logOut + log_out ;; h | * | [?]) show_help @@ -48,23 +48,11 @@ debug_msg "Not empty" # Check if using wifi or ethernet -devInfo="$(nmcli dev | grep " connected" | cut -d " " -f1)" debug_msg "" debug_msg "Checking device info" -debug_msg "devInfo = $devInfo" -if [[ $devInfo == "en"* ]]; then - # ethernet - dev=1 -elif [[ $devInfo == "wl"* ]]; then - # wifi - dev=2 -else - # unknown - dev=0 -fi +dev=$(get_device) debug_msg "dev = $dev" - # Exit if dev = 0 if [[ "$dev" == "0" ]]; then debug_msg "Exiting because dev is zero" @@ -73,23 +61,26 @@ fi # Check if already logged in +# Doesnt matter for now as a request takes lesser time # Select correct login page according to IP # and load corresponding login_url -debug_msg "" -debug_msg "Selecting login url" -debug_msg "Using url: $login_url" +# Nothing to do here for now # Login debug_msg "" debug_msg "Logging in" -if [[ "$dev" == "2" ]]; then +if [[ "$dev" == "2" && "$(hostname -I)" != "10.20"* ]]; then + # Log into the router debug_msg "Connected to wifi. Sending request to cisco router form" - wget --post-data="username=$username&password=$password&loginAccept=1&buttonClicked=4&err_flag=0&info_flag=0&Submit=Submit&err_msg=&info_msg=&redirect_url=" https://20.20.2.11/login.html --no-check-certificate --quiet -O /dev/null + router_login fi -reply=$(wget -qO- --no-check-certificate --post-data="mode=191&username=$username&password=$password" $login_url) +# Log into the ldap +debug_msg "Logging into the LDAP" +reply=$(ldap_login) + # Extract reply debug_msg "Extracting reply" diff --git a/util.sh b/util.sh index 3cc0516..c6b93b6 100644 --- a/util.sh +++ b/util.sh @@ -58,9 +58,37 @@ function update { exit } -function logOut() { +function log_out { reply=$(wget -qO- --no-check-certificate --post-data="mode=193&username=$username&submit=Logout" $login_url) reply=$(extract_msg $reply) send_msg "$reply" exit } + +function get_device { + devInfo="$(nmcli dev | grep " connected" | cut -d " " -f1)" + if [[ $devInfo == "en"* ]]; then + # ethernet + dev=1 + elif [[ $devInfo == "wl"* ]]; then + # wifi + dev=2 + elif [[ $devInfo == "virbr"* ]]; then + # virtual bridge + dev=3 + else + # unknown + dev=0 + fi + + echo "$dev" +} + +function router_login() { + wget --post-data="username=$username&password=$password&loginAccept=1&buttonClicked=4&err_flag=0&info_flag=0&Submit=Submit&err_msg=&info_msg=&redirect_url=" https://20.20.2.11/login.html --no-check-certificate --quiet -O /dev/null +} + +function ldap_login() { + reply=$(wget -qO- --no-check-certificate --post-data="mode=191&username=$username&password=$password" $login_url) + echo $reply +}