1

What I try to do, I connect to a remote server as a normal user with sudo right, then sudo to root, and execute a command & see output in my local terminal. I wrote a small script like this:

#!/bin/bash

my_argument=$1
ssh -t username@hostname 'sudo su -; /path_to_my_script $1'

I type the password twice (one for ssh, the other for sudo), but I see nothing in my local terminal, and script looks terminated in remote host. I believe second problem could be resolved by using exit, but I am a little bit confused how I can get this output to my local terminal.

Thanks

1 Answer 1

5

String inside '' is taken literally. So, you are passing the dollar sign and 1 as a parameter to the script. If you want the string to be interpreted, place it inside "", like:

ssh -t username@hostname "sudo /path_to_my_script $1"
Sign up to request clarification or add additional context in comments.

2 Comments

It's also worth pointing out that your answer fixes an additional problem by passing the script directly to sudo. The original command would have executed an interactive sudo shell first (sudo su -) and then, only after typing interactive exit to exit that shell, would have invoked the script in a NON-sudo context.
Good answer. If you want to make script complete non interactive (means why you have to type passwords whenever script runs) go for expect Click here

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.