简体   繁体   English

从ac#应用程序远程运行sudo命令时出现问题。

[英]Issue in running sudo commands remotely from a c# application.

I am writing an utility in c# to get system information of remote machines. 我在c#中编写实用程序,以获取远程计算机的系统信息。 In certain Linux machines direct login to root is not allowed and some of the commands i am running require root privileges. 在某些Linux机器中,不允许直接登录到root,而我正在运行的某些命令需要root特权。 I am able to establish SSH session (I am using Renci SSh libraries for same). 我能够建立SSH会话(我也使用Renci SSh库)。

Researching leads me to believe in such cases I need to use sudo. 研究使我相信,在这种情况下,我需要使用sudo。 But when I am using sudo remotely like this it is not working. 但是当我像这样远程使用sudo时,它不起作用。

The first part of code works when I am get root access. 当我获得root访问权限时,代码的第一部分将起作用。 Where direct root access is not allowed - the first part does not return anything and the second part using sudo is also not returning anything). 在不允许直接root访问的地方-第一部分不返回任何内容,而第二部分使用sudo也不返回任何内容)。 Please see the code I am using (client is the name assigned to SshClient). 请查看我正在使用的代码(客户端是分配给SshClient的名称)。

I don't know Linux well enough. 我对Linux不够了解。 Request guidance on how to approach this issue. 请求有关如何解决此问题的指南。

client.RunCommand("cat /proc/cpuinfo | grep model | grep name");
client.RunCommand("dmidecode -s system-manufacturer");

client.RunCommand("sudo cat /proc/cpuinfo | grep model | grep name");
client.RunCommand("sudo dmidecode -s system-manufacturer");

This is what finally worked for me. 这终于对我有用。 Posting it for those who come here with similar issues. 将其发布给那些遇到类似问题的人。

  1. requiretty should either be commented out in the sudo file or set as not required for the particular user. requiretty应该在sudo文件中注释掉,或设置为特定用户不需要。

  2. command structure should be as 命令结构应为

     echo <password> | sudo -S <linux command>" 

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

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