refactored keyfile location

This commit is contained in:
sose 2020-09-11 16:04:08 -07:00
parent e52620265a
commit 2cbd0fc146
3 changed files with 39 additions and 33 deletions

View File

@ -1,39 +1,22 @@
#!/bin/sh #!/bin/sh
connection_test() { connection_test() {
ssh_command="$1" key_file="$1"
tilde_username="$2"
tilde_name="$3"
ssh_command="ssh -i $key_file $tilde_username@$tilde_name"
echo "Running \`$ssh_command\`..."
while ! $ssh_command 'exit' while ! $ssh_command 'exit'
do do
echo "There seems to be a problem connecting to the tilde" echo "There seems to be a problem connecting to the tilde"
echo "Use this shell to try and fix any problems, then try connecting again by exiting the shell" echo "Use this shell to try and fix any problems, then try connecting again by exiting the shell"
bash echo "Use \`exit 1\` to abort the install"
bash || fail
done done
echo "Connection success!" echo "Connection success!"
} }
check_key_file() {
ssh_command=""
chmod 700 /tilde/key/*
tilde_username="$1"
tilde_name="$2"
key_file="$(find /tilde/key | tail -n1)"
if [ -f "$key_file" ]
then
while [ -z "$(cat $key_file | head -n1 | grep -- '-----BEGIN .* PRIVATE KEY-----')" ]
do
echo "$key_file does not look like a private key file, try again"
key_file="$(find /tilde/key | tail -n1)"
bash
done
ssh_command="ssh -i $key_file $tilde_username@$tilde_name"
echo "Key file located!"
fi
echo "Testing ssh connection to $tilde_name as user $tilde_username..."
connection_test "$ssh_command"
}
configure_network() { configure_network() {
echo "Press ENTER to configure network" echo "Press ENTER to configure network"
read dummy_var read dummy_var

View File

@ -1,8 +1,9 @@
#!/bin/sh #!/bin/sh
tilde_name=""
tilde_username=""
key_file="" key_file=""
tilde_username=""
tilde_name=""
startx_command="startlxqt" startx_command="startlxqt"
. ./util.sh . ./util.sh
@ -13,7 +14,7 @@ choice=""
clear clear
echo "=== Welcome to Tildelinux! ===" echo "=== Welcome to Tildelinux! ==="
configure_network || fail configure_network
clear clear
echo "What tilde would you like to connect to? (use the domain name as you would for ssh)" echo "What tilde would you like to connect to? (use the domain name as you would for ssh)"
@ -27,12 +28,21 @@ echo "By whatever means, place your private key in the /tilde/key directory"
echo "Here is a shell, once the key is in the proper location, simply type \"exit\" to close the shell and continue" echo "Here is a shell, once the key is in the proper location, simply type \"exit\" to close the shell and continue"
bash bash
echo "Looking for keyfile..." echo "Looking for key file..."
check_key_file "$tilde_username" "$tilde_name" || fail key_file="$(find_key_file '/tilde/key')"
# the key_file variable has now been set by check_key_file while [ -z "$key_file" ]
do
echo "Could not find a valid key file in /tilde/key"
echo "Use this shell to try and fix any problems, then check again by exiting this shell"
echo "Use \`exit 1\` to abort the install"
bash || fail
key_file="$(find_key_file '/tilde/key')"
done
echo "Key file located at $key_file, testing connection..."
connection_test "$key_file" "$tilde_username" "$tilde_name"
echo "Configuring user..." echo "Configuring user..."
configure_user "$key_file" "$tilde_username" "$tilde_name" "$startx_command" || fail configure_user "$key_file" "$tilde_username" "$tilde_name" "$startx_command"
echo "User configured!" echo "User configured!"
echo "All done! Enjoy using Tildelinux!" echo "All done! Enjoy using Tildelinux!"

13
util.sh
View File

@ -11,6 +11,19 @@ is_substring() {
} }
fail() { fail() {
# TODO: make this exit cleanly, and from any subshell
echo "Something went wrong, exiting..." echo "Something went wrong, exiting..."
exit exit
} }
find_key_file() {
search_dir="$1"
for file in "$search_dir"
do
if cat $file | head -n1 | grep -q -- '-----BEGIN .* PRIVATE KEY-----')
then
echo "$file"
fi
done
}