我试图连接到Solaris/Unix服务器,使用C#类读取系统信息/配置、内存使用等。
我的要求是从一个PuTTY应用程序( C#应用程序)运行服务器上的命令(就像我们对客户机所做的那样),并将响应存储在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) 发布于 2013-04-16 20:00:38
经过一番研究,我找到了一个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;
}
}https://stackoverflow.com/questions/15668595
复制相似问题