Help with quotes in query with ssh and su


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with quotes in query with ssh and su
# 1  
Old 03-23-2010
Help with quotes in query with ssh and su

Hi

I have to run a query in a remote postgresql server but I'm having troubles with quotes, what I need to do is:

Login to remote server
su as cpanel user
execute the query

The following code works well if USERH is a numerical value, the problem is went USERH is a string (example $USERH="user") I know is because I have to wrap $USER in single quotes but I don't know how to do it I tried several ways and quotes get lost each time
Code:
#!/bin/bash
HSPHERE_SERVER="xx.xx.xx.xx"
USERH=11111
CMD1='psql -t hsphere wwwuser -c "select * from unix_user where id = %'$USERH'% ;"'
QUERY=  `ssh root@$HSPHERE_SERVER "su - cpanel -c '$CMD1'"`
echo "Result: "$QUERY

This one fails:
Code:
#!/bin/bash
HSPHERE_SERVER="xx.xx.xx.xx"
USERH="user"
CMD1='psql -t hsphere wwwuser -c "select * from unix_user where login =  %'$USERH'% ;"'
QUERY=  `ssh root@$HSPHERE_SERVER "su - cpanel -c '$CMD1'"`
echo "Result: "$QUERY

Any ideas?

Thanks

Last edited by Datacenter1; 03-23-2010 at 06:12 PM..
# 2  
Old 03-23-2010
try this:
Code:
#!/bin/bash
HSPHERE_SERVER='xx.xx.xx.xx'
USERH=11111
CMD1="psql -t hsphere wwwuser -c 'select * from unix_user where id = %$USERH% ;'"
QUERY="ssh root@$HSPHERE_SERVER 'su - cpanel -c '$CMD1'"
echo "Result: "$QUERY

# 3  
Old 03-23-2010
Quote:
Originally Posted by vgersh99
try this:
Code:
#!/bin/bash
HSPHERE_SERVER='xx.xx.xx.xx'
USERH=11111
CMD1="psql -t hsphere wwwuser -c 'select * from unix_user where id = %$USERH% ;'"
QUERY="ssh root@$HSPHERE_SERVER 'su - cpanel -c '$CMD1'"
echo "Result: "$QUERY

Thank You but doesn't work
output:
Code:
Result: ssh root@xx.xx.xx.xx 'su - cpanel -c 'psql -t hsphere wwwuser -c 'select * from unix_user where id = %user% ;''

Is no executing the command
# 4  
Old 03-23-2010
Code:
CMD1="psql -t hsphere wwwuser -c \"select * from unix_user where id = '%$USERH%';\""

assuming the %s should be inside the single quotes.
# 5  
Old 03-23-2010
Thank you but it doesn't work

I don't need '%' in the query

What i need is:
Login to a postgresql server from remote server using ssh as user root
su cpanel
as cpanel user run the postgresql query

From Postgresql server as user cpanel this code works:
psql -t hsphere wwwuser -c "select * from unix_user where login = 'username';"

From same server as user root

this works
su -l cpanel -c 'psql -t hsphere wwwuser -c "select * from unix_user where id = 1;"'

this doesn't work
su -l cpanel -c 'psql -t hsphere wwwuser -c "select * from unix_user where login = 'username';"'

What I need is to make the last code works from an execute from a remote server
# 6  
Old 03-23-2010
then get rid of the two %

Last edited by binlib; 03-23-2010 at 11:30 PM..
# 7  
Old 03-23-2010
No luck

Code:
#!/bin/bash
HSPHERE_SERVER="xx.xx.xx.xx"
USERH="username"
CMD1="psql -t hsphere wwwuser -c \"select * from unix_user where login = '$USERH';\""
QUERY=  `ssh root@$HSPHERE_SERVER "su - cpanel -c '$CMD1'"`
echo "Result: "$QUERY

The output is: ERROR: column "username" does not exist
Result:

The query is ok, i can get results from postgresql

Thank You
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Query regarding PuTTY SSH Tunneling

I have a process running on my local server. http://dev.techx.com:6060/proct I wish to block port 6060 and expose port 7777 to the outside world. I block port 6060 and open port 7777 on the firewall. What should be the PuTTY Settings -> Connection -> SSH -> Tunnels 1. Destination ... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Run sql query after ssh in UNIX

I am running this test.ksh on server1. It successfully logins to server2 but runs the queries of query.sql on server1. query.sql is present in both server1 and server2 Can anybody please help. I need to run queries on server2 itself.:confused: Below is the test script... (10 Replies)
Discussion started by: shruthimithra
10 Replies

3. Shell Programming and Scripting

Query on executing awk using SSH

Hi All, Im trying to find the count of process running on remote server using SSH. Below command dosen't work. ssh -q user@host "ps -ef | grep "pattern" | grep -v 'grep' | awk '{print $2}'|wc -l" But below command works. ssh -q user@host "ps -ef | grep "pattern" | grep -v... (1 Reply)
Discussion started by: Girish19
1 Replies

4. Shell Programming and Scripting

Query related to the SSH key pairs

Hi All, I have question , How to generate the SSH keys for the two servers in the pair. So if the server will be replicated , same SSH key pairs will work on each server. No need to generate the SSH keys for the second server. I have created the SSH keys for the single server. I was... (1 Reply)
Discussion started by: sharsour
1 Replies

5. Shell Programming and Scripting

query in using Double Quotes

Hi i am referring a book for understanding the Double Quotes book suggest for the below code code echo "$USER owes <-$1250.**>; " the o/p isFred owes <-250.**>; however when i ran the same command i got below o/piscpadm owes <-250.**>; need to know why i am not getting the... (8 Replies)
Discussion started by: scriptor
8 Replies

6. Shell Programming and Scripting

run query in shell script after ssh

Hi, I need to run sql query in shell script after getting connected to ssh. For that I connected to ssh through shell script using RSA keys done]. Now when I am running sql query, it's not working... but several other commands like 'ls'. 'mkdir', etc are working properly. Here is my code: ... (2 Replies)
Discussion started by: shekhar2010us
2 Replies

7. Solaris

[Solved] SSH Query differences Solaris 9/10

Hi, I am trying to SSH without a password from a Solaris 10 box to a Solaris 9 box. I am not using root user but file permissions seem to be fine... Can anyone tell me why this might not be working?! I can successfully SSH without a password from the Solaris 9 box to the Solaris 10 box,... (0 Replies)
Discussion started by: mcclunyboy
0 Replies

8. Shell Programming and Scripting

Query regarding ssh keygen

Hi, I have two Unix servers A and B. I have a script in server A. I want to connect to server B from A using ssh only and without giving passwords everytime i connect. I went through other posts regarding this and I generated a public key in server A and copied that in server B. Now when I... (3 Replies)
Discussion started by: mick_000
3 Replies

9. Shell Programming and Scripting

ssh query

hi guys!! i am writing a script in the middle of which i do ssh to some machine, for eg: ssh root@1.1.1.1 this prompt me for password.. since i know the password of the machine, is there anyway by which i can provide password to ssh command? so that i am not prompted for password. ... (3 Replies)
Discussion started by: allrise123
3 Replies

10. Shell Programming and Scripting

Double quotes or single quotes when using ssh?

I'm not very familiar with the ssh command. When I tried to set a variable and then echo its value on a remote machine via ssh, I found a problem. For example, $ ITSME=itsme $ ssh xxx.xxxx.xxx.xxx "ITSME=itsyou; echo $ITSME" itsme $ ssh xxx.xxxx.xxx.xxx 'ITSME=itsyou; echo $ITSME' itsyou $... (3 Replies)
Discussion started by: password636
3 Replies
Login or Register to Ask a Question