简体   繁体   中英

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

I am writing an utility in c# to get system information of remote machines. In certain Linux machines direct login to root is not allowed and some of the commands i am running require root privileges. I am able to establish SSH session (I am using Renci SSh libraries for same).

Researching leads me to believe in such cases I need to use sudo. But when I am using sudo remotely like this it is not working.

The first part of code works when I am get root access. 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). Please see the code I am using (client is the name assigned to SshClient).

I don't know Linux well enough. 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.

  2. command structure should be as

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

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