-1

当我在玩 Visual Studio(尝试通过 ssh 运行命令)时,出现了问题,程序无法正常工作。我在网上搜索了几个小时,但没有找到任何解决我问题的方法。

这是代码:

using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (var sshClient = new SshClient("ip censor", "root", "password censor"))
            {
                sshClient.Connect();
                sshClient.RunCommand("screen -S BungeeCord -X stuff 'alert ciao'`echo -ne '\015'`");
                sshClient.Disconnect();
            }
        }
    }
}

这就是错误

'WindowsFormsApplication3.vshost.exe' (CLR v4.0.30319:          WindowsFormsApplication3.vshost.exe): Loaded  '     c:\users\firestorm\documents\visual studio    2015\Projects\WindowsFormsApplication3\WindowsFormsApplication3\bin\Debug\WindowsFormsApplication3.exe'. Symbols loaded.
'WindowsFormsApplication3.vshost.exe' (CLR v4.0.30319:   WindowsFormsApplication3.vshost.exe): Loaded  'c:\users\firestorm\documents\visual studio  2015\Projects\WindowsFormsApplication3\WindowsFormsApplication3\bin\Debug\Renci. SshNet.dll'. Skipped loading symbols. Module is optimized and the debugger  option 'Just My Code' is enabled.
 'WindowsFormsApplication3.vshost.exe' (CLR v4.0.30319: WindowsFormsApplication3.vshost.exe): Loaded   'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
 'WindowsFormsApplication3.vshost.exe' (CLR v4.0.30319: WindowsFormsApplication3.vshost.exe): Loaded  'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\mscorlib.resources\v4.0_4.0.0.0_it_b77a5c561934e089\mscorlib.resources.dll'. Module was built without symbols.
 'WindowsFormsApplication3.vshost.exe' (CLR v4.0.30319: WindowsFormsApplication3.vshost.exe): Loaded   'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Dynamic\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Dynamic.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'WindowsFormsApplication3.vshost.exe' (CLR v4.0.30319:   WindowsFormsApplication3.vshost.exe): Loaded 'Anonymously Hosted DynamicMethods   Assembly'. 
thread 0x38e4 has exited with code 0 (0x0).
The thread 0x1e7c has exited with code 0 (0x0).
The thread 0x1a00 has exited with code 0 (0x0).
The thread 0x2a90 has exited with code 0 (0x0).
The thread 0x3e4c has exited with code 0 (0x0).
The thread 0x3128 has exited with code 0 (0x0).
The thread 0x3c48 has exited with code 0 (0x0).
The thread 0x12e0 has exited with code 0 (0x0).
The thread 0x136c has exited with code 0 (0x0).
The program '[15892] WindowsFormsApplication3.vshost.exe' has exited with code 0 (0x0).

我究竟做错了什么?谢谢您的帮助!

4

2 回答 2

1

它正在工作,你没有对输出做任何事情。尝试在消息框或类似下面的内容中显示输出:

private void button1_Click(object sender, EventArgs e)
{
    using (var sshClient = new SshClient("ip censor", "root", "password censor"))
    {
        sshClient.Connect();
        var cmd = sshClient.RunCommand("screen -S BungeeCord -X stuff 'alert ciao'`echo -ne '\015'`");

        MessageBox.Show(cmd.Output);

        sshClient.Disconnect();
    }
}
于 2015-08-14T08:55:36.080 回答
0

正如 redspidermkv 的回答所说,代码是正确的,但输出没有做任何事情。通常没关系,您可以定义一个变量但不使用它,或者例如关闭命令等​​,如果是连接,您可以尝试以下操作:

建立连接:

public class SSHConnection
    {
        public SSHConnection() { }
    public ConnectionInfo makeSSHConnection(string ipAdress, int port, string user, string pwd)
    {
        ConnectionInfo ConnNfo = new ConnectionInfo(ipAdress, port, user,
          new AuthenticationMethod[]{

            // Pasword based Authentication
            new PasswordAuthenticationMethod(user,pwd),
          }
             );
        return ConnNfo;
    }
}

使用 ConnNfo 连接;

using (var sshclient = new SshClient(ConnNfo))
                {

                    sshclient.Connect();
                    using (var cmd = sshclient.CreateCommand(cmdCommand))
                    {

                        cmd.Execute();
                        Console.WriteLine("Command>" + cmd.CommandText);
                        Console.WriteLine(cmd.Result);
                        Console.WriteLine("Return Value = {0}", cmd.ExitStatus);
                    }
                    sshclient.Disconnect();
                }

请注意,此示例使用 ssh 连接执行 cmd comamndline 功能,但是您可以轻松地将其替换为 ssh 命令。

(你确定是 ssh 代码给出了错误吗?)

于 2016-03-07T08:19:41.313 回答