Skip to content
Advertisement

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");

Advertisement

Answer

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>"
    
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement