Running Local Script from SSH with SUDO


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running Local Script from SSH with SUDO
# 1  
Old 09-02-2015
Running Local Script from SSH with SUDO

Hello,

I know for SSH'ing and running a local script is...
Code:
ssh -t user@servername < /path/to/localscript.sh

and with SSH'ing and SUDO'ing is...
Code:
ssh -t user@servername "sudo -u username ls -l /home/username"

My inquiry is how can I combine both, by SSH'ing and SUDO'ing but running a local script instead a script on target servername?

I have tried couple of commands but it didn't work for such like...
Code:
ssh -t user@servername < "sudo -u username /path/to/localscript.sh"

ssh -t user@servername "sudo -u username < /path/to/localscript.sh"


Thank you in advance for assistance.
# 2  
Old 09-02-2015
Hi,
Are you try to check if your script work fine with only "sudo" command ?

Regards.
# 3  
Old 09-02-2015
I played around with this a bit out of curiosity and the stumbling block seems to be the stdin redirection to sudo. For example, the following works fine:

ssh -t user@host sudo -u user <command>

Any attempts I used to change the command to include redirection would not work.

I also tried ideas similar to this with no luck

cat /path/to/local.sh 2>&1| ssh -t user@host sudo -u user <&1

I couldn't locate any details specific to sudo not working with redirection but that appears to be the main issue from my testing. I tried various switches with ssh and sudo like ssh -t and sudo -S or sudo -n, but was not able to get a combo that worked.

Any reason you cannot copy the script to the destination machine instead of trying to run it from a local location?
# 4  
Old 09-02-2015
It is useless to pass the filename into ssh, because the remote system cannot use the filename.

This may be a catch-22 because you need standard input to input passwords to sudo, but if stdin is occupied, you have no way to pass in the script. ssh is less of a problem because it can open /dev/tty to talk to you directly, but the sudo on the remote end can't do that.

If ssh and sudo are both configured for passwordless use:

Code:
ssh user@host sudo -u user < localfilename

The script will feed into standard input.
# 5  
Old 09-02-2015
Quote:
Originally Posted by TioTony
I also tried ideas similar to this with no luck

cat /path/to/local.sh 2>&1| ssh -t user@host sudo -u user <&1
That is precisely equivalent to ssh -t user@host sudo -u user < /path/to/local.sh

But again, if stdin is busy carrying scripts, sudo on the remote side can't get a password from it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to have local shell variables in a ksh script seen on remove server in SSH block?

I have googled this and found many solutions, but none of them are working for me. I am in a korn shell, most others reference bsh, maybe that is the issue? Anyway, all I am trying to do is use a variable I have declared in my main script in a remote shell I am running through ssh. So I have a... (8 Replies)
Discussion started by: DJR
8 Replies

2. UNIX for Beginners Questions & Answers

Running local script remotely with arguments

Dear Experts, I have found this script on internet that can be used to execute local script remotely #!/bin/bash # runremote.sh # usage: runremote.sh localscript remoteuser remotehost arg1 arg2 ... realscript=$1 user=$2 host=$3 shift 3 # escape the arguments declare -a args ... (4 Replies)
Discussion started by: mukulverma2408
4 Replies

3. Shell Programming and Scripting

Issue with running a script via ssh

Hi, I'm trying to run a user defined shell script with options and arguments via ssh but getting error as ksh: Script.sh: not found. Here is what i'm running: ssh -t username@server 'cd /path/to/script; script.sh -t start here '-t' with script.sh, is an user defined option and 'start' is also... (3 Replies)
Discussion started by: xsam
3 Replies

4. UNIX for Dummies Questions & Answers

Script still running after ssh

I have the lines below on my script: script.ksh: case `hostname` in some_host) ssh server1A "/home/script.ksh $1 $2" ssh server1B "/home/script.ksh $1 $2" ssh server1C "/home/script.ksh $1 $2" ssh server1D "/home/script.ksh $1 $2" ssh... (1 Reply)
Discussion started by: erin00
1 Replies

5. UNIX for Dummies Questions & Answers

Sudo ssh with command running in background

I am trying to run a command. This is one of my attempts: for i in fileservera; do ssh -t $i 'sudo ls /';doneThis works, and I see the directories. However, what I want to do now is start a process on the remote server such as /usr/bin/connectproc -standalonesudo /usr/bin/connectproc... (1 Reply)
Discussion started by: newbie2010
1 Replies

6. Shell Programming and Scripting

Help in creating Sudo ssh script

Hi Experts, I am new to Shell scripting. I want to login to a server using a script. The normal command I use is --> sudo ssh <Servername> . when i tried putting this into a txt format file and tried running, it throw an error "can't execute". I am an Admin and i have root access. Any help would... (6 Replies)
Discussion started by: Tom1989
6 Replies

7. Shell Programming and Scripting

Running local script remotely with arguments in ksh

When i use ssh command to execute local script on remote server , I am unable to do it. Please let me know how this can be done in ksh req=abc dte=ghd ssh username@hostname "$req $dte" < run_script.sh (2 Replies)
Discussion started by: lalitpct
2 Replies

8. OS X (Apple)

sudo chown -R `whoami` /usr/local

I was following a tutorial on installing Homebrew and I changed the ownership of /usr/local/ to me. Now McAfee Security won't start This is the exact line I typed: sudo chown -R `whoami` /usr/local Then I tried to fix it with: sudo chown -R root /usr/local I still can't start mcafee. It say... (7 Replies)
Discussion started by: chancho
7 Replies

9. Shell Programming and Scripting

ssh foo.com sudo command - Prompts for sudo password as visible text. Help?

I am writing a BASH script to update a webserver and then restart Apache. It looks basically like this: #!/bin/bash rsync /path/on/local/machine/ foo.com:path/on/remote/machine/ ssh foo.com sudo /etc/init.d/apache2 reloadrsync and ssh don't prompt for a password, because I have DSA encryption... (9 Replies)
Discussion started by: fluoborate
9 Replies

10. Shell Programming and Scripting

How to stop a script running in remote server from local script

Hi, I have googled for quite some time and couldn't able to get what exactly I am looking for.. My query is "how to stop a shell script which is running inside a remote server, using a script"??? can any one give some suggestions to sort this out. (1 Reply)
Discussion started by: mannepalli
1 Replies
Login or Register to Ask a Question