5

I have a local script that will not run without root privs on the remote host. I have edited and allowed the sudoers file to run the script from the home dir (/home/username/script). The script creates a txt file with the results but I redirect the output to terminal and pipe the output to a text file on the local machine.

I cannot "ssh user@hostname sudo -Sv < script.sh > results.txt" because this will not run the script from the remote host, specifically the home dir.

Does anyone know of a way (one line) to copy the script to the remote host & execute it as root while retrieving the output?

Thank you for any assistance

2 Answers 2

12

if you have sudo, AND allowed to sudo to root, then this works with a bit less syntax:

ssh -T user@hostname 'sudo su -' < script.sh

Sign up to request clarification or add additional context in comments.

Comments

2

Copy the script with scp, then run it.

scp script.sh user@hostname:
ssh user@hostname sudo ./script.sh > results.txt

To do it in one line:

ssh user@hostname 'cat > script.sh; chmod 755 script.sh; sudo ./script.sh' < script.sh > results.txt

However, this won't work if you need to enter a password into sudo. All of ssh's stdin will be put in the script. There might be a way to do this using Expect, but I don't have much expertise there.

4 Comments

Thank you for the prompt response! The ssh keys aren't stored so I will need to enter a 14 char pswd both when scp'ing and ssh'ing. Is there a way to one-line it?
I won't need to enter a password for sudo (running the script). This seems like it will work great! Also,Thanks a lot for your time and help!!
It isn't a correct response to the question, it was about running a command remotely, not pushing to remote machine!
The last line of the question says copy the script to the remote host & execute it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.