简体   繁体   中英

ssh sudo pipe command

this command works

ssh -t www.foo.com 'sudo ls -l'

I immediately get asked for my password, once entered I get the directory contents

if I try to pipe this command it fails - I never get asked my password so I never get any contents.

ssh -t www.foo.com 'sudo ls -l' | grep 'foo'

This is a basic example, I know I could pipe/grep the output in my "ls" command. for my application I need to pipe the output through a program on my host.

ssh -t www.foo.com 'echo your_pass | sudo -S ls -l' | grep 'foo'

也可能:

ssh -t www.foo.com 'sudo -S ls -l <~/passwd.txt' | grep 'foo'

In order to not be asked for a password to issue sudo commands, you need to have an entry in /etc/sudoers (or better, a file in /etc/sudoers.d ) that lets you do it. There is a nice question on using sudoers here .

A sudoers config to allow you to run ls as root could be:

<yourusername> ALL = (root) NOPASSWD: /bin/ls

One thing to look out for is that ls is quite likely to be a shell built-in, so you may have trouble allowing it. You should be able to set your shell to defer to the system binary instead.

Trying to push your real password through it is not a secure solution.

The reason you are not getting prompt for password is because you are feeding the output of "ssh -t www.foo.com 'sudo ls -l'" to the pipe where you are grepping for 'foo'. So in this case the password prompt won't be shown, but actually the process is waiting for your input.

Try giving the password on the blank new line you get after running the command.

I tried this and it worked. Just for example,

$ ssh -t user@host 'sudo ls -l /' | grep 'root' user@host's password: <===== After this, it waits for user input to feed the password for sudo user. Connection to host closed. drwxr-xr-x 2 root root 4096 2011-08-31 15:33 bin dr-xr-xr-x 24 root root 4096 2014-07-14 00:52 bldmnt dr-xr-xr-x 2 root root 4096 2011-03-23 15:51 blr drwxr-xr-x 3 root root 4096 2011-08-31 15:36 boot . . .

Try it and see if it works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM