Using Plink.exe from C# (accessing a linux PC)

There seems to be a lot of questions on the web from people trying to access a linux from C#, most of them having problems using 'Plink'. This comes with 'Putty' and allows you to run remote commands on a linux machine.

For this example, you have to use Putty to create and store a session using a public key. This makes automated login safer and more secure.

The following code runs the 'ls' command on the remote session, called "quarch". Note the redirect of both standard in and standard out!

System.Diagnostics.ProcessStartInfo ProcStart;
System.Diagnostics.Process Proc;
string Result;
System.IO.StreamReader Output;

ProcStart = new System.Diagnostics.ProcessStartInfo(@"c:\program files\putty\plink.exe");
ProcStart.Arguments = "-load quarch ls";
ProcStart.RedirectStandardOutput = true;
ProcStart.RedirectStandardInput = true;
ProcStart.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
ProcStart.UseShellExecute = false;
ProcStart.CreateNoWindow = false;
Proc = System.Diagnostics.Process.Start(ProcStart);

Output = Proc.StandardOutput;
Proc.WaitForExit(5000);
Result = Output.ReadToEnd();
return Result;

Comments

Popular posts from this blog

JTAG Progromming on a PIC

Evohome problems on Danfoss RAS-C2 valves