簡體   English   中英

使用SharpSSH時驗證失敗錯誤

[英]Auth fail error when using SharpSSH

我正在嘗試使用C#類連接到Solaris/Unix服務器以讀取系統信息/配置,內存使用情況等。

我的要求是從C#應用程序在服務器上運行命令( 就像處理PuTTY客戶端一樣 ),並將響應存儲在string變量中以供以后處理。

經過一番研究,我發現SharpSSH庫可以用來做同樣的事情。

當我嘗試運行代碼時,以下行給我一個Auth Fail異常 我確信憑據(服務器名,用戶名和密碼)正確,因為我能夠使用相同的憑據從PuTTY客戶端登錄。

SshStream ssh = new SshStream(servername, username, password);

我究竟做錯了什么?

以下是堆棧跟蹤(如果有幫助的話)!

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)   

經過一番研究,我發現了一個VB代碼,該代碼為我指明了正確的方向。 似乎為KeyboardInteractiveAuthenticationMethod添加了額外的事件處理程序有助於解決此問題。 希望這對其他人有幫助。

void HandleKeyEvent(Object sender, AuthenticationPromptEventArgs e)
    {
        foreach (AuthenticationPrompt prompt in e.Prompts)
        {
            if (prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) != -1)
            {
                prompt.Response = password;
            }
        }
    }

private bool connectToServer()
{
    try
    {
        KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(username);
        PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(username, password);
        kauth.AuthenticationPrompt += new EventHandler<AuthenticationPromptEventArgs>(HandleKeyEvent);

        ConnectionInfo connectionInfo = new ConnectionInfo(serverName, port, username, pauth, kauth);

        sshClient = new SshClient(connectionInfo);
        sshClient.Connect();
        return true;
        }
    catch (Exception ex)
    {
        if (null != sshClient && sshClient.IsConnected)
        {
            sshClient.Disconnect();
        }
        throw ex;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM