ssh.net没有连接到ssh服务器

时间:2015-08-09 23:43:42

标签: c# ssh ssh.net

当我在使用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).
我在做错了什么? 谢谢你的帮助!

2 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分: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代码会出错吗?)