Sunday 14 July 2019

bash - quoting and escaping of a ssh command line

the following command looks properly quoted and escaped:


$  echo -e ssh -q rmthost 'bash -c "ps -ef|grep '\'''\\\<'defunct'\\\>''\''|grep -v grep|awk '\'''{print \$1}''\'' "'
ssh -q sbpsvrwm757 bash -c "ps -ef|grep '\'|grep -v grep|awk '{print $1}' "

but when executed, the result is not what is expected:


$   ssh -q rmthost 'bash -c "ps -ef|grep '\'''\\\<'defunct'\\\>''\''|grep -v grep|awk '\'''{print \$1}''\'' "'
root16986 16982 0 - ? 0:01

the awk command didn't seem to extract the right column.


Now I followed the link provided by Siguza and came up with:


$  echo ssh -q rmthost "'"bash -c '"ps -ef|grep '"'\'|grep -v grep|awk '{print \$1}'"'"'"'"
ssh -q rmthost 'bash -c "ps -ef|grep '\'|grep -v grep|awk '{print $1}'"'

that looks perfectly escaped too, but still doesn't work:


$  ssh -q rmthost  "'"bash -c '"ps -ef|grep '"'\'|grep -v grep|awk '{print \$1}'"'"'"'"
Missing }

or,


$  echo ssh -q rmthost  bash -c '"ps -ef|grep '"'\'|grep -v grep|awk '{print \$1}'"'"'
ssh -q rmthost bash -c "ps -ef|grep '\'|grep -v grep|awk '{print $1}'"
$ ssh -q rmthost bash -c '"ps -ef|grep '"'\'|grep -v grep|awk '{print \$1}'"'"'
root 16986 16982 0 - ? 0:01

or,


$  echo ssh -q rmthost  "bash -c \"ps -ef |grep '\'|grep -v grep| awk '{print \$1}'\""
ssh -q rmthost bash -c "ps -ef |grep '\'|grep -v grep| awk '{print $1}'"
$ ssh -q rmthost "bash -c \"ps -ef |grep '\'|grep -v grep| awk '{print \$1}'\""
root 16986 16982 0 - ? 0:01

any idea why?


Please note that I need bash -c to enable bash-style output redirection like >/var/tmp/1.ouy 2>&1 for the command.


your help is appreciated.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...