简体   繁体   English

如何使用 C# 打开腻子会话

[英]how to open a putty session using C#

I would like to know how to open putty using C# in Visual Basic express.我想知道如何在 Visual Basic express 中使用 C# 打开腻子。 Then execute commands through the ssh session.然后通过 ssh 会话执行命令。

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.我最近不得不用 WinSCP 做一些类似的事情,我这样做的方式是用重定向的标准输入和输出来启动这个过程。 If Putty use the the standard input/output you might be able to use the same method for that.如果 Putty 使用标准输入/输出,您可能可以使用相同的方法。

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 WinSCP 页面上的示例非常好,所以我建议从它开始,这里有一篇关于类似内容的代码项目文章:如何重定向应用程序的标准输入/输出

You can use plink.exe for SSH an pscp.exe for SCP.您可以将 plink.exe 用于 SSH,将 pscp.exe 用于 SCP。 https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

Donwload the 2 files, then copy them into your Solution.下载这 2 个文件,然后将它们复制到您的解决方案中。 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();

What you actually need is an SSH component for .NET, capable of providing shell and command channel access to the remote host.您实际需要的是 .NET 的 SSH 组件,能够提供对远程主机的 shell 和命令通道访问。 Shell is what you see with PuTTY. Shell 就是您在 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).您可以使用我们的 .NET SSH 组件,它提供 shell 和命令通道,并支持多种类型的身份验证(因此您的服务器使用哪种身份验证类型并不重要 - 我们的组件支持它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM