本文介绍了SharpSSH .NET库:无法从.NET连接到Linux(Debian的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图连接到Linux上使用SharpSSH,但我不能。我想运行从我的.NET应用程序的一些Linux命令。

安装说明:

SharpSsh - .NET库通过SSH - 创建账号@ codeproject.com
- (下载演示项目和二进制文件/ dll文件)
- 复制到SharpSsh文件夹

我正在随此下载的sharpSshTest控制台应用程序。当我手动SSH通过腻子同一主机/登录/密码(端口22)到Linux,我可以正常连接。当通过.NET相同的主机和凭证连接,我得到扔在Tamir.sharpSsh .NET库中的一个例外。

Unix发行和版本:

  $ lsb_release -a
没有LSB模块可用。
发行编号:Debian的
说明:的Debian GNU / Linux的5.0.9(兰尼)
发布:5.0.9
codeNAME:莱尼
 

堆栈跟踪:

 在Tamir.SharpSsh.jsch.Session.connect(的Int32 connectTimeout)
   在Tamir.SharpSsh.jsch.Session.connect()
   在Tamir.SharpSsh.SshStream..ctor(字符串主机,字符串用户名,字符串密码)
   在sharpSshTest.sharpSshTest.SshStream()在C:\来源\ {路径} \ selenium_references \ sharpSsh_Demo \ sharpSsh.demo \ sharpSshTest.cs:行76
 

76号线在sharpSshTest.cs:

  SshStream SSH =新SshStream(主机,用户通过);
 

e.Message的值:

  {验证:假}
 

这里的code。如果它可以帮助:

 使用系统;
使用Tamir.SharpSsh;
使用的System.Threading;

命名空间sharpSshTest
{
    ///<总结>
    ///摘要说明sharpSshTest。
    ///< /总结>
    公共类sharpSshTest
    {
        静态字符串主机,用户通过;
        公共静态无效的主要()
        {
            打印版本();
            Console.WriteLine();
            Console.WriteLine(1)简单的SSH会话,例如使用SshStream);
            Console.WriteLine(2)从本地到远程SCP例如);
            Console.WriteLine(3)SCP例如,从远程到本地);
            Console.WriteLine();

            输入:
            INT I = -1;
            Console.Write(请输入您的选择:);
            尝试
            {
                I = int.Parse(到Console.ReadLine());
                Console.WriteLine();
            }
            抓住
            {
                I = -1;
            }

            开关(ⅰ)
            {
                情况1:
                    SshStream();
                    打破;
                案例2:
                    SCP(发送);
                    打破;
                案例3:
                    SCP(从);
                    打破;
                默认:
                    Console.Write(坏输入);
                    转到输入;
            }
        }

        ///<总结>
        ///获得从所述用户输入
        ///< /总结>
        公共静态无效GetInput()
        {
            Console.Write(远程主机:);
            主机=到Console.ReadLine();
            Console.Write(用户);
            用户到Console.ReadLine =();
            Console.Write(密码:);
            通=到Console.ReadLine();
            Console.WriteLine();
        }

        ///<总结>
        ///演示SshStream类
        ///< /总结>
        公共静态无效SshStream()
        {
            GetInput();

            尝试
            {
                Console.Write( - 连接......);
                SshStream SSH =新SshStream(主机,用户通过);
                Console.WriteLine(行({0} / {1}),ssh.Cipher,ssh.Mac);
                Console.WriteLine(服务器版本= {0},客户端版本= {1},ssh.ServerVersion,ssh.ClientVersion);
                Console.WriteLine( - 使用退出命令断开。);
                Console.WriteLine();

                //设置响应字符的结束
                ssh.Prompt =#;
                //删除终端仿真人物
                ssh.RemoveTerminalEmulationCharacters = TRUE;

                //读取来自SSH流的初始响应
                Console.Write(ssh.ReadResponse());

                //从用户发送命令
                而(真)
                {
                    字符串命令=到Console.ReadLine();
                    如果(command.ToLower()。等于(退出))
                        打破;

                    //写命令到SSH流
                    ssh.Write(命令);
                    //读取从SSH流响应
                    Console.Write(ssh.ReadResponse());
                }
                ssh.Close(); //关闭连接
                Console.WriteLine(连接关闭。);
            }
            赶上(例外五)
            {
                Console.WriteLine(e.Message);
            }
        }

        ///<总结>
        ///演示SCP类
        ///< /总结>
        ///< PARAM NAME =CMD>要么到或从< /参数>
        公共静态无效的SCP(字符串CMD)
        {
            GetInput();

            字符串当地= NULL,远程= NULL;

            如果(cmd.ToLower()。等于(到))
            {
                Console.Write(本地文件);
                本地=到Console.ReadLine();
                Console.Write(远程文件:);
                远程=到Console.ReadLine();
            }
            否则,如果(cmd.ToLower()。等于(从))
            {
                Console.Write(远程文件:);
                远程=到Console.ReadLine();
                Console.Write(本地文件);
                本地=到Console.ReadLine();
            }

            SCP SCP =新的SCP();
            scp.OnConnecting + =新FileTansferEvent(scp_OnConnecting);
            scp.OnStart + =新FileTansferEvent(scp_OnProgress);
            scp.OnEnd + =新FileTansferEvent(scp_OnEnd);
            scp.OnProgress + =新FileTansferEvent(scp_OnProgress);

            尝试
            {
                如果(cmd.ToLower()。等于(到))
                    scp.To(本地,主机,遥控器,用户通过);
                否则,如果(cmd.ToLower()。等于(从))
                    scp.From(主机,遥控器,用户通过本地);
            }
            赶上(例外五)
            {
                Console.WriteLine(e.Message);
            }

            到Console.ReadLine();
        }

        静态无效打印版本()
        {
            尝试
            {
                System.Reflection.Assembly ASM
                    = System.Reflection.Assembly.GetAssembly(typeof运算(Tamir.SharpSsh.SshStream));
                Console.WriteLine(sharpSsh V+ asm.GetName()版本。);
            }
            抓住
            {
                Console.WriteLine(sharpSsh 1.0);
            }
        }

        #地区SCP事件处理程序

        静态ConsoleProgressBar进度;

        私有静态无效scp_OnConnecting(INT transferredBytes,诠释totalBytes,字符串消息)
        {
            Console.WriteLine();
            进度=新ConsoleProgressBar();
            progressBar.Update(transferredBytes,totalBytes,消息);
        }

        私有静态无效scp_OnProgress(INT transferredBytes,诠释totalBytes,字符串消息)
        {
            progressBar.Update(transferredBytes,totalBytes,消息);
        }

        私有静态无效scp_OnEnd(INT transferredBytes,诠释totalBytes,字符串消息)
        {
            progressBar.Update(transferredBytes,totalBytes,消息);
            进度= NULL;
        }

        #endregion SCP事件处理程序


    }
}
 

解决方案

貌似该项目也是白搭。下载在这里新建一个,其中包括新的SharpSSH类库项目的实际来源$ C ​​$ c和所谓的范例控制台应用程序,当你打开Visual Studio解决方案文件。

http://www.tamirgal.com/blog/page/SharpSSH。 ASPX#新闻

在开展示例控制台应用程序,它的工作!

  SharpSSH-1.1.1.13

JSch Smaples:
=============
1)Shell.cs
2)AES.cs
3)UserAuthPublicKey.cs
4)Sftp.cs
5)KeyGen.cs
6)KnownHosts.cs
7)ChangePassphrase.cs
8)PortForwardingL.cs
9)PortForwardingR.cs
10)StreamForwarding.cs
11)Subsystem.cs
12)ViaHTTP.cs

SharpSSH Smaples:
=================
13)SSH壳牌样本
14)SSH期待样品
15)SSH Exec的样品
16)SSH文件传输
17)退出

请输入您的选择:13

{这里类型的主机}:进入远程主机
输入用户名:testjobs
使用公钥认证? [是|否]:无
输入密码:的HelloWorld

连接... OK
Linux的gmqa 2.6.32-5-686-bigmem#1 SMP周四4月7日22点17分10秒UTC 2011的i686

附带的Debian GNU / Linux系统的程序是免费软件;
确切的发行条款每个程序的描述
在/ usr /共享/ DOC / * /版权单个文件。

的Debian GNU / Linux带有绝对没有担保,在一定程度
适用法律所允许。
上次登录:星期二03月06日9时58分32秒2012自{计算机名}
testjobs @ gmqa:〜$
 

I'm trying to connect to Linux using SharpSSH, but am unable to. I want to run some Linux commands from my .NET application.

Install Instructions:

SharpSsh - .NET library to connect to UNIX via SSH
- create account @ codeproject.com
- http://www.codeproject.com/Articles/11966/sharpSsh-A-Secure-Shell-SSH-library-for-NET (download demo project and binaries/dll files)
- copied into "SharpSsh" folder

I'm running the sharpSshTest console application that comes with this download. When I manually SSH (port 22) to Linux via Putty for same host/login/password, I'm able to connect fine. When connecting with the same host and credentials via .NET, I get an exception thrown in the Tamir.sharpSsh .NET library.

Unix distribution and version:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 5.0.9 (lenny)
Release:        5.0.9
Codename:       lenny

StackTrace:

   at Tamir.SharpSsh.jsch.Session.connect(Int32 connectTimeout)
   at Tamir.SharpSsh.jsch.Session.connect()
   at Tamir.SharpSsh.SshStream..ctor(String host, String username, String password)
   at sharpSshTest.sharpSshTest.SshStream() in C:\Source\{path}\selenium_references\sharpSsh_Demo\sharpSsh.demo\sharpSshTest.cs:line 76

Line 76 in sharpSshTest.cs:

SshStream ssh = new SshStream(host, user, pass);

Value of e.Message:

{"verify: False"}

Here's the code if it helps:

using System;
using Tamir.SharpSsh;
using System.Threading;

namespace sharpSshTest
{
    /// <summary>
    /// Summary description for sharpSshTest.
    /// </summary>
    public class sharpSshTest
    {
        static string host, user, pass;
        public static void Main()
        {
            PrintVersion();
            Console.WriteLine();
            Console.WriteLine("1) Simple SSH session example using SshStream");
            Console.WriteLine("2) SCP example from local to remote");
            Console.WriteLine("3) SCP example from remote to local");
            Console.WriteLine();

            INPUT:
            int i=-1;
            Console.Write("Please enter your choice: ");
            try
            {
                i = int.Parse( Console.ReadLine() );
                Console.WriteLine();                
            }
            catch
            {
                i=-1;
            }

            switch(i)
            {
                case 1:
                    SshStream();
                    break;
                case 2:
                    Scp("to");
                    break;
                case 3:
                    Scp("from");
                    break;
                default:
                    Console.Write("Bad input, ");
                    goto INPUT;
            }
        }

        /// <summary>
        /// Get input from the user
        /// </summary>
        public static void GetInput()
        {
            Console.Write("Remote Host: ");
            host = Console.ReadLine();
            Console.Write("User: ");
            user = Console.ReadLine();
            Console.Write("Password: ");
            pass = Console.ReadLine();
            Console.WriteLine();
        }

        /// <summary>
        /// Demonstrates the SshStream class
        /// </summary>
        public static void SshStream()
        {
            GetInput();

            try
            {           
                Console.Write("-Connecting...");
                SshStream ssh = new SshStream(host, user, pass);
                Console.WriteLine("OK ({0}/{1})",ssh.Cipher,ssh.Mac);
                Console.WriteLine("Server version={0}, Client version={1}", ssh.ServerVersion, ssh.ClientVersion);
                Console.WriteLine("-Use the 'exit' command to disconnect.");
                Console.WriteLine();

                //Sets the end of response character
                ssh.Prompt = "#";
                //Remove terminal emulation characters
                ssh.RemoveTerminalEmulationCharacters = true;

                //Reads the initial response from the SSH stream
                Console.Write( ssh.ReadResponse() );

                //Send commands from the user
                while(true)
                {
                    string command = Console.ReadLine();
                    if (command.ToLower().Equals("exit"))
                        break;

                    //Write command to the SSH stream
                    ssh.Write( command );
                    //Read response from the SSH stream
                    Console.Write( ssh.ReadResponse() );
                }
                ssh.Close(); //Close the connection
                Console.WriteLine("Connection closed.");
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        /// <summary>
        /// Demonstrates the Scp class
        /// </summary>
        /// <param name="cmd">Either "to" or "from"</param>
        public static void Scp(string cmd)
        {
            GetInput();

            string local=null, remote=null;

            if(cmd.ToLower().Equals("to"))
            {
                Console.Write("Local file: ");
                local = Console.ReadLine();
                Console.Write("Remote file: ");
                remote = Console.ReadLine();
            }
            else if(cmd.ToLower().Equals("from"))
            {
                Console.Write("Remote file: ");
                remote = Console.ReadLine();
                Console.Write("Local file: ");
                local = Console.ReadLine();
            }

            Scp scp = new Scp();
            scp.OnConnecting += new FileTansferEvent(scp_OnConnecting);
            scp.OnStart += new FileTansferEvent(scp_OnProgress);
            scp.OnEnd += new FileTansferEvent(scp_OnEnd);
            scp.OnProgress += new FileTansferEvent(scp_OnProgress);

            try
            {
                if(cmd.ToLower().Equals("to"))
                    scp.To(local, host, remote, user, pass);
                else if(cmd.ToLower().Equals("from"))
                    scp.From(host, remote, user, pass,local);
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.ReadLine();
        }

        static void PrintVersion()
        {
            try
            {               
                System.Reflection.Assembly asm
                    = System.Reflection.Assembly.GetAssembly(typeof(Tamir.SharpSsh.SshStream));
                Console.WriteLine("sharpSsh v"+asm.GetName().Version);
            }
            catch
            {
                Console.WriteLine("sharpSsh v1.0");
            }
        }

        #region SCP Event Handlers

        static ConsoleProgressBar progressBar;

        private static void scp_OnConnecting(int transferredBytes, int totalBytes, string message)
        {
            Console.WriteLine();
            progressBar = new ConsoleProgressBar();
            progressBar.Update(transferredBytes, totalBytes, message);
        }

        private static void scp_OnProgress(int transferredBytes, int totalBytes, string message)
        {
            progressBar.Update(transferredBytes, totalBytes, message);
        }

        private static void scp_OnEnd(int transferredBytes, int totalBytes, string message)
        {
            progressBar.Update(transferredBytes, totalBytes, message);
            progressBar=null;
        }

        #endregion SCP Event Handlers


    }
}
解决方案

Looks like that project is no good. Downloaded the new one here, which consists of the actual source code of the new "SharpSSH" class library project and a console application called "Examples" when you open the Visual Studio solution file.

http://www.tamirgal.com/blog/page/SharpSSH.aspx#news

When launching the "Examples" console application, it worked!

SharpSSH-1.1.1.13

JSch Smaples:
=============
1)      Shell.cs
2)      AES.cs
3)      UserAuthPublicKey.cs
4)      Sftp.cs
5)      KeyGen.cs
6)      KnownHosts.cs
7)      ChangePassphrase.cs
8)      PortForwardingL.cs
9)      PortForwardingR.cs
10)     StreamForwarding.cs
11)     Subsystem.cs
12)     ViaHTTP.cs

SharpSSH Smaples:
=================
13)     SSH Shell sample
14)     SSH Expect Sample
15)     SSH Exec Sample
16)     SSH File Transfer
17)     Exit

Please enter your choice: 13

Enter Remote Host: {type host here}
Enter Username: testjobs
Use publickey authentication? [Yes|No] :No
Enter Password: helloworld

Connecting...OK
Linux gmqa 2.6.32-5-686-bigmem #1 SMP Thu Apr 7 22:17:10 UTC 2011 i686

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Tue Mar  6 09:58:32 2012 from {machine name}
testjobs@gmqa:~$

这篇关于SharpSSH .NET库:无法从.NET连接到Linux(Debian的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 12:09