LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-12-2011, 04:18 AM   #1
Droa
Member
 
Registered: Oct 2011
Location: ~
Distribution: Debian 7
Posts: 60

Rep: Reputation: Disabled
Bash script: su another user and ssh in the single-command


Hey guys, this is my first post to this forum, i heard good things about it, and its one of the best sites to get help nowdays

i have this problem i wrote a large post about, but gotten a "bad gateway error, so i have to rewrite it all again

well thats why it looks a little short, as i kinda got anoyed by that :/

here is my problem, i have a bash script i want to do ssh with another user, so i use the

Code:
#!/usr/local/bin/bash
#FreeBSD 8.1 GENERIC
USER="test"
RUSER="test"
RHOST="test.tld"
as_user(){
  su - $USER -c "bash -c \"$1\"" 
}

ssh_cmd(){
  as_user "ssh -q -q -o 'BatchMode=yes' -o 'ConnectTimeout 15' -p 20002 ${RUSER}@${RHOST} \"$1\""
}

ssh_cmd "hostname"
ssh_cmd "mysql -uroot -p1234 -Bse \"SHOW DATABASES\""
exit 0
however, seems to not work, anyone got sugesstions?

hope i didnt sound too impolite, i am realy a nice guy :P
 
Old 10-13-2011, 01:17 AM   #2
corp769
Guru
 
Registered: Apr 2005
Posts: 5,814

Rep: Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001
Hello,

What part of it doesn't work? The first thing I have noticed is the very first line; I know on most systems, bash is installed to /bin/bash. What distro are you running?

Cheers,

Josh
 
Old 10-13-2011, 02:32 AM   #3
Droa
Member
 
Registered: Oct 2011
Location: ~
Distribution: Debian 7
Posts: 60

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by corp769 View Post
Hello,

What part of it doesn't work? The first thing I have noticed is the very first line; I know on most systems, bash is installed to /bin/bash. What distro are you running?

Cheers,

Josh
i included the header to inform
as i outcommendted, it is FreeBSD 8.1.

but it seems when i try to use a mysql command is gives an mysql error wit the
No databases by that name: database

as it seems to cut of the "Show databases" with "show" and then adds Databases as the database to use.

so it simply breaks my Quotes

i tryed with \\\"Show databases\\\"
seems to just open a whole other can of worms
 
Old 10-13-2011, 02:53 AM   #4
corp769
Guru
 
Registered: Apr 2005
Posts: 5,814

Rep: Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001
Oops, didn't even notice.....

As far as the message you are getting about the database, I would run it by itself just to double check the format, and to make sure that it works before using it within your script. You could also store it within a variable so it doesn't complain about too many quotation marks.
 
Old 10-13-2011, 03:14 AM   #5
Droa
Member
 
Registered: Oct 2011
Location: ~
Distribution: Debian 7
Posts: 60

Original Poster
Rep: Reputation: Disabled
it works like this

Code:
#!/usr/local/bin/bash
#FreeBSD 8.1 GENERIC

as_user(){
  su - $USER -c "$1"
}

ssh_cmd(){
 as_user "ssh -q -q -o 'BatchMode=yes' -o 'ConnectTimeout 15' -p 20002 ${RUSER}@${RHOST} \"$1\""
}

ssh_cmd "hostname"
ssh_cmd "mysql -u${MYSQLUSER} -p${MYSQLPASS} -Bse \\\"SHOW DATABASES\\\""
exit 0
however, is there no way to make the quotes more simple? or do i have to escape escape every time i go deeper?
 
Old 10-13-2011, 03:19 AM   #6
corp769
Guru
 
Registered: Apr 2005
Posts: 5,814

Rep: Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001
You could always use single quotes for the actual command;
Code:
ssh_cmd 'mysql -u${MYSQLUSER} -p${MYSQLPASS} -Bse "SHOW DATABASES"'
I'm not on linux, so I can't test this. To my knowledge, this should work for you.
 
Old 10-13-2011, 03:59 AM   #7
Droa
Member
 
Registered: Oct 2011
Location: ~
Distribution: Debian 7
Posts: 60

Original Poster
Rep: Reputation: Disabled
i tried playing around with single quotes, it seems to give problems with variables, so i outquoted them


ssh_cmd 'mysql -u'"${MYSQLUSER}"' -p'"${MYSQLPASS}"' -Bse "SHOW DATABASES"'

seems it cuts them off, even when i try echoing from my functions, and the come out all fine.. i guess the main problem is the switch from as_user <-> ssh_cmd
as the command first gets evaluated from as_user.. hmm im still trying to dig down, but maby i am just doing it wrong, and a person already found a better way to do it like this? :P


actually why i am doing this, is becasue i need to access a users id_dsa file for private ssh connection.. however i figured i could just get it from ssh by adding -i /home/${USER}/.ssh/id_dsa to my ssh command.. it works better.. but still i wanted to get my as_user function to work for later use.
 
Old 10-13-2011, 04:33 AM   #8
Droa
Member
 
Registered: Oct 2011
Location: ~
Distribution: Debian 7
Posts: 60

Original Poster
Rep: Reputation: Disabled
yay, i got it working.. the problem was the "bash -c" can't work within another with double-quotes, it have to be single-quotes.. as my full string is assembled by that point, it doesn't matter anyway.. so my fix was this.

Code:
dtest="echo \"Hello World\""

as_user(){
  su - $USER -c "bash -c '$1'"
}

ssh_cmd(){
   as_user "ssh -q -q -o \"BatchMode=yes\" -o \"ConnectTimeout 15\" -p 20002 ${RUSER}@${RHOST} \"$1\""
}

ssh_cmd "hostname"
ssh_cmd 'mysql -u'"${MYSQLUSER}"' -p'"${MYSQLPASS}"' -Bse \"SHOW DATABASES\"'
ssh_cmd "${dtest}"
exit 0
note i had to end the single-quotes, to add a variable, and i still need to escape out of double quotes, but i got reduced the escaping alot :P

thanks for the help corp769

Last edited by Droa; 10-13-2011 at 04:35 AM.
 
Old 10-13-2011, 04:34 AM   #9
corp769
Guru
 
Registered: Apr 2005
Posts: 5,814

Rep: Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001
No problem! And sorry I didn't see the notification at first, I got kind of busy here at work, and overlooked it. Glad to see you got it working!

Cheers,

Josh
 
1 members found this post helpful.
Old 10-13-2011, 04:43 AM   #10
Droa
Member
 
Registered: Oct 2011
Location: ~
Distribution: Debian 7
Posts: 60

Original Poster
Rep: Reputation: Disabled
you only need one snowflake to start an avalanche, and i guess your help where all i needed.
gl at work. i better work too, now that i got that solved

Last edited by Droa; 10-13-2011 at 04:45 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ssh permisions for single user vintage2010 Linux - Newbie 2 06-10-2011 03:16 PM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
Running a command as another user in a bash script ran as root? camphor Programming 2 03-29-2009 04:11 PM
Running A Single Command In A Script As A Different User Woodsman Slackware 4 06-16-2008 07:08 PM


All times are GMT -5. The time now is 07:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration