Skip to content

Commit

Permalink
Merge branch 'better-errors'
Browse files Browse the repository at this point in the history
  • Loading branch information
falkecarlsen committed Apr 30, 2019
2 parents fce5173 + 12ed1d2 commit da56d4d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Waiting for network connection...
Connection took: 5.293s
Network connection established, updating cached passwords
Sourced credentials with username: username
Fetched and extracted updated passwords to 'passwords'
Successfully updated cached passwords
```

## Dependencies
Expand Down
43 changes: 24 additions & 19 deletions chwifi
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/bin/bash
# netctl managing script for easier workday mornings and afternoons

# change to dir of script for sourcing
# Change to dir of script for sourcing
path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
cd "$path"

# source helper-script for calling getcurrentpassword and updatepasswords-function
# Source helper-script for calling getcurrentpassword and updatepasswords-function
source passwordhandler.sh

# source config for user-specific variables
# Source config for user-specific variables
source config

connect() {
Expand All @@ -31,12 +31,12 @@ restart_profile() {
$sudo $network_manager $network_manager_restart $1
}

# safely kill adapter between profiles to ensure smooth dis- and connection, also print ip link status
# Safely kill adapter between profiles to ensure smooth dis- and connection, also print ip link status
set_adapter_down() {
$sudo ip link set $wireless_adapter down
}

# disconnect and connect routine, stops all active profiles and takes connect profile as first parameter
# Disconnect and connect routine, stops all active profiles and takes connect profile as first parameter
connection_routine() {
stop_all
set_adapter_down
Expand All @@ -45,12 +45,12 @@ connection_routine() {
update_routine
}

# perl regex; find "Key=" and replace all chars after, on given line, with parameter $2, do this on file at $1
# Perl regex; find "Key=" and replace all chars after, on given line, with parameter $2, do this on file at $1
replace_password() {
$sudo perl -pi -e "s/(?<=Key=).*$/$2/" $1
}

# call function with two args; concatenation of $network_manager_location and profile as $1, followed by password as $2
# Call function with two args; concatenation of $network_manager_location and profile as $1, followed by password as $2
change_profile_password() {
replace_password $network_manager_location$1 $2
}
Expand All @@ -69,9 +69,14 @@ update_routine() {
wait_for_network
printf '%s\n' "Network connection established, updating cached passwords"
update_passwords
if [[ $? -eq 0 ]]; then
printf "Successfully updated cached passwords\n"
else
printf "Failed to update cached passwords!"\n
fi
}

# change MAC-address according to sourced options and print new MAC-address
# Change MAC-address according to sourced options and print new MAC-address
change_mac() {
printf "New MAC-address: "
$sudo macchanger $macchanger_options $wireless_adapter | grep New | grep -o '\(\([0-9a-f]\{2\}\):\)\{5\}[0-9a-f]\{2\}'
Expand All @@ -86,47 +91,47 @@ display_help() {
printf "\twork\n"
}

# if no-args, print help
# If no-args, print help
if [[ $# -eq 0 ]] ; then
display_help
elif [[ $# -gt 0 ]]; then

# if home, then connect and update cached passwords when connection is given
# If home, then connect and update cached passwords when connection is given
if [[ "$1" =~ home ]]; then
printf '%s\n' "Home-keyword found, connecting to home"
connection_routine $network_manager_home_profile

# if work, then find password, connect and update cached passwords when connection is given
# If work, then find password, connect and update cached passwords when connection is given
elif [[ "$1" =~ work ]]; then
printf '%s\n' "Work-keyword found, checking for cached password"
# grab daily password from helper
# Grab daily password from helper
daily_password=$(get_daily_password)

# if no cached password, prompt for password
# If no cached password, prompt for password
if [[ $? -eq 1 ]]; then
printf '%s' "No cached password found, input password manually: "
read daily_password
else
# print daily password, if found
# Print daily password, if found
printf '%s\n' "Daily work password is: $daily_password"
fi

# change work profile password and do connection routine
# Change work profile password and do connection routine
change_profile_password $network_manager_work_profile $daily_password
connection_routine $network_manager_work_profile

# if alpha, num, alpha regex, then work password
# If alpha, num, alpha regex, then work password
elif [[ "$1" =~ $password_syntax ]]; then
printf '%s\n' "Connecting to work with given password: $1"

# change work profile password and do connection routine
# Change work profile password and do connection routine
change_profile_password $network_manager_work_profile $daily_password
connection_routine $network_manager_work_profile

# if two arguments are given, process those
# If two arguments are given, process those
elif [[ $# -eq 2 ]]; then

# if first argument is either '-r' or 'restart' then restart profile given as second argument
# If first argument is either '-r' or 'restart' then restart profile given as second argument
if [[ "$1" =~ -r ]] || [[ "$1" =~ restart ]]; then
restart_profile $2
fi
Expand Down
28 changes: 21 additions & 7 deletions passwordhandler.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/bash
# handles fetching of passwords, extracting, writing to file, and returning current, daily password
# Handles fetching of passwords, extracting, writing to file, and returning current, daily password

# source config for user-variables
# Source config for user-variables
source config

# set current date locally
# Set current date locally
date=$(date +%d/%m/%Y)

extract_password_table() {
Expand All @@ -13,25 +13,39 @@ extract_password_table() {

update_passwords() {
printf '%s\n' "Sourced credentials with username: $username"
# Run cashandler at $dest, with $username and $password, and redirect output to html on filesystem
source cashandler.sh $dest $username $password > $password_html_file
# Run extractor on html and redirect resulting table to password-file
extract_password_table $password_html_file > $password_file
printf '%s\n' "Fetched and extracted updated passwords to '$password_file'"
# Assume that some sensible passwords were extracted and read array into variable
readarray -t password_array < $password_file

# If first password's date in password-file is the current date, then update succeeded and return success
if [[ ${password_array[3]} =~ $date ]]; then
return 0
else
return 1
fi
}

get_daily_password() {
# if file does not exist, then return error
# If file does not exist, then return error
if [[ ! -f $password_file ]]; then
return 1
fi

# Read in array
readarray -t password_array < $password_file

# For rows in flattened table
for i in {0..9}
do
# if password found, echo it, and return
# If password found, echo it, and return success
if [[ ${password_array[$i]} =~ $date ]]; then
echo ${password_array[$i-1]}
return 0
fi
done
# else return error
# Else return error
return 1
}

0 comments on commit da56d4d

Please sign in to comment.