sed over ssh doesn't escape backslash - linux

I'm trying to add a line before "rezhome" in the file as it would look like below:
...
app3-reservation, \
app4-reservation, \
app5-reservation, \
rezhome
When I run locally with below command it works fine.
sed -i 's/rezhome/app5-reservation, \\\n&/' grouphost.cfg
But using ssh backslash is not escaped and I get below result
ssh localhost "sed -i 's/rezhome/app5-reservation, \\\n&/' /path/grouphost.cfg"
...
app3-reservation, \
app4-reservation, \
app5-reservation, \nrezhome
Any help please?

The \ gets processed by both the sed and the ssh so you'll need to further escape them to use it with both. Try:
ssh localhost "sed -i 's/rezhome/app5-reservation, \\\\\n&/' /path/grouphost.cfg"

Related

Using escape characters inside double quotes in ssh command in bash script

I want to run some commands each time when I log in to a remote system. Storing commands in .bashrc on remote is not an option.
What is the proper way to escape the escape chars inside of quotes in bash script for ssh?
How can I write each command in new line?
My script
#!/bin/bash
remote_PS1=$'\[\033[01;32m\]\u#\[\033[03;80m\]\h\[\033[00m\]:\[\033[01;34m\]\!:\w\[\033[00m\]\$ '
ssh -t "$#" 'export SYSTEMD_PAGER="";' \
'export $remote_PS1;' \
'echo -e "set nocompatible" > /home/root/.vimrc;' \
'bash -l;'
didn't work.
Escaping escape characters inside double-quotes and run them on remote server is way too complicated for me :)
Instead, I wrote a remoterc file for remote and a small remotessh script.
In remotessh, first I copy remoterc on remote machine and run bash command with that remoterc file interactively.
remoterc:
#!/bin/bash
SYSTEMD_PAGER=""
PS1="\[\033[01;32m\]\u#\[\033[03;80m\]\h\[\033[00m\]:\[\033[01;34m\]\!:\w\[\033[00m\]\$ "
echo -e "set nocompatible" > /home/root/.vimrc
remotessh:
#!/bin/bash
scp remoterc "$1":/home/root/
ssh "$1" -t "bash --rcfile remoterc -i"
It works :)
You can use Bash's printf %q.
According to help printf:
%q      quote the argument in a way that can be reused as shell input
See the following example:
$ cat foo.sh
ps1='\[\033[1;31m\]\u:\w \[\033[0m\]\$ '
ps1_quoted=$( printf %q "$ps1" )
ssh -t foo#localhost \
'export FOO=bar;' \
"export PS1=$ps1_quoted;" \
'bash --norc'
Result:

How to execute multiple commands on remote host connected using dual SSH connection?

I have two remote server and i am trying to execute more than one command on a remote host to which i am connected by using ssh command. My command syntax is like:
ssh -t -i key-1 user1#remote-1 "ssh -t -i key-2 user2#remote-2 "cmd-1;cmd-2;...cmd-n;"";
I have tried using semi-colon (;) and && symbols between two commands and observed that first command executes on remote-2 and second command executes on remote-1.
My requirement is that I want to execute all commands except the last one on remote-2. And, the last command on remote-1.
Note: I know how to execute multiple commands by connecting to single remote host. So, I will appreciate if answer is given only after understanding the problem statement.
Assuming that you cannot create config files to simplify your command on the two hosts, and that you do not want to split this into two separate commands, it should be as simple as moving the last command out of the inner SSH command:
ssh -t -i key-1 user1#remote-1 "ssh -t -i key-2 user2#remote-2 \"cmd-1;cmd-2;...cmd-n-1\"; cmd-n"
# or
ssh -t -i key-1 user1#remote-1 'ssh -t -i key-2 user2#remote-2 "cmd-1;cmd-2;...cmd-n-1"; cmd-n'
You should also escape your double nested quotes, or simply change your outer/inner quotes to single quotes providing you are not expanding within this command.
Also as an aside, you can simplify your SSH command greatly by using a .ssh_config file, specifically with the ProxyJump parameter (man page).
Instead of single-line, consider taking advantage of here-documents to simplify the sequence. It has the advantage of making it easier to enter long commands.
ssh -t -i key-1 user1#remote-1 <<__END__
ssh -t -i key-2 user2#remote-2 "cmd-1;cmd-2;..."
cmd-n
__END__
I'm not able to test locally, but should be possible to nest further
ssh -t -i key-1 user1#remote-1 <<__END__
ssh -t -i key-2 user2#remote-2 <<__SUB__
# Execute on remote2
cmd-1
cmd-2
...
__SUB__
# Execute on remote1
cmd-n
__END__

Rasa stack commands

I have got an error , error: unrecognized arguments: \ , while running below commands in Rasa stack bot bulding.
python -m rasa_core.run -d models/dialogue -u models/nlu/current \
--port 5002 --credentials credentials.yml
\ is used on Linux terminals to break up a command in multiple lines, e.g.:
copy file1.txt \
file2.txt
It seems that your terminal does not recognise \ properly. Are you using Windows as operating system?
On Windows you have to use ^ to break it in multiple lines, e.g copy file1.txt file2.txt becomes:
copy ^
file1.txt ^
file2.txt
If this also does not work you can also remove the \ and have the command in one line:
python -m rasa_core.run -d models/dialogue -u models/nlu/current --port 5002 --credentials credentials.yml
\ is used as notation for continuation of a command in Linux. Usually used when the command is very long, so that command is readable. In windows, you can execute without the \ character, it should work

How can you run an ldap search using variables and a not filter?

ldapsearch -h 1.0.24.24 -p 389 -x -t -LLL -S cn -D
cn=user,ou=resources,o=otherresource,c=xx -w server101 -b ou=Non-
Staff,ou=people,o=test,c=us '(&(objectClass=inetOrgPerson)
(createTimestamp<=$month_8)(!(nEApps=*))(nEDHHSNFAccNbr=\00)
(nECreatedBy=cioSelfRegistered)(loginTime<=$month_3))' dn
I can only use my variables if the filters are surrounded by double quotes
and I can only use the not filter if they are surround by single quotes is there a way to make this work in the same query?
! is a special character (it does a command history substitution) in interactive mode, but not in a script. Is this supposed to be something you'll type in interactively, or something embedded in a script? If the latter, you can just use double-quotes, and the ! won't be a problem.
If you're using this interactively, there are a coupe of options. One is to mix quoting modes in a single argument , as in "double-quoted-section"'single-quoted-section'"another-quoted-section". This looks weird, but works fine. Something like this:
ldapsearch -h 1.0.24.24 -p 389 -x -t -LLL -S cn \
-D cn=user,ou=resources,o=otherresource,c=xx -w server101 \
-b ou=Non-Staff,ou=people,o=test,c=us \
"(&(objectClass=inetOrgPerson)(createTimestamp<=$month_8)("'!'"(nEApps=*))(nEDHHSNFAccNbr=\00)(nECreatedBy=cioSelfRegistered)(loginTime<=$month_3))" dn
^^^^^
Alternately, you could define a variable as ! (using single-quotes), then use that variable inside double-quotes:
exclamation='!'
ldapsearch -h 1.0.24.24 -p 389 -x -t -LLL -S cn \
-D cn=user,ou=resources,o=otherresource,c=xx -w server101 \
-b ou=Non-Staff,ou=people,o=test,c=us \
"(&(objectClass=inetOrgPerson)(createTimestamp<=$month_8)($exclamation(nEApps=*))(nEDHHSNFAccNbr=\00)(nECreatedBy=cioSelfRegistered)(loginTime<=$month_3))" dn
^^^^^^^^^^^^
The ! filter only 'needs' single quotes because of the *, which the shell will attempt to expand itself as a wildcard.
Solution: escape it.

SED in remote SUDO ssh script

I am trying to disable RHN check when running yum on 1000 servers. It is done by:
Editing this file /etc/yum/pluginconf.d/rhnplugin.conf
[main]
enabled = 0
I wrote a script to do this remotely. We are using individual accounts and I need to execute this command using SUDO:
for HOST in $(cat serverlist ) ; do echo $HOST; ssh -o ConnectTimeout=5 -oStrictHostKeyChecking=no $HOST -t 'sudo cp /etc/yum/pluginconf.d/rhnplugin.conf /etc/yum/pluginconf.d/rhnplugin.$(date +%F) ; sudo sed -i -e "s/1/0/g" /etc/yum/pluginconf.d/rhnplugin.conf ' ; done
I know it is a long line but why does it not work?
All individual commands work on their own
sudo cp /etc/yum/pluginconf.d/rhnplugin.conf /etc/yum/pluginconf.d/rhnplugin.$(date +%F)
sudo sed -i -e "s/1/0/g" /etc/yum/pluginconf.d/rhnplugin.conf
have tried escaping the special chars:
sudo sed -i -e "s\/1\/0\/g" /etc/yum/pluginconf.d/rhnplugin.conf
But I get an error all the time:
sed: -e expression #1, char 1: unknown command: `?'
Thanks for your help.
The sudo(1) command expects a pseudo-teletype (pty) and fails if it does not see one. Rewrite your command line to use su(1) instead. Use your local sudo(1) configuration to limit access to this script so only the select few can execute the script.
I actually found the answer to this question, or rather workaround. See the snippet below, where I got to -as root- ssh as me (szymonri) to other host, then invoke sed command as root in order to edit /etc/hosts file. All thanks to base64 magic.
ME=`echo -e "$(hostname -I | awk '{print $1}')\toverlord"`
B64ENC=`echo "sed -i 's/.*overlord/$ME/g' /etc/hosts" | base64`
su - szymonri sh -c "ssh jetson bash -c \\\"echo $B64ENC \| base64 --decode \| sudo bash \\\""
line: I"m obtaining m yown IP address as an /etc/hosts line
line: I'm base64 encoding sed command with the first line in it.
line: I'm invoking the SSH shenannigan, where I su as regular user, ssh to another box as the user, and use power of sudo to edit the file.

Resources