I would like to know how to open putty using C# in Visual Basic express. Then execute commands through the ssh session.
-
1Why does it have to be putty? Can't you open a SSH connection on C# and execute commands through that?– NullUserExceptionCommented Sep 8, 2010 at 14:50
-
that would be great how do I do that– user770022Commented Sep 8, 2010 at 14:53
-
3See tamirgal.com/blog/page/SharpSSH.aspx– Jouke van der MaasCommented Sep 8, 2010 at 14:54
-
Here is what i am trying to do. I have been looking for a way to create a simple GUI for users to stop,start,create, list running VMs in Citrix XenServer. I will create a form that has input boxes for users to put the name of the vm in, then a button to do the wanted task for that name. I would will also create a list box that will list all VM's and another that will list all running VM's. The same for all paused VM's and so on. my biggest issue is I cant find a way to run the command on the remote linux system then list the output into the area I want.– user770022Commented Sep 8, 2010 at 14:54
3 Answers
You can use plink.exe for SSH an pscp.exe for SCP. https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
Donwload the 2 files, then copy them into your Solution. Then under Properties select: Copy if newer.
// SCP
var process = new Process();
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = Directory.GetCurrentDirectory() + $@"\pscp.exe";
processStartInfo.Arguments = $@"-P 22 -pw password filepath.zip root@host:/path ";
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.CreateNoWindow = true;
process.StartInfo = processStartInfo;
process.OutputDataReceived += (sender, args) => Console.WriteLine(args.Data);
process.ErrorDataReceived += (sender, args) => Console.WriteLine(args.Data);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
Console.WriteLine(process.ExitCode);
// SSH
process = new Process();
processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = Directory.GetCurrentDirectory() + $@"\plink.exe";
processStartInfo.Arguments = $@"-P 22 -pw password root@host command";
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.CreateNoWindow = true;
process.StartInfo = processStartInfo;
process.OutputDataReceived += (sender, args) => Console.WriteLine(args.Data);
process.ErrorDataReceived += (sender, args) => Console.WriteLine(args.Data);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
-
1
I recently had to do something similar with WinSCP and the way I did it was to kick off the process with redirected Standard Input and Output. If Putty use the the standard input/output you might be able to use the same method for that.
The sample on the WinSCP pages is quite good so I'd suggest starting with that, and here's a code project article about something similar: How to redirect Standard Input/Output of an application
What you actually need is an SSH component for .NET, capable of providing shell and command channel access to the remote host. Shell is what you see with PuTTY. You "type" the requests and get some response, which you need to parse then to separate responses from command prompt. Command channel is when the commands are sent one by one and you receive only response(s) back. You don't need to parse anything (besides handling actual responses). Simple components can't send multiple commands in one session.
You can take our SSH component for .NET, that offers both shell and command channels, and supports many types of authentication (so it doesn't matter what authentication type your server uses - our component supports it).
-
5shameless plug for a $1000+ component to replace open source product Commented Mar 28, 2014 at 21:24
-
The price for SSHlackbox client package needed for the task starts from $265 for a new license. Commented Mar 29, 2014 at 8:54
-
3Eugene, It is one thing for a member to 'spontaneously' endorse a product and for a member to recommend "buy my product", whatever the cost. Most importantly though is the point that the OP asks one question and you didn't really answer the question, but rather changed the question. Even if you did so to the satisfaction of the OP when I come along looking for answers to the same question, and no I am not going to buy your product, this question and answer become search engine pollution. Commented Mar 31, 2014 at 13:17
-
@CosCallis Please read the OP's question attentively, it was about how to execute SSH commands from C#. This is what the answer was given for AND what was accepted as an answer. If personally you need a different answer, you are free to post your specific question and surely it will be answered to your benefit (or you can run a bounty on it). Saying that OPs question was not answered is not your prerogative but OPs (and he accepted the answer already). Finally the rules of SO were a bit different in different years. The answer was (and remains) valid for the site. Commented Mar 31, 2014 at 16:00