Skip to content

Instantly share code, notes, and snippets.

@mkf
Last active September 21, 2019 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkf/5130300d060a72b0ea05d01925d89395 to your computer and use it in GitHub Desktop.
Save mkf/5130300d060a72b0ea05d01925d89395 to your computer and use it in GitHub Desktop.
passh, that is: sshpass -d {anonymous pipe from executing $1 with args from the rest of argv}
#!/usr/bin/env bash
# Created by Michał Krzysztof Feiler in September 2019
# Desired usage example:
# ./passh.sh \; ssh user@host \; pass stdout path/of/passfile
# ( this example uses https://github.com/mkf/pass-stdout )
# ( this script aims to serve this example. so far it is a failure. please help. )
shopt -s expand_aliases
alias narg='[ "$#" -eq 0 ]'
if narg; then
>&2 echo 'Lacking arguments. First argument is supposed to be the delimiter, such as "\;".'
exit 1; fi
DELIMITER="$1"; shift
alias delim='[ "$1" == "$DELIMITER" ]'
if narg || delim; then
>&2 echo 'Lacking the result command. It is supposed to be the first, delimited by the delimiters.'
exit 1; fi
RESULT_COMMAND=("$1"); shift
until narg || delim; do
RESULT_COMMAND+=("$1"); shift; done; delim && shift
if narg || delim; then
>&2 echo 'Lacking the password source command. It is supposed to be the second, delimited by the delimiters.'
exit 1; fi
SOURCE_COMMAND=("$1"); shift
until narg || delim; do
SOURCE_COMMAND+=("$1"); shift; done; delim && shift
SSHPASS_PARAMS=("sshpass")
if ! narg; then
SSHPASS_PARAMS=("$1")
until narg || delim; do
SSHPASS_ARGS+=("$1"); shift; done; delim && shift
if ! narg; then
>&2 echo "The following actual parameters do not match any of the formal positional parameters of this script:"
(( eae=0 )); until narg; do (( eae++ ))
>&2 echo "$eae" "$1"; shift; done
fi; fi
coproc THE_SOURCE { "${SOURCE_COMMAND[@]}" | head -n 1 ; }
exec "${SSHPASS_PARAMS[0]}" -d "${THE_SOURCE[1]}" "${SSHPASS_PARAMS[@]:1}" "${RESULT_COMMAND[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment