在 C# 中使用 putty 执行 Unix 命令 [英] Execute Unix commands using putty in C#

查看:95
本文介绍了在 C# 中使用 putty 执行 Unix 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,

我能够打开 putty 会话,但我无法使用 C# 提供输入,我尝试使用 process.StandardInput.WriteLine 但它不起作用.请帮帮我.

I am able to open putty session but i was not able to give the inputs using C# , i have tried using process.StandardInput.WriteLine but it is not working . Please help me.

class Program
{
    static void Main(string[] args)
    {
        Process process = new Process();
        process.StartInfo.FileName = @"C:\Users\win7\Desktop\putty.exe";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardInput = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.Arguments = "-ssh mahi@192.168.37.129 22 -pw mahi";
        process.Start();
        process.StandardInput.WriteLine("ls");


    }
}

大家好,根据 Onots 的建议,我使用了 Plink 而不是腻子.

Hi All , I Have used Plink instead of putty by the suggestion given by Onots.

我做了以下事情

1.将环境变量中的路径编辑到我的 plink 位置,即 D:\Plink\

1.Edited my path in environment variable to my plink location i.e., D:\Plink\

2.Manually Opened Command Prompt and Logged into my host server using plink using the commandplink-0.57.exe -ssh mahi@192.168.37.129 -pw mahi,我可以手动执行命令.

2.Manually Opened Command Prompt and Logged into my host server using plink using the command plink-0.57.exe -ssh mahi@192.168.37.129 -pw mahi and i am able to execute commands manually.

3.现在我修改了代码如下

3.Now i have modified the code as below

class Program
{
    static void Main(string[] args)
    {
        Process process = new Process();
        process.StartInfo.FileName = @"D:\Plink0.57\plink-057.exe"; 
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardInput = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.Arguments = " -ssh mahi@192.168.37.129  -pw mahi";
        process.Start();
        process.StandardInput.WriteLine("ls");
    }
}

但是我无法打开 plink 并且该命令未使用 C# 执行.但是发生的情况是,命令提示符刚刚出现并立即关闭,我认为错误消息为

But i was not able to open plink and the command is not executing using C#. But what happens is , a command prompt just appears and closes immediately , i have figured the error message as

"Unable to write to standard input"

请帮帮我,我不能再继续了.提前致谢.

Please help me ,i am not able to proceed any further. Thanks In Advance.

推荐答案

以下代码给出解决方案

class Program
    {
        static void Main(string[] args)
        {
            ProcessStartInfo startinfo = new ProcessStartInfo();
            startinfo.FileName = @"D:\Plink0.57\plink-057.exe";
            startinfo.Arguments = "-ssh mahi@192.168.37.129  -pw mahi";
            Process process = new Process();
            process.StartInfo = startinfo;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.Start();
            process.StandardInput.WriteLine("ls");
            process.WaitForExit();
            Console.ReadKey();
        }
    }

这篇关于在 C# 中使用 putty 执行 Unix 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆