tabs -> spaces

This commit is contained in:
fosslinux 2018-11-16 17:26:43 +11:00 committed by Samuel Tyler
parent e53826911b
commit f0890e6f39
3 changed files with 318 additions and 318 deletions

246
setup.sh
View File

@ -19,145 +19,145 @@
# Email: oneseveneight@airmail.cc # Email: oneseveneight@airmail.cc
is_substring(){ is_substring(){
case $2 in case $2 in
*$1*) *$1*)
return 0 return 0
;; ;;
*) *)
return 1 return 1
;; ;;
esac esac
} }
configure_directories() { configure_directories() {
server_name="$1" server_name="$1"
local_dir="" local_dir=""
remote_dir="" remote_dir=""
echo "What would you like to call the local directory? (~/Remote/...)" echo "What would you like to call the local directory? (~/Remote/...)"
read -r answer read -r answer
local_dir="$HOME/Remote/$answer" local_dir="$HOME/Remote/$answer"
mkdir -p "$local_dir" mkdir -p "$local_dir"
echo "Created directory $local_dir" echo "Created directory $local_dir"
echo "What directory on \"$server_name\" would you like to link it to? (Absolute path)" echo "What directory on \"$server_name\" would you like to link it to? (Absolute path)"
read -r answer read -r answer
remote_dir="$answer" remote_dir="$answer"
echo "Linking directory $local_dir to $remote_dir on \"$server_name\"" echo "Linking directory $local_dir to $remote_dir on \"$server_name\""
sshfs "$server_name:$remote_dir" "$local_dir" sshfs "$server_name:$remote_dir" "$local_dir"
echo "Done, enjoy!" echo "Done, enjoy!"
} }
attempt_login() { attempt_login() {
server_name="$1" server_name="$1"
ssh_temp_config="$2" ssh_temp_config="$2"
ssh_config="$3" ssh_config="$3"
echo "Attempting to log in with configuration for \"$server_name\"" echo "Attempting to log in with configuration for \"$server_name\""
if ssh -qF "$ssh_temp_config" "$server_name" exit if ssh -qF "$ssh_temp_config" "$server_name" exit
then then
echo "Login successful! Writing ssh config..." echo "Login successful! Writing ssh config..."
cp "$ssh_temp_config" "$ssh_config" cp "$ssh_temp_config" "$ssh_config"
echo "Would you like to configure sshfs for this server? (y/n)" echo "Would you like to configure sshfs for this server? (y/n)"
read -r answer read -r answer
case "$answer" in case "$answer" in
"y") "y")
configure_directories "$server_name" configure_directories "$server_name"
;; ;;
"n") "n")
echo "All right, you're all done!" echo "All right, you're all done!"
echo "Exiting..." echo "Exiting..."
;; ;;
"*") "*")
echo "\"$answer\" was not recognized as an answer, please enter 'y' or 'n'" echo "\"$answer\" was not recognized as an answer, please enter 'y' or 'n'"
esac esac
else else
echo "Login failed, what would you like to do?" echo "Login failed, what would you like to do?"
echo " 1) Try logging in again" echo " 1) Try logging in again"
echo " 2) Re-configure my credentials" echo " 2) Re-configure my credentials"
read -r answer read -r answer
case "$answer" in case "$answer" in
"1") "1")
attempt_login "$server_name" "$ssh_temp_config" "$ssh_config" attempt_login "$server_name" "$ssh_temp_config" "$ssh_config"
;; ;;
"2") "2")
setup_keys "$server_address" setup_keys "$server_address"
;; ;;
esac esac
fi fi
} }
setup_keys() { setup_keys() {
server_address="$1" server_address="$1"
server_name="$2" server_name="$2"
login="" login=""
key_path="" key_path=""
ssh_dir="" ssh_dir=""
key_type="" key_type=""
echo "Configuring \"$server_address\" as \"$server_name\"" echo "Configuring \"$server_address\" as \"$server_name\""
echo "What is your login name for this server?" echo "What is your login name for this server?"
read -r login read -r login
echo "Where is the pivate key you use for this account (Absolute path)" echo "Where is the pivate key you use for this account (Absolute path)"
read -r key_path read -r key_path
if test -r "$key_path" if test -r "$key_path"
then then
echo "File found, configuring ssh..." echo "File found, configuring ssh..."
ssh_dir="$HOME/.ssh" ssh_dir="$HOME/.ssh"
ssh_host_dir="$HOME/.ssh/$server_name" ssh_host_dir="$HOME/.ssh/$server_name"
key_type=$(head -n 1 "$key_path" \ key_type=$(head -n 1 "$key_path" \
| cut -f 2 -d ' ' \ | cut -f 2 -d ' ' \
| tr '[:upper:]' '[:lower:]') | tr '[:upper:]' '[:lower:]')
test -d "$ssh_dir" || mkdir "$ssh_dir" test -d "$ssh_dir" || mkdir "$ssh_dir"
test -d "$ssh_host_dir" || mkdir "$ssh_host_dir" test -d "$ssh_host_dir" || mkdir "$ssh_host_dir"
echo "Key appears to be of type \"$key_type\"" echo "Key appears to be of type \"$key_type\""
echo "Generating public key..." echo "Generating public key..."
ssh-keygen -y -f "$key_path" > "$ssh_host_dir/id_$key_type.pub" ssh-keygen -y -f "$key_path" > "$ssh_host_dir/id_$key_type.pub"
echo "Copying private key..." echo "Copying private key..."
cp "$key_path" "$ssh_host_dir/id_$key_type" cp "$key_path" "$ssh_host_dir/id_$key_type"
echo "Saving configuration..." echo "Saving configuration..."
test -e "$ssh_dir/config" || touch "$ssh_dir/config" test -e "$ssh_dir/config" || touch "$ssh_dir/config"
cp "$ssh_dir/config" "$ssh_dir/config.tmp" cp "$ssh_dir/config" "$ssh_dir/config.tmp"
{ {
echo "Host $server_name" echo "Host $server_name"
echo " HostName $server_address" echo " HostName $server_address"
echo " port 22" echo " port 22"
echo " User $login" echo " User $login"
echo " IdentityFile $ssh_host_dir/id_$key_type" echo " IdentityFile $ssh_host_dir/id_$key_type"
} >> "$ssh_dir/config.tmp" } >> "$ssh_dir/config.tmp"
echo "Right now your configuration looks like this:" echo "Right now your configuration looks like this:"
cat "$ssh_dir/config.tmp" cat "$ssh_dir/config.tmp"
attempt_login "$server_name" "$ssh_dir/config.tmp" "$ssh_dir/config" attempt_login "$server_name" "$ssh_dir/config.tmp" "$ssh_dir/config"
else else
echo "$key_path could not be read." echo "$key_path could not be read."
echo "Either it doesn't exist, or you dont have the correct permissions, please try again." echo "Either it doesn't exist, or you dont have the correct permissions, please try again."
setup_keys "$server_address" "$server_name" setup_keys "$server_address" "$server_name"
fi fi
} }
pick_account() { pick_account() {
answers="1 2" answers="1 2"
answer="" answer=""
echo "What account would you like to configure?" echo "What account would you like to configure?"
echo " 1) tilde.town" echo " 1) tilde.town"
echo " 2) tilde.team" echo " 2) tilde.team"
read -r answer read -r answer
if is_substring "$answer" "$answers" if is_substring "$answer" "$answers"
then then
case "$answer" in case "$answer" in
"1") "1")
setup_keys "tilde.town" "town" setup_keys "tilde.town" "town"
;; ;;
"2") "2")
setup_keys "tilde.team" "team" setup_keys "tilde.team" "team"
;; ;;
esac esac
else else
echo "\"$answer\" is not a valid option" echo "\"$answer\" is not a valid option"
pick_account pick_account
fi fi
} }
pick_account pick_account

View File

@ -12,177 +12,177 @@ ssh_dir="$HOME/.ssh"
priv_key_path=" " priv_key_path=" "
get_field(){ get_field(){
field_num="$1" field_num="$1"
info="$2" info="$2"
echo "$info" | cut -f "$field_num" -d '|' echo "$info" | cut -f "$field_num" -d '|'
} }
error_box(){ error_box(){
error_text="$1" error_text="$1"
yad \ yad \
--posx="$mouse_x" \ --posx="$mouse_x" \
--posy="$mouse_y" \ --posy="$mouse_y" \
--width 250 \ --width 250 \
--image "dialog-error" \ --image "dialog-error" \
--title "Error!" \ --title "Error!" \
--text "$error_text" \ --text "$error_text" \
--button="OK":0 --button="OK":0
} }
succ_box(){ succ_box(){
succ_text="$1" succ_text="$1"
yad \ yad \
--posx="$mouse_x" \ --posx="$mouse_x" \
--posy="$mouse_y" \ --posy="$mouse_y" \
--width 250 \ --width 250 \
--image "dialog-info" \ --image "dialog-info" \
--title "Success!" \ --title "Success!" \
--text "$succ_text" \ --text "$succ_text" \
--button="OK":0 --button="OK":0
} }
dec_box(){ dec_box(){
error_text="$1" error_text="$1"
yad \ yad \
--posx="$mouse_x" \ --posx="$mouse_x" \
--posy="$mouse_y" \ --posy="$mouse_y" \
--width 250 \ --width 250 \
--image "dialog-error" \ --image "dialog-error" \
--title "Error!" \ --title "Error!" \
--text "$error_text" \ --text "$error_text" \
--button="No":1 \ --button="No":1 \
--button="Yes":0 --button="Yes":0
return $? return $?
} }
info_box(){ info_box(){
info_text="$1" info_text="$1"
yad \ yad \
--posx="$mouse_x" \ --posx="$mouse_x" \
--posy="$mouse_y" \ --posy="$mouse_y" \
--no-buttons \ --no-buttons \
--width 250 \ --width 250 \
--image "dialog-info" \ --image "dialog-info" \
--title "Info" \ --title "Info" \
--text "$info_text" --text "$info_text"
} }
get_info() { get_info() {
error_num=0 error_num=0
error_msg="" error_msg=""
info=$(yad \ info=$(yad \
--posx="$mouse_x" \ --posx="$mouse_x" \
--posy="$mouse_y" \ --posy="$mouse_y" \
--title="New server setup" \ --title="New server setup" \
--text="Add a new server" \ --text="Add a new server" \
--form \ --form \
--field="Server":CB \ --field="Server":CB \
--field="Short Name" \ --field="Short Name" \
--field="Login" \ --field="Login" \
--field="Port" \ --field="Port" \
--field="SSH Directory":DIR \ --field="SSH Directory":DIR \
--field="Private key":FL \ --field="Private key":FL \
--button="Cancel":1 \ --button="Cancel":1 \
--button="Continue":0 \ --button="Continue":0 \
"$server_address" "$server_name" "$login" "$ssh_port" "$ssh_dir" "$priv_key_path") "$server_address" "$server_name" "$login" "$ssh_port" "$ssh_dir" "$priv_key_path")
[ $? -ne 0 ] && exit # Shellcheck will yell at me for this, perhaps find a better way? [ $? -ne 0 ] && exit # Shellcheck will yell at me for this, perhaps find a better way?
server_address=$(get_field 1 "$info") server_address=$(get_field 1 "$info")
server_name=$(get_field 2 "$info") server_name=$(get_field 2 "$info")
login=$(get_field 3 "$info") login=$(get_field 3 "$info")
ssh_port=$(get_field 4 "$info") ssh_port=$(get_field 4 "$info")
ssh_dir=$(get_field 5 "$info") ssh_dir=$(get_field 5 "$info")
priv_key_path=$(get_field 6 "$info") priv_key_path=$(get_field 6 "$info")
if test -z "$login" if test -z "$login"
then then
error_msg="No login provided!" error_msg="No login provided!"
error_num=$(( error_num + 1)) error_num=$(( error_num + 1))
fi fi
if test -d "$(echo "$priv_key_path" | sed 's/\/ *$//')" if test -d "$(echo "$priv_key_path" | sed 's/\/ *$//')"
then then
error_msg="$error_msg\\nNo private key provided!" error_msg="$error_msg\\nNo private key provided!"
error_num=$(( error_num + 1)) error_num=$(( error_num + 1))
fi fi
if [ $error_num -gt 0 ] if [ $error_num -gt 0 ]
then then
error_box "$error_msg" \ error_box "$error_msg" \
&& get_info && get_info
fi fi
configure_server configure_server
} }
configure_server() { configure_server() {
ssh_current_config="$ssh_dir/config" ssh_current_config="$ssh_dir/config"
ssh_temp_config="$ssh_dir/config.tmp" ssh_temp_config="$ssh_dir/config.tmp"
ssh_host_dir="$ssh_dir/$server_address" ssh_host_dir="$ssh_dir/$server_address"
key_type=$(head -n 1 "$priv_key_path" \ key_type=$(head -n 1 "$priv_key_path" \
| cut -f 2 -d ' ' \ | cut -f 2 -d ' ' \
| tr '[:upper:]' '[:lower:]') | tr '[:upper:]' '[:lower:]')
test -e "$ssh_current_config" \ test -e "$ssh_current_config" \
|| touch "$ssh_current_config" || touch "$ssh_current_config"
cp "$ssh_current_config" "$ssh_temp_config" cp "$ssh_current_config" "$ssh_temp_config"
if grep -qi "Host $server_name" "$ssh_current_config" if grep -qi "Host $server_name" "$ssh_current_config"
then then
dec_box "Host named $server_name already exists, overwrite?" \ dec_box "Host named $server_name already exists, overwrite?" \
|| get_info || get_info
if ! dec_box "Keep old config in file? (ssh will use the new one)" if ! dec_box "Keep old config in file? (ssh will use the new one)"
then then
IFS="" IFS=""
in_old_host_block=0 in_old_host_block=0
while read -r line while read -r line
do do
if echo "$line" | grep -q "Host $server_name" if echo "$line" | grep -q "Host $server_name"
then then
in_old_host_block=0 in_old_host_block=0
elif echo "$line" | grep -q "Host " elif echo "$line" | grep -q "Host "
then then
in_old_host_block=1 in_old_host_block=1
fi fi
[ $in_old_host_block -eq 1 ] \ [ $in_old_host_block -eq 1 ] \
&& echo "$line" && echo "$line"
done < "$ssh_current_config" > "$ssh_temp_config" done < "$ssh_current_config" > "$ssh_temp_config"
fi fi
fi fi
mkdir -p "$ssh_host_dir" mkdir -p "$ssh_host_dir"
cp "$priv_key_path" "$ssh_host_dir/id_$key_type" cp "$priv_key_path" "$ssh_host_dir/id_$key_type"
{ {
echo "Host $server_name" echo "Host $server_name"
echo " HostName $server_address" echo " HostName $server_address"
echo " Port $ssh_port" echo " Port $ssh_port"
echo " User $login" echo " User $login"
echo " IdentityFile $ssh_host_dir/id_$key_type" echo " IdentityFile $ssh_host_dir/id_$key_type"
} >> "$ssh_temp_config" } >> "$ssh_temp_config"
info_box "Attempting to log in with the provided credentials" & info_box "Attempting to log in with the provided credentials" &
info_pid=$(( $! + 2 )) # yikes, is this safe? info_pid=$(( $! + 2 )) # yikes, is this safe?
echo "$ssh_temp_config" echo "$ssh_temp_config"
echo "$server_address" echo "$server_address"
if ssh -qF "$ssh_temp_config" "$server_name" exit if ssh -qF "$ssh_temp_config" "$server_name" exit
then then
kill $info_pid kill $info_pid
succ_box "Login success!\\nAccount for $server_address created!" succ_box "Login success!\\nAccount for $server_address created!"
cp "$ssh_temp_config" "$ssh_current_config" cp "$ssh_temp_config" "$ssh_current_config"
rm "$ssh_temp_config" rm "$ssh_temp_config"
exit # Without this the "Host already exists" box appears, investigate. exit # Without this the "Host already exists" box appears, investigate.
else else
kill $info_pid kill $info_pid
error_box "Login failed, please try again" error_box "Login failed, please try again"
get_info get_info
fi fi
} }
get_info get_info

110
yad.sh
View File

@ -6,76 +6,76 @@ ssh_dir="$HOME/.ssh"
priv_key_path=" " priv_key_path=" "
get_field(){ get_field(){
field_num="$1" field_num="$1"
info="$2" info="$2"
echo "$info" | cut -f "$field_num" -d '|' echo "$info" | cut -f "$field_num" -d '|'
} }
error_box(){ error_box(){
error_text="$1" error_text="$1"
echo "Error: $error_text" echo "Error: $error_text"
yad \ yad \
--width "250" \ --width "250" \
--image "dialog-error" \ --image "dialog-error" \
--title "Error!" \ --title "Error!" \
--text "$error_text" --text "$error_text"
} }
info_box(){ info_box(){
info_text="$1" info_text="$1"
echo "$info_text" echo "$info_text"
yad \ yad \
--no-buttons \ --no-buttons \
--width "250" \ --width "250" \
--image "dialog-info" \ --image "dialog-info" \
--title "Info" \ --title "Info" \
--text "$info_text" --text "$info_text"
} }
get_info() { get_info() {
error_num="0" error_num="0"
error_msg="" error_msg=""
info=$(yad \ info=$(yad \
--title="New server setup" \ --title="New server setup" \
--text="Add a new server" \ --text="Add a new server" \
--form \ --form \
--field="Server":CB \ --field="Server":CB \
--field="Login" \ --field="Login" \
--field="SSH Directory":DIR \ --field="SSH Directory":DIR \
--field="Private key":FL \ --field="Private key":FL \
"$server_address" "$login" "$ssh_dir" "$priv_key_path") "$server_address" "$login" "$ssh_dir" "$priv_key_path")
server_address=$(get_field "1" "$info") server_address=$(get_field "1" "$info")
login=$(get_field "2" "$info") login=$(get_field "2" "$info")
ssh_dir=$(get_field "3" "$info") ssh_dir=$(get_field "3" "$info")
priv_key_path=$(get_field "4" "$info") priv_key_path=$(get_field "4" "$info")
if test -z "$login" if test -z "$login"
then then
error_msg="No login provided!" error_msg="No login provided!"
error_num=$(( error_num + 1)) error_num=$(( error_num + 1))
fi fi
if test -d "$(echo "$priv_key_path" | sed 's/\/ *$//')" if test -d "$(echo "$priv_key_path" | sed 's/\/ *$//')"
then then
error_msg="$error_msg\nNo private key provided!" error_msg="$error_msg\nNo private key provided!"
error_num=$(( error_num + 1)) error_num=$(( error_num + 1))
fi fi
if [ $error_num -gt '0' ] if [ $error_num -gt '0' ]
then then
error_box "$error_msg" error_box "$error_msg"
get_info get_info
fi fi
} }
test_info() { test_info() {
info_box "Attempting to log in with the provided credentials" info_box "Attempting to log in with the provided credentials"
if ssh -qF "$ssh_temp_config" "$server_name" exit if ssh -qF "$ssh_temp_config" "$server_name" exit
then then
info_box "Login success! writing config..." info_box "Login success! writing config..."
fi fi
} }
get_info get_info