bash script using sed command on remote server with ssh and special characters - bash

I am trying to run this sed command on a remote server through a bash script but I can not get the escaping correct.
ssh -t user#ip "sed -i -e '/<!--insert_new_code-->/i\ <div class="col-lg-4 col-md-6">\n <?php include "''explorers/'"${coin_symbol_lower}"'.php''";?>\n </div>\n\n' /home/some-data/some_dir/dir/dir/index.php"
I have the spaces in there to keep the html code looking clean. The sed command works fine when ran locally, just not over ssh.
Any help getting it working would be appreciated

The command passed to ssh is delimited by double quotes. Since there are also double quotes within the string being passed to sed, they will need to be escaped to prevent the shell from trying to take them:
ssh -t user#ip "sed -i -e '/<!--insert_new_code-->/i\ <div class=\"col-lg-4 col-md-6\">\n <?php include \"''explorers/'\"${coin_symbol_lower}\"'.php''\";?>\n </div>\n\n' /home/some-data/some_dir/dir/dir/index.php"

Try this syntax
ssh -t user#ip << \EOF
sed -i -e '/<!--insert_new_code-->/i\ <div class="col-lg-4 col-md-6">\n <?php include "''explorers/'"${coin_symbol_lower}"'.php''";?>\n </div>\n\n' /home/some-data/some_dir/dir/dir/index.php
EOF

Related

Bash script placing quotes around command

I have a Bash script that will get an IP to use as part of an SSH tunnel, but running this script the SSH tunnel fails. When using set -x I can see it places the arguments to the SSH command in single quotes and manually running this line results in the same error.
The Script:
ssh -N -L 9000:${ip_array[$2]}:443 ssh-server
The first argument is used elsewhere in the script for something else which is why the second is used here. ssh-server is an alias in my SSH config to the server i am tunneling through.
The output I get is:
ssh -N -L '9000:"172.0.0.1":443' ssh-server
Could this be because the script to fetch the IP returns strings to the array?
you can try removing the double-quotes first :
ip=$(echo "${ip_array[$2]}" | sed "s/\"//g")
ssh -N -L 9000:${ip}:443 ssh-server
Or just use shell parameter expansion to remove the quotes:
ssh -N -L 9000:${ip_array[$2]//"/}:443 ssh-server
That lone double quote may mess up your editor's syntax highlighting.
Get rid of the quotes by piping it through the tr command:
ssh -N -L 9000:$( echo ${ip_array[$2]} | tr -d '"' ):443 ssh-server

Using SED in a ssh command on a remote node

I wrote a script to ssh to some nodes and run a sed command inside the node. The script looks like
NODES="compute-0-3"
for i in $NODES
do
echo $i
ssh $i 'sed -i \'s/172.16.48.70/172.20.54.10/g\' /etc/hosts;'
done
However, the error is
unexpected EOF while looking for matching `''
syntax error: unexpected end of file
It seems that the character ' is not treated as the begining of a sed command.
I suggest to replace
ssh $i 'sed -i \'s/172.16.48.70/172.20.54.10/g\' /etc/hosts;'
by
ssh "$i" 'sed -i "s/172.16.48.70/172.20.54.10/g" /etc/hosts'
If you absolutely want to use single quotes:
ssh "$i" 'sed -i '"'"'s/172.16.48.70/172.20.54.10/g'"'"' /etc/hosts'

running a pipe command with variable substitution on remote host

I'd to run a piped command with variable substitution on a remote host and redirect the output. Given that the login shell is csh, I have to used "bash -c". With help from users nlrc and jerdiggity, a command with no variable substitution can be formulated as:
localhost$ ssh -f -q remotehost 'bash -c "ls /var/tmp/ora_flist.sh|xargs -L1 cat >/var/tmp/1"'
but the single quote above will preclue using variable substitution, say, substituting ora_flist.sh for $filename. how can I accomplish that?
Thanks.
Something like this should work:
ssh -f -q remotehost 'bash -c "ls /var/tmp/ora_flist.sh|xargs -L1 cat >/var/tmp/1"'
So your problem was that you want the shell variable to be extended locally. Just leave it outside the single quotes, e.g.
ssh -f -q remotehost 'bash -c "ls '$filename' | xargs ..."'
Also very useful trick to avoid the quoting hell is to use heredoc, e.g.
ssh -f -q remotehost <<EOF
bash -c "ls $filename | xargs ... "
EOF

Passing link parameters via sed

I'm trying to insert an image link into a file for a wiki on a remote server.
ssh root#10.10.10.1 "sed -i -e '1i'[^http://10.10.10.2/image.jpg^]'\'" /var/www/wiki/page
Works but I need to add the resize parameter after the file name but it doesn't work, how do I account for the spaces?
ssh root#10.10.10.1 "sed -i -e '1i'[^http://10.10.10.2/image.jpg height480 width=640^]'\'" /var/www/wiki/page
sed: can't read height480: No such file or directory
sed: can't read width=640^]\: No such file or directory
I'm not sure why you're quoting only the '1i'. You should quote the entire sed expression, if you have spaces in it. Try this:
ssh root#10.10.10.1 "sed -i -e '1i[^http://10.10.10.2/image.jpg height480 width=640^]'" /var/www/wiki/page
Better use heredoc here to avoid crazy escaping and other issues like spaces in command:
ssh -t -t root#10.10.10.1 <<'EOF'
sed -i '1i[^http://10.10.10.2/image.jpg height480 width=640^]' /var/www/wiki/page
exit
EOF

Deleting word from line on remote server

I am trying to delete a word from bash profile on remote server but the command never come out.
ssh -X test_server 'sed -e \'s/unalias ls//g\' -i .bash_profile' </dev/null
Not sure what i am doing wrong, Kindly assist.
It's not possible to nest single quotes in shell, switching to double quotes should give you the desired result. E.g.:
$ ssh somehost sh <<< 'echo "Xunalias lsX" | sed -e "s/unalias ls//"'
XX

Resources