Example #1
0
            public bool TryGetClient(SSHCredentials credentials, out SshClient client)
            {
                client = null;
                SshClient deleteClient = null;

                foreach (SshClient c in this)
                {
                    if (c.ConnectionInfo.Host == credentials.Host)
                    {
                        if (c.IsConnected == true)
                        {
                            client = c;
                        }
                        else
                        {
                            deleteClient = c;
                        }
                        break;
                    }
                }

                if (deleteClient != null)
                {
                    Remove(deleteClient);
                }

                return(client != null);
            }
Example #2
0
        public static bool Authorize(SSHCredentials sshCredentials)
        {
            SshClient client = null;

            PrivateKeyConnectionInfo connectionInfo = new PrivateKeyConnectionInfo(
                sshCredentials.Host,
                sshCredentials.UserName,
                new PrivateKeyFile[] { new PrivateKeyFile(sshCredentials.KeyFile) });

            client = new SshClient(connectionInfo);
            client.Connect();
            client.Disconnect();
            return(true);
        }
Example #3
0
        static public bool EnsureTunnelExists(SqlDBCredentials credentials)
        {
            SSHCredentials sshCredentials = credentials.SSHCredentials;
            SshClient      client         = null;

            if (_clients.TryGetClient(sshCredentials, out client) == false)
            {
                PrivateKeyConnectionInfo connectionInfo = new PrivateKeyConnectionInfo(
                    sshCredentials.Host,
                    sshCredentials.UserName,
                    new PrivateKeyFile[] { new PrivateKeyFile(sshCredentials.KeyFile) });

                client = new SshClient(connectionInfo);
                client.KeepAliveInterval = TimeSpan.FromMinutes(1);
                client.ErrorOccurred    += Client_ErrorOccurred;
                client.HostKeyReceived  += Client_HostKeyReceived;
                client.Connect();

                if (client.IsConnected)
                {
                    ForwardedPortLocal port = new ForwardedPortLocal(
                        IPAddress.Loopback.ToString(),
                        sshCredentials.LocalTunnelPort,
                        sshCredentials.TunnelHost, 3306);
                    client.AddForwardedPort(port);
                    port.Start();
                    if (port.IsStarted == false)
                    {
                        client.Disconnect();
                        client = null;
                        throw new Exception("Could not forward port");
                    }
                    _clients.Add(client);
                }
            }
            return(client != null);
        }