【问题标题】:SharpSSH .NET Library: unable to connect to Linux (Debian) from .NETSharpSSH .NET 库:无法从 .NET 连接到 Linux (Debian)
【发布时间】:2012-03-06 16:30:38
【问题描述】:

我正在尝试使用 SharpSSH 连接到 Linux,但无法连接。我想从我的 .NET 应用程序中运行一些 Linux 命令。

安装说明:

SharpSsh - 通过 SSH 连接到 UNIX 的 .NET 库
- 创建帐户@codeproject.com
- http://www.codeproject.com/Articles/11966/sharpSsh-A-Secure-Shell-SSH-library-for-NET(下载演示项目和二进制/dll文件)
- 复制到“SharpSsh”文件夹

我正在运行此下载附带的 sharpSshTest 控制台应用程序。当我通过 Putty 手动 SSH(端口 22)到 Linux 以获得相同的主机/登录名/密码时,我可以正常连接。通过 .NET 连接相同的主机和凭据时,我在 Tamir.sharpSsh .NET 库中抛出异常。

Unix 发行版和版本:

$ 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

堆栈跟踪:

   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

sharpSshTest.cs 中的第 76 行:

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

e.Message 的价值:

{"verify: False"}

如果有帮助,这里是代码:

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


    }
}

【问题讨论】:

    标签: .net linux exception debian sharpssh


    【解决方案1】:

    看起来那个项目不太好。在此处下载新的,其中包含新的“SharpSSH”类库项目的实际源代码和打开 Visual Studio 解决方案文件时名为“Examples”的控制台应用程序。

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

    在启动“示例”控制台应用程序时,它工作正常!

    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:~$
    

    【讨论】:

    • 是的,那个项目有点过时了。我已经在 bitbucket.org/mattgwagner/sharpssh 进行了一些更新并获取了一些补丁,但这远非理想。还可以在 CodePlex 上查看 SSH.net。
    • 是否可以在不启动控制台窗口(使用此库)的情况下在 Linux 和命令中进行身份验证?如果没有,我可以使用什么库?
    • 是的,我不明白为什么不这样做,因为它会与远程服务器进行所有通信。
    • 你能给我一个调用 touch {filename} 命令的例子,或者你选择的一些 Linux 命令吗?
    • (注意:我没有测试过) -- SshExec ssh = new SshExec(host, user, pass); ssh.Connect(); String result = ssh.RunCommand("touch /tmp/restart.txt");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    相关资源
    最近更新 更多