Ubuntu pass Password containing '"' to sudo - linux

I am trying to pass the password that contains " character as part of my password in a bash script to sudo -S su. I am running the bash script but I get prompt as bash error: line1 EOF as end to " not found.
Suggest some method to pass the password containing quotes successfully.
echo "pass\"word" | sudo -S su
prompt:
bash error: line1 EOF as end to '"' not found
I want to run the bash script without errors

This error is not caused by the quote in your password, but by the fact that both sudo and the shell spawned by su are trying to read from stdin:
$ bash -c 'pass"word'
bash: -c: line 1: unexpected EOF while looking for matching `"'
bash: -c: line 2: syntax error: unexpected end of file
$ echo 'totallynotmypassword' | sudo -S su
bash: line 1: totallynotmypassword: command not found
As suggested by user KamilCuk, it is better to create a NOPASSWD entry in your sudoers file.

Related

How can i modify a remote file over ssh with sed?

i am trying to add a line to the top of a JS file on a remote server
the command i want to run is:
sed -i "1i\const test = require(\'../../../test/test.json\');" /opt/test.js
so i have tried the following:
ssh user#host "sed -i "1i\const test = require(\'../../../test/test.json\');" /opt/test.js"
this gives me an error due to the "(" and ")" so i added a "\" before them:
ssh user#host "sed -i "1i\const test = require\(\'../../../test/test.json\'\);" /opt/test.js"
however i still get the error:
bash: -c: line 0: syntax error near unexpected token `('
how can i fix this?
Rather than use sed -i (which just manages a temp file behind the scenes), use ed and provide the script (via ssh) to ed's standard input.
ssh user#host 'ed /opt/test.js' <<'EOF'
1i
const test = require('../../../test/test.json');
.
wq
EOF

Bash - write to visudo

I want to write something to visduo and I can't write to /etc/sudoers
for SERVER in $(cat some.txt); do
ssh -q -o StrictHostKeyChecking=no root#$SERVER '
echo 'm2madm ALL=(ALL:ALL) NOPASSWD:/sbin/iptables -L' | sudo EDITOR='tee -a' visudo
'
problem is with '
but I really don't know what I should do, can you help me please?
the output is:
domain_check.sh: line 25: syntax error near unexpected token `('
domain_check.sh: line 25: `echo ('m2madm ALL=(ALL:ALL) NOPASSWD:/sbin/iptables -L' | sudo EDITOR='tee -a' visudo)'
If you want to use variables in the string you sent trough ssh, enclose that string with ". Within the string you should use a single quote ', or escape every double quote \".
Your problem occurs because you "close" the string before you're meaning to. The following should work.
for SERVER in $(cat some.txt); do
ssh -q -o StrictHostKeyChecking=no root#$SERVER "
echo 'm2madm ALL=(ALL:ALL) NOPASSWD:/sbin/iptables -L' | sudo EDITOR='tee -a' visudo
"
done
P.s. there is a shell spell check site in which you can test your code. I don't know which, but I will add it as a comment below my answer.

Escaping single quotes in shell for postgresql

I'm trying to execute SQK through psql under postgres account. I'm able run SQL
that doesn't contain quotes
root#server:/home/rosta/SCRIPTS# su postgres -c "psql -c 'SELECT NOW()'"
now
-------------------------------
2014-06-07 09:38:17.120368+02
(1 row)
The problem appears with an SQL query that contains quotes like SELECT 'hi'. I'm testing with simple 'hi', but I would like to execute something like this from a shell script.
su postgres -c "psql -c 'create database $DB_NAME template=template0 encoding='utf8' owner=aaa lc_collate='cs_CZ.utf8''"
Can anyone advise me how to escape quotes around encoding and collate in the command above
Some of my tests:
root#server:/home/rosta/SCRIPTS# su postgres -c "psql -c 'SELECT \'hi\''"
bash: -c: line 0: unexpected EOF while looking for matching `''
bash: -c: line 1: syntax error: unexpected end of file
root#server:/home/rosta/SCRIPTS# su postgres -c "psql -c 'SELECT \\'hi\\''"
bash: -c: line 0: unexpected EOF while looking for matching `''
bash: -c: line 1: syntax error: unexpected end of file
root#server:/home/rosta/SCRIPTS# su postgres -c 'psql -c \'SELECT \\'hi\\'\''
What I usually do is use double quotes (") for postgres -c's argument and escaped double quotes (\") for psql -c's argument. That way, I can use single quotes (') inside the SQL string with no problem:
[root#mycomputer ~]# su postgres -c "psql -c \"SELECT 'hi' \" "
?column?
----------
hi
(1 row)
Simplest way is to use a 'here document' , which ignores all quoting:
#!/bin/sh
DB_NAME=my_data_base
psql -U postgres postgres <<STOP_IT
create database $DB_NAME template=template0
encoding='utf8'
owner=aaa
lc_collate='cs_CZ.utf8'
;
STOP_IT

missing file operand cp after su

I run this command on ubuntu:
su - test -c cp /home/test/toto.txt /home/test/dir
But I have this error!
cp: missing file operand
anyone has an idea about the problem?
thank you
Option -c is understood by command su, taking the next argument (not the remaining) to be the command. That next command is just cp.
Try putting the command into quotes:
su - test -c 'cp /home/test/toto.txt /home/test/dir'
If this is problematic because you want to have quotes inside the command, try using escape instead of the inner quotes:
su - test -c 'echo hello\ \ there'

sed not working on remote machine

I'm trying to run a script on a remote server like so:
ssh root#cnc-02 'bash -c "
echo $SHELL;
cd /home/bldadmin/patch;
pwd;
echo '$int_ver_cnc';
echo '$rev_ver_cnc';
echo '$pre_ver_cnc';
cp -Rf RP_'$pre_ver_cnc'-'$int_ver_cnc' RP_'$int_ver_cnc'-'$rev_ver_cnc';
cd /home/bldadmin/patch/RP_'$int_ver_cnc'-'$rev_ver_cnc'/CSCONsap/data/twoway/manual;
rm rulePkg.zip;
mv cncrules-CNC60Test-1.48.0-1.49.0.zip rulePkg.zip;
cd /home/bldadmin/patch/RP_'$int_ver_cnc'-'$rev_ver_cnc';
find . -name install.sh.orig;
sed -e 's/^\(patchid=\)\(.*\)/\1\"1.47.0-1.48.0\"/g' -e 's/^\(fromVersion=\)\(.*\)/\1\"1.47.0\"/g' -e 's/^\(toVersion=\)\(.*\)/\1\"1.48.0\"/g' install.sh.orig >newfile.sh.orig;
"'
This is my script on my local machine. It will SSH to a remote machine and execute a list of commands. All the commands are executing but sed is not working. I'm getting the below error
"bash: -c: line 14: syntax error near unexpected token `(' bash: -c:
line 14: ` sed -e s/^(patchid=)(.*)/11.47.0-1.48.0/g -e
s/^(fromVersion=)(.*)/11.47.0/g -e s/^(toVersion=)(.*)/11.48.0/g
install.sh.orig >newfile.sh.orig;' "
The sed command when executed locally is working fine, I don't know what I am missing?
Instead of getting in a quoting headache save your script into a file script.sh
(use a sensible descriptive name) and run:
$ ssh root#cnc-02 'bash -s' < script.sh

Resources