public AddForwardedPort ( |
||
port | The port. | |
return | void |
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) )); }
public void tunnel(object options) { //_client.ErrorOccurred += (s, args) => args.Dump(); //_forwardedPort.Exception += (s, args) => args.Dump(); //_forwardedPort.RequestReceived += (s, args) => args.Dump(); _forwardedPort = new ForwardedPortLocal( options.GetProperty("boundHost").ToStringOrDefault(), (uint)options.GetProperty("boundPort").ToIntOrDefault(), options.GetProperty("host").ToStringOrDefault(), (uint)options.GetProperty("port").ToIntOrDefault()); _client.AddForwardedPort(_forwardedPort); _forwardedPort.Start(); connect(); }
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); }
/// <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; }
/// <summary> /// Init forwarding ports. /// </summary> private void InitPortWorwarding() { var list = _cfg.GetSection("portsForwarding").Get <string[]>(); if (list?.Length == 0) { throw new SshClientException(Resources.ConnectionPortsNotSpecified); } for (var i = 0; i < list?.Length; i++) { var cfgArray = list[i].Split(":"); if (cfgArray.Length < 2) { continue; } var host = cfgArray[0]; var port = Convert.ToUInt32(cfgArray[1]); if (string.IsNullOrWhiteSpace(host)) { continue; } Console.WriteLine($@"Forward: {host}: {port}"); var forwardPort = new ForwardedPortLocal(host, port, host, port); _client.AddForwardedPort(forwardPort); if (!forwardPort.IsStarted) { forwardPort.Start(); } } }
private void btn_Q2_Click(object sender, EventArgs e) { sshC = new SshClient("192.168.10.131", "root", "123123"); sshC.KeepAliveInterval = new TimeSpan(0, 0, 30); sshC.ConnectionInfo.Timeout = new TimeSpan(0, 0, 20); sshC.Connect(); // 动态链接 ForwardedPortDynamic port = new ForwardedPortDynamic("127.0.0.1", 22); sshC.AddForwardedPort(port); port.Start(); ////var fport = new ForwardedPortRemote("192.168.10.131", 22, "127.0.0.1", 22); ////sshC.AddForwardedPort(fport); ////fport.Start(); ////string x = sshC.RunCommand("pwd").Result; ; string x = sshC.RunCommand("pwd").Result; ; }
/// <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); } }
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; }
/// <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; }
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_); }
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; } }