LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script: su another user and ssh in the single-command (http://www.linuxquestions.org/questions/programming-9/bash-script-su-another-user-and-ssh-in-the-single-command-907731/)

Droa 10-12-2011 03:18 AM

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

corp769 10-13-2011 12:17 AM

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

Droa 10-13-2011 01:32 AM

Quote:

Originally Posted by corp769 (Post 4497099)
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

corp769 10-13-2011 01:53 AM

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.

Droa 10-13-2011 02:14 AM

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?

corp769 10-13-2011 02:19 AM

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.

Droa 10-13-2011 02:59 AM

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.

Droa 10-13-2011 03:33 AM

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 :)

corp769 10-13-2011 03:34 AM

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

Droa 10-13-2011 03:43 AM

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


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