Mike K.
Mike K.

Reputation: 608

Using sshpass to run a command on another server

I'm trying to run a single command on server X, to have that SSH into server Y and run another command

I'm doing this like the below:

sshpass -p 'my_password' ssh -t test-admin@my_ip "sudo su c command_must_be_run_root --arguments"

So to break it down:

Any ideas on how to fix this?

Upvotes: 0

Views: 5776

Answers (2)

arctic
arctic

Reputation: 71

For what ever reason when you have a command with arguments you need to actually tell sudo su to login as root. Without logging in first it will run the first part of the command, even with all of it in single quotes, but not the args. (I guess it thinks that is the end of the command or it's only 1 command per sudo su -c, and that is why the persistent login works?). After adding sudo su -l root then you can continue with -c and everything that follows needs single quotes.

Should look like this: sshpass -p 'my_password' ssh -t test-admin@my_ip "sudo su -l root -c 'command_ran_as_root --arguments'"

Upvotes: 2

larsks
larsks

Reputation: 311516

I don't think that su command is valid in any case. The syntax of su is su <someuser> [arguments...], so what you've written is going to try running command_must_be_run_root as user c.

I suspect that c is supposed to be -c, in which case you need to quote the arguments to -c, which would solve the problem you're asking about:

sshpass -p 'my_password' ssh -t test-admin@my_ip "sudo su -c 'command_must_be_run_root --arguments'"

But the next question is, if you already have sudo access, why are you bothering with su? You could just write instead:

sshpass -p 'my_password' ssh -t test-admin@my_ip sudo command_must_be_run_root --arguments

Upvotes: 0

Related Questions