I am writing a very small C# program designed to allow me to connect remotely to a server via SSH (using SSH .NET Client Library), and execute commands, mainly commands about printers such as 'lpstat'. Until here I used to use Putty for that purpose and check stuff (mainly printers states) manually. I would like to automate some of this. Using Putty I was connecting with user1 (when prompted for a user/password just after connecting) and had to "sudo su - user2" to then be able to execute lpstat and others commands.
Now here is the problem : Once I am connected with SSH .NET, the command "sudo su - " seems to freeze the connection. Why ? Could it be the same problem as this one from Serverfault ? If there is another way to be connected as user2 (ie using SSH .NET differently), that would be fine with me.
Important notes:
- I don't know user2's or root's password
- It is a production server. I am not its administrator, and I am not a very advanced Unix user. I do not want to alter that server configuration.
- I believe the freeze has to do with the command asking for a password, but I'm not sure.
My current code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Renci.SshNet;
using System.IO;
using System.Threading;
namespace SSHChecker
{
class Program
{
static void Main(string[] args)
{
SshClient sshClient = new SshClient(
"IP",
666, // port
"user1",
"user1Password");
sshClient.Connect();
SshCommand sshCommand;
String res;
//sshCommand = sshClient.RunCommand("ls d /"); // works fine
sshCommand = sshClient.RunCommand("sudo su - user2"); // freezes ...
sshCommand = sshClient.RunCommand("ls d /"); // ... so this code is never executed.
sshClient.Disconnect();
}
}
}