The Shell lost the inverted comma in a nested ssh command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting The Shell lost the inverted comma in a nested ssh command
# 1  
Old 03-13-2019
The Shell lost the inverted comma in a nested ssh command

Hi,

i want use this Comand for my psql request


Code:
sh ssh -o StrictHostKeyChecking=no rootatemailaddress.de sudo psql -U postgres -c "select pg_terminate_backend(pid) from pg_stat_activity where datnam=\'$DB\';"'"



but the shell lost the inverted comma for datnam=\'$DB\'. The request deliver datnam=DB1 but for the psql request i need datnam='DB'. Where is my mistake? The psql comand need the inverted comma left and right from the $DB variable.

Last edited by RudiC; 03-13-2019 at 07:17 AM..
# 2  
Old 03-13-2019
Quite involved a remote command. I can't replicate your entire setup, but for a simplified command, dropping the sh from the front, and removing the '" from the end made it work. Give it a try?
# 3  
Old 03-14-2019
Could you try these variations?:-
Code:
ssh -o StrictHostKeyChecking=no rootatemailaddress.de 'sudo psql -U postgres -c "select pg_terminate_backend(pid) from pg_stat_activity where datnam=\'$DB\';"'"'       # Wrapping it all in single quotes should pass the double quotes, but you may need to escape the single quotes within the double quotes
ssh -o StrictHostKeyChecking=no rootatemailaddress.de 'sudo psql -U postgres -c \'select pg_terminate_backend(pid) from pg_stat_activity where datnam=$DB;\''             # backslash and two single quotes at the end
ssh -o StrictHostKeyChecking=no rootatemailaddress.de "sudo psql -U postgres -c \"select pg_terminate_backend(pid) from pg_stat_activity where datnam=\"\$DB\";\""       # backslash and two double quotes at the end, escaping the $ for the DB name
ssh -o StrictHostKeyChecking=no rootatemailaddress.de "sudo psql -U postgres -c \"select pg_terminate_backend(pid) from pg_stat_activity where datnam=\\\"\$DB\\\";\""     # extra escaping so that the quoted string passed ends up as \"$DB\" to the remote shell.
ssh -o StrictHostKeyChecking=no rootatemailaddress.de "sudo psql -U postgres -c \"select pg_terminate_backend(pid) from pg_stat_activity where datnam=\"\$DB\";\""       # Which seems to work okay if I insert an echo before the sudo (because I don't have postgress installed on my local machine.

They are variations to try passing quotes and/or quoted text. I presume that you want to pick up the value of $DB from the remote machine rather than the local machine.

Does that get you anywhere? If you want to just dummy run it, add an echo just before the sudo within the quotes (of whatever type) and it should show you what it would run on the remote machine. Where do you pick up the vale of $DB? Is that set in the profile of the account you are connecting to? Should we worry that the sudo command has no password requirement?



I hope that this helps,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check if file exists via ssh in ssh (nested)

I'm using redhat and have an odd issue with a nested ssh call. ssh -i ~/.ssh/transfer-key -q transfer@fserver1 ] && ssh -i ~/.ssh/transfer-key transfer@fserver1 "ssh -i ~/.ssh/sftp-key sftpin@10.0.0.1 ]" && ssh -i ~/.ssh/transfer-key transfer@fserver1 "scp -i ~/.ssh/sftp-key /home/S/outbox/*... (2 Replies)
Discussion started by: say170
2 Replies

2. Shell Programming and Scripting

Using shell command need to parse multiple nested tag value of a XML file

I have this XML file - <gp> <mms>1110012</mms> <tg>988</tg> <mm>LongTime</mm> <lv> <lkid>StartEle=ONE, Desti = Motion</lkid> <kk>12</kk> </lv> <lv> <lkid>StartEle=ONE, Source = Velocity</lkid> <kk>2</kk> </lv> <lv> ... (3 Replies)
Discussion started by: NeedASolution
3 Replies

3. Shell Programming and Scripting

Shell script for ssh command listening

Hi, I'm trying to write a shell script checks if there is ssh command listening on port 1080 at loop back interface. If there is, just exit nicely with exit code 0. If not, start command: #ssh -D 1080 smsuser@10.76.172.23 ping -i 60 localhost The ssh keys needs to be generated and... (2 Replies)
Discussion started by: Kijana_kenn
2 Replies

4. Shell Programming and Scripting

Attempting to pass my array as a nested parameter to loop. A little lost.

I want to pass this array as a parameter. IFS=$'\n' fortune_lines=($(fortune | fold -w 30 )) Inside of this line screen -p 0 -S ${SCREEN_SESSION} -X stuff "`printf "say ${fortune_lines}\r"`" And I am lost at this point. I am thinking something like this? Then make it loop.. ... (7 Replies)
Discussion started by: briandanielz
7 Replies

5. Shell Programming and Scripting

issue on ssh command in ksh shell

Hi guru, I'm making crazy cause an issue on a ksh shell I made. In this shell I want to execute unix command on a remote machine using an ssh connection like ssh user@host 'command'..... The command is very simply, is an ls on a remote directory but it give me an unexpected result. The... (4 Replies)
Discussion started by: leobdj
4 Replies

6. Shell Programming and Scripting

Data pipe lost when using ssh in shell script

Hi, I want to do SSH on many different machines and then run some commands on them. A binary application randomly generates IP addresses and my script will take care of doing SSH. $ ./IPGen.exe | ./myScript.sh my script looks like this: while read line; do result1=$(ssh $line... (2 Replies)
Discussion started by: siavash
2 Replies

7. Shell Programming and Scripting

Shell Script with ssh command

How do I use the ssh command to connect to another server without the password prompt? I use: ssh user@host and it prompts for the password. how do I include the password in the ssh command? alternatively, how do you execute 1 command from server A on server B? thanks, ... (4 Replies)
Discussion started by: toughlittleone
4 Replies

8. Shell Programming and Scripting

Shell script with wget in ssh command

Hi, I am using a linux with bash. I have a script written which will login to a remote server and from there it runs a "wget" to downlaod a build file from a webserver. Here is the line inside the script: ssh -t -q -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@${a}'wget... (4 Replies)
Discussion started by: sunrexstar
4 Replies

9. Shell Programming and Scripting

Help with ssh command in shell script

Hi All, I am using ssh in my shell script. Can any one please suggest me option so that i can avoid the login message as below in the execution: NOTE: Please note that you have logged into the newer version of server "gabbro" ******* Performing functions to this computer withe the... (6 Replies)
Discussion started by: vikash_k
6 Replies

10. Shell Programming and Scripting

difference between double inverted coma and single inverted comma

Whats the basic difference between double inverted comma and single inverted comma and no comma applied at all? Eg1 if Eg2 if iEg3 f (1 Reply)
Discussion started by: abhisekh_ban
1 Replies
Login or Register to Ask a Question