简体   繁体   English

ssh sudo pipe命令

[英]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. 这是一个基本的例子,我知道我可以在“ls”命令中输出/ grep输出。 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. 为了不要求输入密码来发出sudo命令,你需要在/etc/sudoers (或者更好的是/etc/sudoers.d的文件)中有一个条目可以让你这样做。 There is a nice question on using sudoers here . 关于在这里使用sudoers这是一个很好的问题。

A sudoers config to allow you to run ls as root could be: 一个sudoers配置允许你以root身份运行ls可能是:

<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. 需要注意的一点是, ls很可能是内置的shell,因此您可能无法使用它。 You should be able to set your shell to defer to the system binary instead. 您应该能够将shell设置为遵循系统二进制文件。

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'. 您没有得到密码提示的原因是因为您正在将“ssh -t www.foo.com'sudo ls -l'”的输出提供给您正在为'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 /' | $ 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. grep'root'user @ host的密码:<=====在此之后,它等待用户输入为sudo用户提供密码。 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 . 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启动。 . .

Try it and see if it works for you. 试一试,看看它是否适合你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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