I am trying to read a password from stdin
into a variable in a Bash script. This variable should then be expanded into an ssh-command and transmitted to the server. There, the password should arrive in correctly escaped manner, so it can be used as input for another command. (In this case to change the login-password of my pihole).
Currently I have something like:
read -rs -p "Password: `echo $'\n> '`" newpass
ssh root@gate "pihole -a -p \"${newpass}\""
But also tried:
ssh root@gate "pihole -a -p ""'""${newpass}""'"
ssh root@gate 'pihole -a -p '"${newpass}"
ssh root@gate 'pihole -a -p '"${newpass}"''
ssh root@gate "pihole -a -p \'${newpass}\'"
ssh root@gate 'pihole -a -p ''\''"${newpass}"'\''
Even with printf:
IFS= read -rs -p "Password: `echo $'\n> '`" newpass
command=$(printf 'pihole -a -p %s\n' "$newpass")
or
IFS= read -rs -p "Password: `echo $'\n> '`" newpass
command=$(printf 'pihole -a -p '\''%s'\''\n' "$newpass")
and many more. But with this password: a$#5!6k?h'v;z'
they all fail. And there isn't even a back-tick in it, yet...
My question to all you Bash Gurus out there is:
What is the correct way to parse a password into a variable, where the user may enter literally any character?
This includes:
$
!
` (backtick)
' (single quote)
"
;
#
@
\
/
~
\n
and so on...
I have read all the related questions here and in other forums, but couldn't find a solution for this use case.
I know, that there will be people who suggest to use Python or C or something else, but still I'm interested in Bash's capabilities.
However, if it definitely isn't possible to accomplish with Bash, then I'm also interested in ideas on how you would do this (cleanly and securely) with other tools.
Thanks in advance
IFS= read -r
does. The problem you have is not with that, but with safely passing an arbitrary string to another shell's command line (via SSH)./proc
withhidepid=2
prevent the issue of users peaking at other users' processes?