Пример #1
0
        //Tutorial: Connecting to a host and opening a shell
        private static void ConnectAndOpenShell()
        {
            SampleKeyboardInteractiveAuthenticationHandler authHandler = null;
            SSHConnectionParameter f = new SSHConnectionParameter("172.22.1.15", 22, SSHProtocol.SSH2, AuthenticationType.PublicKey, "okajima", "aaa");

            //former algorithm is given priority in the algorithm negotiation
            f.PreferableHostKeyAlgorithms = new PublicKeyAlgorithm[] { PublicKeyAlgorithm.RSA, PublicKeyAlgorithm.DSA };
            f.PreferableCipherAlgorithms  = new CipherAlgorithm[] { CipherAlgorithm.Blowfish, CipherAlgorithm.TripleDES };
            f.WindowSize = 0x1000; //this option is ignored with SSH1
            f.KeyboardInteractiveAuthenticationHandlerCreator =
                (connection) => {
                return(authHandler = new SampleKeyboardInteractiveAuthenticationHandler("aaa"));
            };

            Tracer tracer = new Tracer();

            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            s.Connect(new IPEndPoint(IPAddress.Parse(f.HostName), f.PortNumber)); //22 is the default SSH port

            ISSHConnection conn;

            if (f.AuthenticationType == AuthenticationType.KeyboardInteractive)
            {
                //Creating a new SSH connection over the underlying socket
                conn = SSHConnection.Connect(s, f, c => new Reader(c), c => new Tracer());
                bool result = authHandler.GetResult();
                Debug.Assert(result == true);
            }
            else
            {
                //NOTE: if you use public-key authentication, follow this sample instead of the line above:
                //f.AuthenticationType = AuthenticationType.PublicKey;
                f.IdentityFile     = "C:\\P4\\tools\\keys\\aaa";
                f.VerifySSHHostKey = (info) => {
                    byte[] h = info.HostKeyFingerPrint;
                    foreach (byte b in h)
                    {
                        Debug.Write(String.Format("{0:x2} ", b));
                    }
                    return(true);
                };

                //Creating a new SSH connection over the underlying socket
                conn = SSHConnection.Connect(s, f, c => new Reader(c), null);
            }

            //Opening a shell
            var ch = conn.OpenShell(channelOperator => new ChannelHandler(channelOperator));

            //you can get the detailed connection information in this way:
            //SSHConnectionInfo ci = _conn.ConnectionInfo;

            //Go to sample shell
            SampleShell(ch);
        }
Пример #2
0
        //Tutorial: Connecting to a host and opening a shell
        private static void ConnectAndOpenShell()
        {
            SampleKeyboardInteractiveAuthenticationHandler authHandler = null;
            SSHConnectionParameter f = new SSHConnectionParameter("172.22.1.15", 22, SSHProtocol.SSH2, AuthenticationType.PublicKey, "okajima", "aaa");
            //former algorithm is given priority in the algorithm negotiation
            f.PreferableHostKeyAlgorithms = new PublicKeyAlgorithm[] { PublicKeyAlgorithm.RSA, PublicKeyAlgorithm.DSA };
            f.PreferableCipherAlgorithms = new CipherAlgorithm[] { CipherAlgorithm.Blowfish, CipherAlgorithm.TripleDES };
            f.WindowSize = 0x1000; //this option is ignored with SSH1
            f.KeyboardInteractiveAuthenticationHandlerCreator =
                (connection) => {
                    return (authHandler = new SampleKeyboardInteractiveAuthenticationHandler("aaa"));
                };

            Tracer tracer = new Tracer();

            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Connect(new IPEndPoint(IPAddress.Parse(f.HostName), f.PortNumber)); //22 is the default SSH port

            ISSHConnection conn;

            if (f.AuthenticationType == AuthenticationType.KeyboardInteractive) {
                //Creating a new SSH connection over the underlying socket
                conn = SSHConnection.Connect(s, f, c => new Reader(c), c => new Tracer());
                bool result = authHandler.GetResult();
                Debug.Assert(result == true);
            }
            else {
                //NOTE: if you use public-key authentication, follow this sample instead of the line above:
                //f.AuthenticationType = AuthenticationType.PublicKey;
                f.IdentityFile = "C:\\P4\\tools\\keys\\aaa";
                f.VerifySSHHostKey = (info) => {
                    byte[] h = info.HostKeyFingerPrint;
                    foreach (byte b in h)
                        Debug.Write(String.Format("{0:x2} ", b));
                    return true;
                };

                //Creating a new SSH connection over the underlying socket
                conn = SSHConnection.Connect(s, f, c => new Reader(c), null);
            }

            //Opening a shell
            var ch = conn.OpenShell(channelOperator => new ChannelHandler(channelOperator));

            //you can get the detailed connection information in this way:
            //SSHConnectionInfo ci = _conn.ConnectionInfo;

            //Go to sample shell
            SampleShell(ch);
        }