linux - bash script fails when password contains backtick -


a colleague wrote script acts wrapper executing other scripts remotely. set ask password echo'ed remote systems use in sudo.

relevant code*:

read -s pw .... ${ssh_tt} ${host} "echo ${pw} | ${sudo_s} ./${script_name} > ${home}/${host_output}" 2> /dev/null 

* please ignore upper-case variables.

if enter password contains backtick following error:

bash: -c: line 0: unexpected eof while looking matching ``' bash: -c: line 1: syntax error: unexpected end of file 

if change password 1 not contain backtick script runs fine. imagine happen passwords contain single- , double-quotes well.

while changing password option not desirable due size of our platform (we don't use centralized authentication). i'm wondering if there way sanitize or otherwise escape backtick isn't interpreted shell either locally or remotely.

you can escape password before execute last line.

in bash:

escaped_pw=`printf "%q" ${pw}` 

then use escaped_pw in place of pw.

${ssh_tt} ${host} "echo ${escaped_pw} | ${sudo_s} ./${script_name} > ${home}/${host_output}" 2> /dev/null 

here's example :

$ read -s pw $ echo $pw ronak`gandhi $ escaped_pw=`printf "%q" ${pw}` $ echo $escaped_pw ronak\`gandhi $ ssh myhost "echo ${escaped_pw} - `date`" ronak`gandhi - thu jun 18 19:28:47 pdt 2015 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -