This repository has been archived on 2024-04-19. You can view files and clone it, but cannot push or open issues or pull requests.
sostools/sos-ssh.sh

450 lines
17 KiB
Bash
Raw Normal View History

2022-08-15 13:48:16 +02:00
#!/bin/bash
function varCleanup() {
export "SOSSSH_PASSWD="
}
function setConstant() {
export "SOSSSH_VERSION=dev3"
2022-08-15 13:48:16 +02:00
}
function getDefaultConfiguration() {
# SOSSSH_CONFIGVERSION=1
# # sos-ssh configuration\n
# # this is the default configuration, you\n
# # may make changes in sos-ssh.conf.d\n
# # since they may get overriten in a future\n
# # update.\n
# \n
# # Commands\n
# ## SSH\n
# ## DEFAULT: /bin/ssh\n
# #SOSSSH_SSH=/bin/ssh\n
# \n
# ## SSHpass\n
# ## DEFAULT: /bin/sshpass\n
# #SOSSSH_SSHPASS=/bin/sshpass\n
# #\n
# ## Editor\n
# ## DEFAULT: /bin/nano\n
# ## You may want to set this to /bin/vi\n
# ## or /bin/vim or any other editor.\n
# #SOSSSH_EDITOR=/bin/nano\n
# #\n
# ## cURL\n
# ## DEFAULT: /bin/curl\n
# #SOSSSH_CURL=/bin/curl\n
# #\n
# ## ls\n
# ## DEFAULT: /bin/ls\n
# ## The ls command must be from GNU\n
# ## coreutils, otherwise it may not\n
# ## work correctly with sos-ssh.\n
# #SOSSSH_LS=/bin/ls\n
# #\n
# ## rm\n
# ## DEFAULT: /bin/rm\n
# ## The rm command must be from GNU\n
# ## coreutils, otherwise it may not\n
# ## work correctly with sos-ssh.\n
# #SOSSSH_RM=/bin/rm\n
# #\n
# ## mkdir\n
# ## DEFAULT: /bin/mkdir\n
# ## The mkdir command must be from\n
# ## GNU coreutils, otherwise it may\n
# ## not work correctly with sos-ssh.\n
# #SOSSSH_MKDIR=/bin/mkdir\n
# #\n
# ## mv\n
# ## DEFAULT: /bin/mv\n
# ## The mv command must be from GNU\n
# ## coreutils, otherwise it may not\n
# ## work correctly with sos-ssh.\n
# #SOSSSH_MV=/bin/mv\n
# #\n
# ## chmod\n
# ## DEFAULT: /bin/chmod\n
# ## The chmod command must be from\n
# ## GNU coreutils, otherwise it may\n
# ## not work correctly with sos-ssh.\n
# #SOSSSH_CHMOD=/bin/chmod\n
# #\n
# ## ping\n
# ## DEFAULT: /bin/ping\n
# ## The ping command used to\n
# ## check if the server is online.\n
# #SOSSSH_PING=/bin/ping\n
# \n
# # Title\n
# ## Suppress\n
# ## DEFAULT: false\n
# ## This hides the branding at\n
# ## startup completely.\n
# #SOSSSH_TITLE_SUPPRESS=false\n
# #\n
# ## Text\n
# ## DEFAULT: false\n
# ## This prints the branding\n
# ## at startup in normal text.\n
# #SOSSSH_TITLE_TEXT=false\n
# \n
# # Temp\n
# # DEFAULT: /dev/shm\n
# # The temporary directory.\n
# # NO TRAILING SLASH AT THE END!\n
# #SOSSSH_TMP=/dev/shm\n
# \n
# # Internet\n
# ## Update URL\n
# ## DEFAULT: https://staropensource.ddns.net\n
# ## /git/staropensource/sostools\n
# ## /raw/branch/develop/sos-ssh.sh
# ## The update URL, from where sos-ssh gets\n
# ## updates. The save location will be\n
# ## determined from the command line arguments.\n
# #SOSSSH_UPDATEURL=https://staropensource.ddns.net/git/staropensource/sostools/raw/branch/develop/sos-ssh.sh\n
# #\n
# ## SSH host\n
# ## DEFAULT: staropensource.ddns.net\n
# ## The SSH host. If left empty, the\n
# ## setup process will ask for it.\n
# #SOSSSH_HOST=staropensource.ddns.net\n
# #\n
# ## SSH port\n
# ## DEFAULT: 22\n
# ## The SSH port. If left empty, the\n
# ## setup process will ask for it.\n
# ## INFO: Currently ignored!\n
# #SOSSSH_PORT=22\n
# #\n
# ## SSH username\n
# ## DEFAULT: root\n
# ## The SSH username. If left empty, the\n
# ## setup process will ask for it.\n
# #SOSSSH_USERNAME=root\n
# #\n
# ## SSH password\n
# ## DEFAULT:\n
# ## The SSH password. If left empty, the\n
# ## setup process will ask for it.\n
# #SOSSSH_PASSWD=\n
export "SOSSSH_EXITSTRING=SOSSSH_CONFIGVERSION=1\n# sos-ssh configuration\n# this is the default configuration, you\n# may make changes in sos-ssh.conf.d\n# since they may get overriten in a future\n# update.\n\n# Commands\n## SSH\n## DEFAULT: /bin/ssh\n#SOSSSH_SSH=/bin/ssh\n\n## SSHpass\n## DEFAULT: /bin/sshpass\n#SOSSSH_SSHPASS=/bin/sshpass\n#\n## Editor\n## DEFAULT: /bin/nano\n## You may want to set this to /bin/vi\n## or /bin/vim or any other editor.\n#SOSSSH_EDITOR=/bin/nano\n#\n## cURL\n## DEFAULT: /bin/curl\n#SOSSSH_CURL=/bin/curl\n#\n## ls\n## DEFAULT: /bin/ls\n## The ls command must be from GNU\n## coreutils, otherwise it may not\n## work correctly with sos-ssh.\n#SOSSSH_LS=/bin/ls\n#\n## rm\n## DEFAULT: /bin/rm\n## The rm command must be from GNU\n## coreutils, otherwise it may not\n## work correctly with sos-ssh.\n#SOSSSH_RM=/bin/rm\n#\n## mkdir\n## DEFAULT: /bin/mkdir\n## The mkdir command must be from\n## GNU coreutils, otherwise it may\n## not work correctly with sos-ssh.\n#SOSSSH_MKDIR=/bin/mkdir\n#\n## mv\n## DEFAULT: /bin/mv\n## The mv command must be from GNU\n## coreutils, otherwise it may not\n## work correctly with sos-ssh.\n#SOSSSH_MV=/bin/mv\n#\n## chmod\n## DEFAULT: /bin/chmod\n## The chmod command must be from\n## GNU coreutils, otherwise it may\n## not work correctly with sos-ssh.\n#SOSSSH_CHMOD=/bin/chmod\n#\n## ping\n## DEFAULT: /bin/ping\n## The ping command used to\n## check if the server is online.\n#SOSSSH_PING=/bin/ping\n\n# Title\n## Suppress\n## DEFAULT: false\n## This hides the branding at\n## startup completely.\n#SOSSSH_TITLE_SUPPRESS=false\n#\n## Text\n## DEFAULT: false\n## This prints the branding\n## at startup in normal text.\n#SOSSSH_TITLE_TEXT=false\n\n# Temp\n# DEFAULT: /dev/shm\n# The temporary directory.\n# NO TRAILING SLASH AT THE END!\n#SOSSSH_TMP=/dev/shm\n\n# Internet\n## Update URL\n## DEFAULT: https://staropensource.ddns.net\n## /git/staropensource/sostools\n## /raw/branch/develop/sos-ssh.sh## The update URL, from where sos-ssh gets\n## updates. The save location will be\n## determined from the command line arguments.\n#SOSSSH_UPDATEURL=https://staropensource.ddns.net/git/staropensource/sostools/raw/branch/develop/sos-ssh.sh\n#\n## SSH host\n## DEFAULT: staropensource.ddns.net\n## The SSH host. If left empty, the\n## setup process will ask for it.\n#SOSSSH_HOST=staropensource.ddns.net\n#\n## SSH port\n## DEFAULT: 22\n## The SSH port. If left empty, the\n## setup process will ask for it.\n## INFO: Currently ignored!\n#SOSSSH_PORT=22\n#\n## SSH username\n## DEFAULT: root\n## The SSH username. If left empty, the\n## setup process will ask for it.\n#SOSSSH_USERNAME=root\n#\n## SSH password\n## DEFAULT:\n## The SSH password. If left empty, the\n## setup process will ask for it.\n#SOSSSH_PASSWD=\n"
return "0"
}
function loadDefaultConfiguration() {
export "SOSSSH_CONFIGVERSION=1"
export "SOSSSH_SETUP_USER=false"
export "SOSSSH_SETUP_SYSTEM=false"
export "SOSSSH_SSH=/bin/ssh"
export "SOSSSH_SSHPASS=/bin/sshpass"
export "SOSSSH_EDITOR=/bin/nano"
export "SOSSSH_CURL=/bin/curl"
export "SOSSSH_LS=/bin/ls"
export "SOSSSH_RM=/bin/rm"
export "SOSSSH_MKDIR=/bin/mkdir"
export "SOSSSH_CHMOD=/bin/chmod"
export "SOSSSH_PING=/bin/ping"
export "SOSSSH_TITLE_SUPPRESS=false"
export "SOSSSH_TITLE_TEXT=false"
export "SOSSSH_TMP=/dev/shm"
export "SOSSSH_UPDATEURL=https://staropensource.ddns.net/git/staropensource/sostools/raw/branch/develop/sos-ssh.sh"
export "SOSSSH_HOST=staropensource.ddns.net"
export "SOSSSH_PORT=22"
export "SOSSSH_USERNAME=root"
export "SOSSSH_PASSWD="
}
function loadSystemConfigurationMigrationError() {
echo "Your system configuration file is too old, but can't be reset."
echo ""
echo "If you want to ignore loading the system configuration,"
echo "create a empty file at $HOME/.sostools/ssh/nosysconf"
varCleanup
exit "249"
}
function loadSystemConfiguration() {
if [ -f "$HOME/.sostools/ssh/nosysconf" ] || [ -d "$HOME/.sostools/ssh/nosysconf" ]; then
return
else
if [ -f "/etc/sostools/ssh/sos-ssh.conf" ]; then
export "SOSSSH_EXITSTRING=$SOSSSH_CONFIGVERSION"
source "/etc/sostools/ssh/sos-ssh.conf"
if [ ! "$SOSSSH_CONFIGVERSION" == "$SOSSSH_EXITSTRING" ]; then
if [ "$(whoami)" == "root" ]; then
$SOSSSH_RM -rf "/etc/sostools/ssh/sos-ssh.conf" &> /dev/null
if [ ! "$?" == "0" ]; then loadSystemConfigurationMigrationError; fi
if [ -f "/etc/sostools/ssh/sos-ssh.conf" ] || [ -d "/etc/sostools/ssh/sos-ssh.conf" ]; then loadSystemConfigurationMigrationError; fi
getDefaultConfiguration
echo -e "$SOSSSH_EXITSTRING" &> /etc/sostools/ssh/sos-ssh.conf
echo "Your system configuration file is too old and has been reset."
varCleanup
exit "250"
else
loadSystemConfigurationMigrationError
fi
fi
else
if [ "$(whoami)" == "root" ]; then
getDefaultConfiguration
echo -e "$SOSSSH_EXITSTRING" &> /etc/sostools/ssh/sos-ssh.conf
fi
fi
if [ -d "/etc/sostools/ssh/sos-ssh.conf.d/" ]; then
for file in $(/bin/ls -Aw 1 "/etc/sostools/ssh/sos-ssh.conf.d/");do
source "/etc/sostools/ssh/sos-ssh.conf.d/${file}"
done
fi
fi
}
function loadUserConfiguration() {
if [ -f "$HOME/.sostools/ssh/sos-ssh.conf" ]; then
export "SOSSSH_EXITSTRING=$SOSSSH_CONFIGVERSION"
source "$HOME/.sostools/ssh/sos-ssh.conf"
if [ ! "$SOSSSH_CONFIGVERSION" == "$SOSSSH_EXITSTRING" ]; then
$SOSSSH_RM "$HOME/.sostools/ssh/sos-ssh.conf" &> /dev/null
getDefaultConfiguration
echo "$SOSSSH_EXITSTRING" &> "$HOME/.sostools/ssh/sos-ssh.conf"
echo "Your user configuration file is too old and has been reset."
varCleanup
exit "251"
fi
fi
if [ -d "$HOME/.sostools/ssh/sos-ssh.conf.d/" ]; then
for file in $(/bin/ls -Aw 1 "$HOME/.sostools/ssh/sos-ssh.conf.d/");do
source "$HOME/.sostools/ssh/sos-ssh.conf.d/${file}"
done
fi
}
function doUserSetup() {
if [ "$1" == "force" ] || [ "$1" == "force+noinfo" ] || [ ! "$SOSSSH_SETUP_USER" == "true" ]; then
if [ "$1" == "force+noinfo" ] || [ "$1" == "noinfo" ]; then
echo -n ""
else
echo "Writing user configuration"
fi
$SOSSSH_MKDIR "$HOME/.sostools/ssh/sos-ssh.conf.d" -p
getDefaultConfiguration
echo -e "$SOSSSH_EXITSTRING" &> $HOME/.sostools/ssh/sos-ssh.conf
echo -e "# DO NOT DELETE OR MODIFY THIS FILE\nSOSSSH_SETUP_USER=true" &> $HOME/.sostools/ssh/sos-ssh.conf.d/setup.conf
$SOSSSH_CHMOD "+x" "$HOME/.sostools/ssh/sos-ssh.conf" "$HOME/.sostools/ssh/sos.git.conf.d/*" &> /dev/null
if [ "$1" == "force+noinfo" ] || [ "$1" == "noinfo" ]; then
echo -n ""
else
echo "Applying new user configuration"
fi
loadUserConfiguration
fi
}
function doSystemSetup() {
if [ "$1" == "force" ] || [ "$1" == "force+noinfo" ] || [ ! "$SOSSSH_SETUP_SYSTEM" == "true" ] && [ "$(whoami)" == "root" ]; then
if [ "$1" == "force+noinfo" ] || [ "$1" == "noinfo" ]; then
echo -n ""
else
echo "Writing system configuration"
fi
$SOSSSH_MKDIR "/etc/sostools/ssh/sos-ssh.conf.d" -p
getDefaultConfiguration
echo -e "$SOSSSH_EXITSTRING" &> /etc/sostools/ssh/sos-ssh.conf
$SOSSSH_CHMOD "+x" "/etc/sostools/ssh/sos-ssh.conf" "/etc/sostools/ssh/sos.git.conf.d/*" &> /dev/null
echo -e "# DO NOT DELETE OR MODIFY THIS FILE\nSOSSSH_SETUP_SYSTEM=true" &> /etc/sostools/ssh/sos-ssh.conf.d/setup.conf
if [ "$1" == "force+noinfo" ] || [ "$1" == "noinfo" ]; then
echo -n ""
else
echo "Applying new system configuration"
fi
loadSystemConfiguration
fi
}
function chmodConfiguration() {
$SOSSSH_CHMOD +x "$HOME/.sostools/ssh/sos-ssh.conf" &> /dev/null
for file in $($SOSSSH_LS --color=never -w 1 "$HOME/.sostools/ssh/sos-ssh.conf.d"); do
$SOSSSH_CHMOD +x "$HOME/.sostools/ssh/sos-ssh.conf.d/$file" &> /dev/null
done
if [ -d "/etc/sostools/ssh/" ] && [ "$(whoami)" == "root" ]; then
$SOSSSH_CHMOD +x "/etc/sostools/ssh/sos-ssh.conf" &> /dev/null
for file in $($SOSSSH_LS --color=never -w 1 "/etc/sostools/ssh/sos-ssh.conf.d"); do
$SOSSSH_CHMOD +x "/etc/sostools/ssh/sos-ssh.conf.d/$file" &> /dev/null
done
fi
}
loadDefaultConfiguration
chmodConfiguration
loadSystemConfiguration
loadUserConfiguration
doSystemSetup
doUserSetup
setConstant
function argumentMissing() {
echo "ERROR: Missing arguments."
varCleanup
exit 6
}
function collectFiles() {
echo "$(pwd)" &> $SOSSSH_TMP/SOSSSH_collectfiles_pwd
cd "$1"
export "SOSSSH_EXITSTRING="
for file in $(ls --color=never -w 1 "$1"); do
if [ "$SOSSSH_EXITSTRING" == "" ]; then
export "SOSSSH_EXITSTRING=$(ls --color=never -w 1 $(pwd)/$file)"
else
export "SOSSSH_EXITSTRING= $(ls --color=never -w 1 $(pwd)/$file)"
fi
done
cd "$(cat ${SOSSSH_TMP}/SOSSSH_collectfiles_pwd)"
rm -rf "${SOSSSH_TMP}/SOSSSH_collectfiles_pwd"
return
}
function doSSHSetup() {
echo "SSHsetup"
if [ "$SOSSSH_HOST" == "" ]; then
echo -n "Hostname: "
else
echo -n "Hostname [$SOSSSH_HOST]: "
fi
read -r SOSSSH_ENTRY_HOST
if [ "$SOSSSH_PORT" == "" ]; then
echo -n "Port: "
else
echo -n "Port [$SOSSSH_PORT]: "
fi
read -r SOSSSH_ENTRY_PORT
if [ "$SOSSSH_USERNAME" == "" ]; then
echo -n "Username: "
else
echo -n "Username [$SOSSSH_USERNAME]: "
fi
read -r SOSSSH_ENTRY_USERNAME
if [ "$SOSSSH_PASSWD" == "" ]; then
echo -n "Password: "
else
echo -n "Password [<censored>]: "
fi
read -r -s SOSSSH_ENTRY_PASSWD
if [ "$SOSSSH_ENTRY_HOST" == "" ]; then
export "SOSSSH_ENTRY_HOST=$SOSSSH_HOST"
fi
if [ "$SOSSSH_ENTRY_PORT" == "" ]; then
export "SOSSSH_ENTRY_PORT=$SOSSSH_PORT"
fi
if [ "$SOSSSH_ENTRY_USERNAME" == "" ]; then
export "SOSSSH_ENTRY_USERNAME=$SOSSSH_USERNAME"
fi
if [ "$SOSSSH_ENTRY_PASSWD" == "" ]; then
export "SOSSSH_ENTRY_PASSWD=$SOSSSH_PASSWD"
fi
echo ""
echo "Host: $SOSSSH_ENTRY_HOST"
echo "Port: $SOSSSH_ENTRY_PORT"
echo "Username: $SOSSSH_ENTRY_USERNAME"
if [ "$SOSSSH_ENTRY_PASSWD" == "" ]; then
echo "Password:"
else
echo "Password: <censored>"
fi
2022-08-15 13:48:16 +02:00
echo -n "Is this information correct [y/N]? "
read -r SOSSSH_ENTRY_CONFIRMATION
if [ "$SOSSSH_ENTRY_CONFIRMATION" == "y" ] || [ "$SOSSSH_ENTRY_CONFIRMATION" == "Y" ]; then
export "SOSSSH_ENTRY_PASSWD="
2022-08-15 13:48:16 +02:00
echo -e "# This file has been generated by sos-ssh by using setup\nSOSSSH_HOST=$SOSSSH_ENTRY_HOST\nSOSSSH_PORT=$SOSSSH_ENTRY_PORT\nSOSSSH_USERNAME=$SOSSSH_ENTRY_USERNAME\nSOSSSH_PASSWD=$SOSSSH_ENTRY_PASSWD" &> "$HOME/.sostools/ssh/sos-ssh.conf.d/sshsetup.conf"
else
export "SOSSSH_ENTRY_PASSWD="
2022-08-15 13:48:16 +02:00
echo "Cancelled."
fi
}
if [ ! "$SOSSSH_TITLE_SUPPRESS" == "true" ]; then
if [ ! "$SOSSSH_TITLE_TEXT" == "true" ]; then
# _
# ___ ___ ___ ___ ___| |__
#/ __|/ _ \/ __|_____/ __/ __| '_ \
#\__ \ (_) \__ \_____\__ \__ \ | | |
#|___/\___/|___/ |___/___/_| |_|
echo " _"
echo " ___ ___ ___ ___ ___| |__"
echo "/ __|/ _ \/ __|_____/ __/ __| '_ \\"
echo "\__ \ (_) \__ \_____\__ \__ \ | | |"
echo "|___/\___/|___/ |___/___/_| |_|"
echo ""
else
echo -e "sos-ssh\n"
fi
fi
if [ "$1" == "" ]; then
echo "Use \"sos-ssh help\" for all commands."
echo "If you want more information, use \"sos-ssh helpextend\"."
varCleanup
exit 1
elif [ "$1" == "help" ]; then
echo "<required> [optional]"
echo "sos-ssh connect"
echo " editconf <system>"
echo " help"
echo " helpextend"
echo " selfupdate"
echo " setup"
echo " version"
varCleanup
exit 0
elif [ "$1" == "helpextend" ]; then
echo "<required> [optional]"
echo "command | args | description"
echo "connect | | connect to the server"
echo "editconf | <system> | edit the configuration"
echo "help | | lists all commands"
echo "helpextend | | lists all commands with extended information"
echo "selfupdate | | update sos-ssh"
echo "setup | | restart sos-ssh setup"
echo "version | | returns version string"
varCleanup
elif [ "$1" == "connect" ]; then
if [ "$SOSSSH_HOST" == "" ] || [ "$SOSSSH_PORT" == "" ] || [ "$SOSSSH_USERNAME" == "" ] || [ "$SOSSSH_PASSWD" == "" ]; then
echo "ERROR: Credentials are missing. Please run sos-ssh setup."
varCleanup
exit "154"
fi
ping -c 1 "$SOSSSH_HOST" &> /dev/null
if [ ! "$?" == "0" ]; then
echo "ERROR: You have no connection or \"$SOSSSH_HOST\" is not online."
varCleanup
exit "153"
fi
$SOSSSH_SSHPASS "-p" "$SOSSSH_PASSWD" $SOSSSH_SSH "$SOSSSH_USERNAME@$SOSSSH_HOST"
varCleanup
elif [ "$1" == "editconf" ]; then
if [ "$2" == "" ]; then
argumentMissing
fi
if [ "$2" == "true" ]; then
if [ "$(whoami)" == "root" ]; then
collectFiles "/etc/sostools/ssh/sos-ssh.conf.d/"
echo "Opening system configuration files in $SOSSSH_EDITOR"
$SOSSSH_EDITOR "/etc/sostools/ssh/sos-ssh.conf" $SOSSSH_EXITSTRING
varCleanup
else
echo "ERROR: You aren't root."
varCleanup
exit "2"
fi
elif [ "$2" == "false" ]; then
collectFiles "$HOME/.sostools/ssh/sos-ssh.conf.d/"
echo "Opening user configuration files in $SOSSSH_EDITOR"
$SOSSSH_EDITOR "$HOME/.sostools/ssh/sos-ssh.conf" $SOSSSH_EXITSTRING
varCleanup
else
echo "ERROR: Invalid boolean."
varCleanup
exit "214"
fi
elif [ "$1" == "selfupdate" ]; then
varCleanup
echo -e "echo Updating sos-ssh to \"$0\";$SOSSSH_CURL -sSLqq \"$SOSSSH_UPDATEURL\" &> $0;kill -9 $$" &> $SOSSSH_TMP/sos-ssh-updater.sh
chmod +x $SOSSSH_TMP/sos-ssh-updater.sh
$SOSSSH_TMP/sos-ssh-updater.sh
elif [ "$1" == "setup" ]; then
doSSHSetup
varCleanup
elif [ "$1" == "version" ]; then
echo "sos-ssh $SOSSSH_VERSION"
echo "config $SOSSSH_CONFIGVERSION"
varCleanup
else
argumentMissing
varCleanup
fi