本文整理汇总了C#中Renci.SshNet.SshClient.AddForwardedPort方法的典型用法代码示例。如果您正苦于以下问题:C# SshClient.AddForwardedPort方法的具体用法?C# SshClient.AddForwardedPort怎么用?C# SshClient.AddForwardedPort使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Renci.SshNet.SshClient
的用法示例。
在下文中一共展示了SshClient.AddForwardedPort方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LocalVmToolStripMenuItemClick
private void LocalVmToolStripMenuItemClick(object sender, EventArgs e)
{
var connectionInfo = new PasswordConnectionInfo(host, 22, username, password);
connectionInfo.AuthenticationBanner += ConnectionInfoAuthenticationBanner;
connectionInfo.PasswordExpired += ConnectionInfoPasswordExpired;
sshClient = new SshClient(connectionInfo);
sshClient.ErrorOccurred += SshClientErrorOccurred;
Log(string.Format("Connecting to {0}:{1} as {2}", connectionInfo.Host, connectionInfo.Port, connectionInfo.Username));
try
{
sshClient.Connect();
var tunnel = sshClient.AddForwardedPort<ForwardedPortLocal>("localhost", 20080, "www.google.com", 80);
tunnel.Start();
}
catch (Exception ex)
{
Log(ex.ToString());
}
finally
{
sshClient.Dispose();
}
Log("Connected");
sshClient.ForwardedPorts.ToList().ForEach(p => Log(string.Format("SSH tunnel: {0}:{1} --> {2}:{3}", p.BoundHost, p.BoundPort, p.Host, p.Port) ));
}
示例2: sshConnected
/// <summary>
/// SSH连接端口转发
/// </summary>
/// <param name="server">服务器地址</param>
/// <param name="port">服务器端口</param>
/// <param name="uid">用户名</param>
/// <param name="pwd">密码</param>
/// <returns></returns>
public static bool sshConnected(string server, int port, string uid, string pwd) {
bool sshState = false;
var client = new SshClient(server, port, uid, pwd);
try {
client.Connect();
Constant.sshConnected = client.IsConnected;
} catch(Exception ex) {
throw ex;
}
var porcik = new ForwardedPortLocal("localhost", 3306, "localhost", 3306);
try {
client.AddForwardedPort(porcik);
porcik.Start();
sshState = true;
} catch(Exception ex) {
throw ex;
}
return sshState;
}
示例3: Proxy
public Proxy(Target target)
{
_client = new SshClient(target.ProxyServer.Host,
target.ProxyServer.UserInfo,
target.ProxyPassword);
_client.Connect();
var fwPort = new ForwardedPortLocal("127.0.0.1",0, target.TargetServer.Host, (uint) target.TargetServer.Port);
_client.AddForwardedPort(fwPort);
fwPort.Start();
var port = HackExtractPort(fwPort); // TODO: Update the library to read it directly from BoundPort
_localUri = string.Format("{0}://{1}@127.0.0.1:{2}{3}", target.TargetServer.Scheme,
target.TargetServer.UserInfo,
port,
target.TargetServer.LocalPath);
_subBackend = Backend.OpenBackend(new Uri(_localUri), target.Password);
}
示例4: StartTunnel
/// <summary>
/// Starts the ssh connection, and bridge the streams.
/// </summary>
private void StartTunnel()
{
try
{
_client = new SshClient(_config.host, _config.user, new PrivateKeyFile(new MemoryStream(Encoding.Default.GetBytes(PrivateKey))));
_client.Connect();
_client.KeepAliveInterval = new TimeSpan(0, 0, 5);
if (!_client.IsConnected)
{
throw new ServiceException("Can't start tunnel, try again.");
}
string connectHost = string.IsNullOrEmpty(this.LocalHost) ? "127.0.0.1" : this.LocalHost;
_connectionPort = _client.AddForwardedPort<ForwardedPortRemote>((uint)_config.through_port, connectHost, (uint)LocalPort);
_connectionPort.Exception += new EventHandler<ExceptionEventArgs>(fw_Exception);
_connectionPort.RequestReceived += new EventHandler<PortForwardEventArgs>(port_RequestReceived);
_connectionPort.Start();
}
catch (Exception e)
{
throw new ServiceException(e.Message);
}
}
示例5: Start
private void Start()
{
iProxyServer.Start(this);
XElement body = new XElement("getaddress");
body.Add(new XElement("uidnode", iDeviceUdn));
XElement tree = CallWebService("getaddress", body.ToString());
if (tree == null)
return;
XElement error = tree.Element("error");
if (error != null)
{
Logger.ErrorFormat("Remote access method {0} failed with error {1}.", "getaddress", error.Value);
return;
}
XElement successElement = tree.Element("success");
XElement sshServerElement = successElement.Element("sshserver");
iSshServerHost = sshServerElement.Element("address").Value;
iSshServerPort = Convert.ToInt32(sshServerElement.Element("port").Value);
XElement portForwardElement = successElement.Element("portforward");
iPortForwardAddress = portForwardElement.Element("address").Value;
iPortForwardPort = (uint)Convert.ToInt32(portForwardElement.Element("port").Value);
if (Environment.OSVersion.Platform.ToString() == "Unix")
{
iSshClientNative = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "ssh",
Arguments = String.Format(
"-i {0} -p {1} -R {2}:{3}:{4}:{5} -N {6}@{7} -o StrictHostKeyChecking=no",
FileFullName(kFilePrivateKey), iSshServerPort, iPortForwardAddress,
iPortForwardPort, iNetworkAdapter, iProxyServer.Port, kSshServerUserName,
iSshServerHost)
};
iSshClientNative.StartInfo = startInfo;
iSshClientNative.Start();
}
else
{
PrivateKeyFile pkf = new PrivateKeyFile(FileFullName(kFilePrivateKey));
iSshClient = new SshClient(iSshServerHost, iSshServerPort, kSshServerUserName, pkf);
iSshClient.Connect();
Logger.InfoFormat("Connected to ssh server at {0}:{1}", iSshServerHost, iSshServerPort);
iForwardedPortRemote = new ForwardedPortRemote(iPortForwardAddress, iPortForwardPort, iNetworkAdapter, iProxyServer.Port);
iSshClient.AddForwardedPort(iForwardedPortRemote);
iForwardedPortRemote.Start();
}
Logger.InfoFormat("Forwarded remote port {0}:{1} to {2}:{3}", iPortForwardAddress, iPortForwardPort, iNetworkAdapter, iProxyServer.Port);
iConnectionCheckTimer.Enabled = true;
}
示例6: BuildTunnel
/// <summary>
/// Connect to SSH-Server and open TCP tunnel.
/// </summary>
/// <returns>Returns true on success, else false.</returns>
private bool BuildTunnel()
{
try
{
client = new SshClient(config.SupportHost,
config.SupportPort,
textBoxUsername.Text,
textBoxPassword.Text);
client.ErrorOccurred += new EventHandler<Renci.SshNet.Common.ExceptionEventArgs>(client_ErrorOccurred);
client.KeepAliveInterval = new System.TimeSpan(0, 0, 10);
client.Connect();
client.SendKeepAlive();
var port = new ForwardedPortRemote(IPAddress.Loopback,
config.FwdRemotePort, IPAddress.Loopback, config.FwdLocalPort);
port.Exception += new EventHandler<Renci.SshNet.Common.ExceptionEventArgs>(port_Exception);
port.RequestReceived += new EventHandler<Renci.SshNet.Common.PortForwardEventArgs>(port_RequestReceived);
client.AddForwardedPort(port);
port.Start();
}
catch (System.Exception ex)
{
Log.WriteLine("BuildTunnel() general exception: {0}", ex.Message);
if (client.IsConnected)
{
client.Disconnect();
}
MessageBox.Show(GetCaption("serverLoginError"), GetCaption("loginFailed"),
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
return sessionActive = true;
}
示例7: ConnectSSH
private void ConnectSSH(string ip, string port, string dbname, string account, string pw, string sshhostname, string sshuser, string sshpw)
{
SshClient_ = new SshClient(ip, sshuser, sshpw);
SshClient_.Connect();
var fowardport = new ForwardedPortLocal("127.0.0.1", sshhostname, Convert.ToUInt32(port));
//var fowardport = new ForwardedPortLocal("127.0.0.1", 25251, sshhostname, Convert.ToUInt32(port));
SshClient_.AddForwardedPort(fowardport);
//private string connection_string_ssh_ = "server={0};user={1};database={2};port={3};password={4};";
fowardport.Start();
real_connection_string_ = string.Format(connection_string_ssh_, "127.0.0.1", account, dbname, fowardport.BoundPort, pw);
MySqlConnection_ = new MySqlConnection(real_connection_string_);
}
示例8: SshTunnel
public SshTunnel(ConnectionInfo connectionInfo, uint remotePort)
{
try
{
client = new SshClient(connectionInfo);
port = new ForwardedPortLocal("127.0.0.1", "leisuredb01", remotePort);
//port = new ForwardedPortLocal("127.0.0.1", 3306, "leisuredb01", remotePort);
//port = new ForwardedPortLocal("127.0.0.1", 22, "leisuredb01", remotePort);
//port = new ForwardedPortLocal("127.0.0.1", "leisuredb01", remotePort);
//port = new ForwardedPortLocal()
//client.ErrorOccurred += (s, args) => args.Dump();
//port.Exception += (s, args) => args.Dump();
//port.RequestReceived += (s, args) => args.Dump();
client.Connect();
client.AddForwardedPort(port);
port.Start();
// Hack to allow dynamic local ports, ForwardedPortLocal should expose _listener.LocalEndpoint
var listener = (TcpListener)typeof(ForwardedPortLocal).GetField("_listener", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(port);
localPort = ((System.Net.IPEndPoint)listener.LocalEndpoint).Port;
}
catch
{
Dispose();
throw;
}
}