Exemple #1
0
        static async Task <int> Main(string[] args)
        {
            if (args.Length == 0)
            {
                System.Console.WriteLine("Specify a host to connect to");
                return(1);
            }

            ParseUserHostAndPort(args, out string username, out string host, out int port);
            // string password = ReadPassword();

            var settings = new SshClientSettings
            {
                Host        = host,
                Port        = port,
                UserName    = username,
                Credentials = { new IdentityFileCredential() }
                // Credentials = { new PasswordCredential(username, password) }
            };

            await using var client = new SshClient(settings, CreateLogger());

            await client.ConnectAsync();

            return(0);
        }
Exemple #2
0
        public async Task <SshClient> CreateClientAsync(Action <SshClientSettings> configure = null)
        {
            var        settings   = new SshClientSettings();
            IPEndPoint ipEndPoint = _serverSocket.LocalEndPoint as IPEndPoint;

            settings.Host = ipEndPoint.Address.ToString();
            settings.Port = ipEndPoint.Port;
            configure?.Invoke(settings);

            var client = new SshClient(settings);

            await client.ConnectAsync();

            return(client);
        }
Exemple #3
0
        public async Task CancelConnect(int msTimeout)
        {
            IPAddress address = IPAddress.Loopback;

            using var s = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            s.Bind(new IPEndPoint(address, 0));
            s.Listen();
            int port = (s.LocalEndPoint as IPEndPoint) !.Port;

            using var client = new SshClient($"user@{address}:{port}");

            CancellationTokenSource cts = new();

            cts.CancelAfter(msTimeout);
            await Assert.ThrowsAsync <OperationCanceledException>(() => client.ConnectAsync(cts.Token));
        }
Exemple #4
0
        public async Task Timeout(int msTimeout)
        {
            IPAddress address = IPAddress.Loopback;

            using var s = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            s.Bind(new IPEndPoint(address, 0));
            s.Listen();
            int port = (s.LocalEndPoint as IPEndPoint) !.Port;

            using var client = new SshClient($"user@{address}:{port}", settings =>
            {
                settings.ConnectTimeout = TimeSpan.FromMilliseconds(msTimeout);
            });
            SshSessionException exception = await Assert.ThrowsAsync <SshSessionException>(() => client.ConnectAsync());

            Assert.IsType <TimeoutException>(exception.InnerException);
        }