[SharpSSH] Connexion à un serveur distant + port forwarding
Bonjour à tous,
j'ai téléchargé une API nommé "SharpSSH" pour remplacer Putty.
J'aimerais me connecter en C# puis lancer mstsc automatiquement.
Lorsque je me connecte avec Putty, je dois faire une redirection de port, voici un exemple:
http://img83.imageshack.us/img83/3949/15474128.png
http://img529.imageshack.us/img529/9673/47271607.png
J'aimerais donc faire la même chose que putty, voici le code que j'ai essayé, mais j'ai l'erreur "Auth fail"
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
SshShell ssh = new SshShell("xxx.xxx.xxx.xxx", "admin", "monpass");
ssh.Connect(22);
//Create a new JSch instance
JSch jsch=new JSch();
//Prompt for username and server host
Console.WriteLine("Please enter the user and host info at the popup window...");
String host = InputForm.GetUserInput
("Enter username@hostname", Environment.UserName+"@localhost");
String user=host.Substring(0, host.IndexOf('@'));
host=host.Substring(host.IndexOf('@')+1);
//Create a new SSH session
Session session=jsch.getSession(user, host, 22);
//Get from user the remote port, local host and local host port
String foo = InputForm.GetUserInput("Enter -R port:host:hostport", "3389:192.168.10.201:3390");
int rport=int.Parse(foo.Substring(0, foo.IndexOf(':')));
foo=foo.Substring(foo.IndexOf(':')+1);
String lhost=foo.Substring(0, foo.IndexOf(':'));
int lport=int.Parse(foo.Substring(foo.IndexOf(':')+1));
// username and password will be given via UserInfo interface.
UserInfo ui=new MyUserInfo();
session.setUserInfo(ui);
session.connect();
Console.WriteLine(host+":"+rport+" -> "+lhost+":"+lport);
//Set port forwarding on the opened session
session.setPortForwardingR(rport, lhost, lport); |
classe MyUserInfo:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| using System;
using System.Collections.Generic;
using System.Text;
using Tamir.SharpSsh.jsch;
using Tamir.SharpSsh.jsch.examples;
namespace testSSH
{
class MyUserInfo : UserInfo
{
/// <summary>
/// Holds the user password
/// </summary>
private String passwd;
/// <summary>
/// Returns the user password
/// </summary>
public String getPassword(){ return passwd; }
/// <summary>
/// Prompt the user for a Yes/No input
/// </summary>
public bool promptYesNo(String str)
{
return InputForm.PromptYesNo(str);
}
/// <summary>
/// Returns the user passphrase (passwd for the private key file)
/// </summary>
public String getPassphrase(){ return null; }
/// <summary>
/// Prompt the user for a passphrase (passwd for the private key file)
/// </summary>
public bool promptPassphrase(String message){ return true; }
/// <summary>
/// Prompt the user for a password
/// </summary>\
public bool promptPassword(String message)
{
passwd=InputForm.GetUserInput(message, true);
return true;
}
/// <summary>
/// Shows a message to the user
/// </summary>
public void showMessage(String message)
{
InputForm.ShowMessage(message);
}
}
} |
Merci
Adrien