如何使用 C# 打开 Putty 会话

use*_*022 2 c# ssh

我想知道如何在 Visual Basic Express 中使用 C# 打开 putty。然后通过 ssh 会话执行命令。

Mic*_*tos 5

您可以使用 plink.exe 进行 SSH,使用 pscp.exe 进行 SCP。 https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

下载这 2 个文件,然后将它们复制到您的解决方案中。然后在属性下选择:如果较新则复制。

 // 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();
Run Code Online (Sandbox Code Playgroud)