I need to set up a strange system through which an arbitrary command is sent to a number of different servers (well, actually, VPS accounts). We have a command "vpass" that "passes" a command from the root level to resident VPS accounts. Suppose I wanted each VPS to do some trivial thing, like report its hostname. I might use a script like:
for account in `cat accountlist`
do
vpass $account hostname
done
So, as you can see, the vpass syntax is:
vpass accountname command__to_run
Now, it would be convenient to set up a little script that runs an arbitrary command that's in a file called "command". To replicate the results above, I create "command" with "hostname" in it, and then do:
cmd=$(cat command)
for account in `cat accountlist`
do
vpass $account $cmd
done
That works just right. However, more complex commands fail. For example, this works:
for account in `cat accountlist`
do
vpass $account mysql --user=root --password=p@ssword -e "USE database ; SELECT stuff FROM table ORDER BY RAND() LIMIT 1;"
done
However, putting that exact MySQL command into the "command" file fails. If I add a line "echo $cmd" after the variable assignment, it gives exactly the right command back, yet passing it into the vpass command fails.
Any suggestions on where the failure might come from? I'm at a loss, mainly because of time-induced brain fuzziness. Many thanks in advance.
Update:
I added -x and checked the results... Apparently some strange things are being done to the command:
vpass acct1 mysql --user=root --password=p@ssword -e '"USE' database ';' SELECT stuff FROM table ORDER BY 'RAND()' LIMIT '1;"'
It seems to my naive troubleshooting that those changes-- the addition of single-quotes throughout the command-- must be the cause of the problem, unless I misunderstand the output of -x.
Update 2:
I don't know why this didn't occur to me before... lack of sleep? Anyway, xargs helps...
cat command | xargs vpass $account
So, that works, but I'm a little perturbed that the other method doesn't. If only for my own education, I'd appreciate any thoughts on why that's failing.
)