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"'
Related
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?
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
For a simple example:
ssh user#ip echo " messages"
this output like:
messages
not the expected(with heading spaces):
messages
and the heading spaces are skipped, how to keep these spaces within the returned output?
It is because ssh accepts only single command argument. If you pass more than one, all of them are passed through bash -c "command", which basically removes all the additional spaces from additional arguments. Workaround can be
ssh user#ip 'echo " messages"'
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 am trying to create a patch that users can use to remotely edit a file in a pre-defined way using sed, and I could do this manually on each computer, but it would take a long time.
The line I am struggling with is as follows:
host=[hostname]
port=[portnum]
ssh -t $host -p $port "cp ~/file1 ~/file1.bak ; sed -i \"s/fcn1('param1', $2)\n/fcn2('param2'):$zoom\n/g\" ~/file1"
This makes a backup of file1 and then edits a line in the file. I actually want to edit more than one line, but this line demonstrates the problems:
The command works, provided no $ signs are used within the sed command.
I have tried a number of ways of escaping these $ signs but cannot seem to find one that works.
I can use a . wildcard in the find, but obviously not in the replace string.
I would use single quotes for the sed command, in order to avoid expanding the $2, but single quotes are already used inside the command.
Does anyone have any ideas of how to overcome this problem? Thanks in advance for any suggestions!
This should work as well:
ssh -t $host -p $port "cp ~/file1 ~/file1.bak && sed -i \"s/fcn1('param1', \\\$2)/fcn2('param2'):\\\$zoom/g\" file1"
You need 3 back slashes as you have to escape the $ sign in the string passed in the remote bash to sed. And you have to escape that back slash and the $ sign when sending it over via ssh.