Deleting word from line on remote server - shell

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

Related

ssh bash with whitespace?

Can anyone explain, please, why this command echoes an empty line instead of "abc"? I'm stuck with this. I know there are multiple ways of reaching the same goal but please also explain why this command does not print "abc" and why it's wrong.
ssh 127.0.0.1 bash -c "echo abc"
You effectively lose a level of quoting when you execute commands via ssh. You would need to write instead:
ssh 127.0.0.1 'bash -c "echo abc"'
Without those outside quotes, the command you're running on the remote system is:
bash -c echo abc
Here you're running the command echo, with $0 set to abc.

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

Resources