C#renci ssh命令输出不同于客户端到客户端

问题描述 投票:-1回答:1

一些真正奇怪的是发生在我身上。

我有SSH命令,我的远程服务器上执行python脚本。当我通过腻子执行它它就像它应该并且被示出所期望的输出。但是,当通过我的C#应用​​程序中使用ssh.net执行它总是返回一些旧的数据是很长一段时间从服务器了(这是FreeBSD的9.3)。

以下是我执行我的命令,并捕获输出:

var task = new Task(() => {
    var ret = ssh_instance.CreateCommand("cd /home/" + "python python_script.py -param");
    ret.Execute();
    FlexibleMessageBox.Show(ret.Result);
});
task.Start();

这似乎从字面上无法解释给我,所以我会很感激,如果有人能够告诉我什么我做错了。

c# ssh.net
1个回答
-1
投票

请记住。

  1. 连接将调用“CreateCommand()”方法后关闭。
  2. 你需要从你对你的每一个命令流读取所有接收到的数据。 - >在我看来,这是一个原因,你有旧的结果,从您的网络连接。

所以,我尝试使用Renci.SshNet.ShellStream类。在我的代码是,

    private Renci.SshNet.SshClient _client = null;
    private Renci.SshNet.ShellStream shellStream = null;

    private void connect()
    {
        try
        {
            this.disConnect();
            _client = new Renci.SshNet.SshClient(cboUri.Text, tbxSrvUsr.Text, tbxSrvPW.Text);
            //_client.ErrorOccurred += new EventHandler<Renci.SshNet.Common.ExceptionEventArgs>(_client_ErrorOccurred);
            //_client.HostKeyReceived += new EventHandler<Renci.SshNet.Common.HostKeyEventArgs>(_client_HostKeyReceived);

            _client.Connect();

            byte[] buffer = new byte[1000];
            shellStream = _client.CreateShellStream("Jornathan", 80, 24, 800, 800, 1024);
            shellStream.BeginRead(buffer, 0, buffer.Length, null, null);


            shellStream.DataReceived += new EventHandler<Renci.SshNet.Common.ShellDataEventArgs>(shellStream_DataReceived);

            tbxStatus.Text = "Connected";            

            this.btnConnect.Enabled = false;
            this.cboServerList.Enabled = false;

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

并添加方法来读取服务器中的所有文本。请记住,直到遇到接收到的数据大小为“0”,你应该阅读所有的文字。如下参考。

    private void readOutput()
    {
        do
        {
            string strOutput = shellStream.ReadLine();
            if (strOutput != null && strOutput.Length > 0)
            {
                System.Console.WriteLine(strOutput);
                this.SetStatusText(strOutput);
            }
            else
            {
                break;
            }

        } while (true);
    }

然后添加事件处理程序,这就是它!

    private System.Threading.Thread Thread_OutputHandle = null;

    private void shellStream_DataReceived(object sender, Renci.SshNet.Common.ShellDataEventArgs e)
    {
        if (Thread_OutputHandle == null)
        {

            Thread_OutputHandle = new System.Threading.Thread(new System.Threading.ThreadStart(readOutput));
            Thread_OutputHandle.Start();
        }
        else if (Thread_OutputHandle.ThreadState == System.Threading.ThreadState.Stopped)
        {
            Thread_OutputHandle.Abort();

            Thread_OutputHandle = new System.Threading.Thread(new System.Threading.ThreadStart(readOutput));
            Thread_OutputHandle.Start();
        }            
    }

其他:我希望能记得我有帮助,使这个代码。但我不能。

请让我知道你什么时候发现的,我已经得到了帮助代码位置。我会添加它的位置在这里。

© www.soinside.com 2019 - 2024. All rights reserved.