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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-12-2011, 03:18 AM
|
#1
|
|
Member
Registered: Oct 2011
Location: ~
Distribution: Debian 7
Posts: 60
Rep: 
|
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
|
|
|
|
10-13-2011, 12:17 AM
|
#2
|
|
Guru
Registered: Apr 2005
Location: /dev/null
Distribution: technixOS
Posts: 5,723
|
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
|
|
|
|
10-13-2011, 01:32 AM
|
#3
|
|
Member
Registered: Oct 2011
Location: ~
Distribution: Debian 7
Posts: 60
Original Poster
Rep: 
|
Quote:
Originally Posted by corp769
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
|
|
|
|
10-13-2011, 01:53 AM
|
#4
|
|
Guru
Registered: Apr 2005
Location: /dev/null
Distribution: technixOS
Posts: 5,723
|
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.
|
|
|
|
10-13-2011, 02:14 AM
|
#5
|
|
Member
Registered: Oct 2011
Location: ~
Distribution: Debian 7
Posts: 60
Original Poster
Rep: 
|
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?
|
|
|
|
10-13-2011, 02:19 AM
|
#6
|
|
Guru
Registered: Apr 2005
Location: /dev/null
Distribution: technixOS
Posts: 5,723
|
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.
|
|
|
|
10-13-2011, 02:59 AM
|
#7
|
|
Member
Registered: Oct 2011
Location: ~
Distribution: Debian 7
Posts: 60
Original Poster
Rep: 
|
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.
|
|
|
|
10-13-2011, 03:33 AM
|
#8
|
|
Member
Registered: Oct 2011
Location: ~
Distribution: Debian 7
Posts: 60
Original Poster
Rep: 
|
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 03:35 AM.
|
|
|
|
10-13-2011, 03:34 AM
|
#9
|
|
Guru
Registered: Apr 2005
Location: /dev/null
Distribution: technixOS
Posts: 5,723
|
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.
|
10-13-2011, 03:43 AM
|
#10
|
|
Member
Registered: Oct 2011
Location: ~
Distribution: Debian 7
Posts: 60
Original Poster
Rep: 
|
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 03:45 AM.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:27 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|