All,
I have not been able to escape single quotes while writing a file to a remote node.
ssh -i demo.pem -t ec2-user#10.10.10.10 'echo '\''{"watches": [{"type": "key","key": "test","handler": "neon -e 'sudo /opt/watch_handler.sh'"}]}'\''| sudo tee /etc/key.json'
The output I get is as follows.
{"watches": [{"type": "key","key": "test","handler": "neon -e sudo /opt/watch_handler.sh"}]}
I would like the output to have single quotes around 'sudo /opt/watch_handler.sh'
{"watches": [{"type": "key","key": "test","handler": "neon -e 'sudo /opt/watch_handler.sh'"}]}
'\' is not working.
Could you please help.
Thanks,
Getting multiple levels of quoting correct is troublesome and error-prone. Consider alternative solutions, such as:
cat <<EOF | ssh -i demo.pem -t ec2-user#10.10.10.10 sudo tee /etc/key.json
{"watches": [{"type": "key","key": "test","handler": "neon -e 'sudo /opt/watch_handler.sh'"}]}
EOF
I like using cat because it does not require any escaping to work. However, you can also generate the string locally using echo instead of cat as long as you escape the double quotes in your JSON expression:
echo "{\"watches\": [{\"type\": \"key\",\"key\": \"test\",\"handler\": \"neon -e 'sudo /opt/watch_handler.sh'\"}]}" | sudo tee /etc/key.json
Related
Is there a way to add gitlab variables to the command ?
eg: variables: ARTIFACTORY_ADDRESS: "a.com"
script:
sshpass -p "password" ssh -o "StrictHostKeyChecking=no" user#SERVER 'echo $ARTIFACTORY_ADDRESS'
Currently its not taking the value from the variable and printing $ARTIFACTORY_ADDRESS in the console. I want the value to be printed in the console
Check first if using double-quotes would help enabling variable substitution:
sshpass -p "password" ssh -o "StrictHostKeyChecking=no" user#SERVER \
"echo $ARTIFACTORY_ADDRESS"
^^^ ^^^
I wanted to change the title of the window using the command as described here over SSH, however I kept get getting the error:
033]sh: Hello: command not found
Connection to host closed.
with the command:
ssh.exe user#host -t 'echo -en "\033];Hello World\007"'
No matter how I try to escape them, it seems to somehow return error. Tried:
ssh.exe user#host -t 'echo -en "\\033];Hello World\\007"'
ssh.exe user#host -t "echo -en \'\\033];Hello World\\007\'"
Any idea how to fix this?
I'm trying to search some string on some files but I didn't find the good combination.
I want to use this command but I have an error in my syntax
grep -r -H "<?php $GLOBALS[" /var/www/vhosts/
I want to search (via SSH) the string <?php $GLOBALS[ on the files under /var/www/vhosts
Use this:
ssh user#host "grep -r -H '<?php \$GLOBALS\[' /var/www/vhosts/"
I'm trying to find and replace with variables, but it doesn't work.
Here is the code. I need to append -C -w 10% -c 5% -p /u0 to append to the end of a matching line. I do not know how to suppress the (-) Any ideas? Thank you.
OLD=$(command[check_disk]=/usr/local/nagios/libexec/check_disk -w 10% -c 5% -p / -p /var -p /tmp -p /home -p /boot -p /usr -A -e)
NEW=$(command[check_disk]=/usr/local/nagios/libexec/check_disk -w 10% -c 5% -p / -p /var -p /tmp -p /home -p /boot -p /usr -A -e -C -w 10% -c 5% -p /u0)
sed -i "s/$OLD/$NEW/" /home/scripts/nrpe.cfg
Try this (assumes bash):
OLD='command[check_disk]=/usr/local/nagios/libexec/check_disk -w 10% -c 5% -p / -p /var -p /tmp -p /home -p /boot -p /usr -A -e'
NEW='command[check_disk]=/usr/local/nagios/libexec/check_disk -w 10% -c 5% -p / -p /var -p /tmp -p /home -p /boot -p /usr -A -e -C -w 10% -c 5% -p /u0'
oldEscaped=$(sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$OLD")
newEscaped=$(sed 's/[\\&/]/\\&/g' <<<"$NEW")
sed -i "s/$oldEscaped/$newEscaped/" /home/scripts/nrpe.cfg
Your first problem was that you mistook $(...) for a string-quoting mechanism (it is not; it's used for command substitution (executing the enclosed command and replacing the construct with the command's output)).
To assign literal strings, simply use single quotes as above.
Your second problem was that you can't blindly pass strings to sed's s (string-substitution) command, because certain characters have special meaning to sed, so to use them literally they have to be escaped - the most obvious problem being the / instances in the strings, which get mistaken for the delimiters of the s/.../.../ command.
Therefore, 2 auxiliary sed commands are used to perform the requisite escaping:
sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$OLD" escapes the old string so that none of its characters can be mistaken for the regex delimiter or special regular-expression characters.
sed 's/[\\&/]/\\&/g' <<<"$NEW" escapes the new string so that none of its characters can be mistaken for the regex delimiter or backreferences (such as &, or \1).
Finally, note that it's better not to use all-uppercase shell variable names such as $OLD, so as to avoid conflicts with environment variables.
I'm trying to write several commands trought ssh connection bue I got problem with escape characters. Below an example of what I'd like to do:
/usr/bin/ssh mrtg#172.20.29.40 echo -e "ciao\nprova"
I got this result:
ciaonprova
instead of:
ciao
prova
if I use -e option for ssh:
/usr/bin/ssh -e mrtg#172.20.29.40 echo -e 'ciao\nprova'
I receive this error:
Bad escape character 'mrtg#172.20.29.40'.
Can someone give me a suggestion to let remote server interpret escape characters?
The -e option has nothing to do with your command (these are SSH escape characters, not shell).
You can just put your command in quotes:
/usr/bin/ssh mrtg#172.20.29.40 'echo -e "ciao\nprova"'