Last active
April 10, 2022 17:07
-
-
Save baerla/c9bfbffb4ab02bb0faccb7f7ad9a1a64 to your computer and use it in GitHub Desktop.
A bash script used for uploading a second script over ssh to a server and invoking it with given parameters. Using bash interal argument parsing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# set -e: Fail if some command exits with non-zero status | |
# set -u: Fail on use of uninitialized variable | |
set -eu | |
# Usage | |
if [[ $# -lt 2 ]]; then | |
echo "Usage: $0 -h|--host <host> -u|--user <username> -p|--password <password> [-c|--compression <'gz'|'zip'|'xz'>]" | |
exit 2 | |
fi | |
# Parsing options | |
ARGS=$(getopt --options 'h:u:pc:' --long 'host:,user:,password,compression:' -- "$@") | |
eval set -- "$ARGS" | |
unset ARGS | |
while true; do | |
case "$1" in | |
'-h'|'--host') | |
host="$2" | |
shift 2;; | |
'-u'|'--user') | |
user="$2" | |
shift 2;; | |
'-p'|'--password') | |
read -s -p 'Password: ' password | |
shift;; | |
'-c'|'--compression') | |
case "$2" in | |
'') | |
compression='xz';; | |
*) | |
compression="$2";; | |
esac | |
shift 2;; | |
'--') | |
shift | |
break;; | |
*) | |
printf "Unknown option %s among %s\n" "$1" "$*" | |
exit 1;; | |
esac | |
done | |
#echo "Host:$host" | |
#echo "User:$user" | |
#echo "Password:$password" | |
#echo "Compression:$compression" | |
exec 3<<< "$password" sshpass -d3 scp server.sh $user@$host:~/htdocs/ | |
exec 3<<< "$password" sshpass -d3 ssh $user@$host "./htdocs/server.sh $compression" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment