Пример #1
0
        } // Demo

        private static void MyDemoSSH(string[] args){
            Granados.SSHConnectionParameter f = new Granados.SSHConnectionParameter();
			f.EventTracer = new Tracer(); //to receive detailed events, set ISSHEventTracer
			Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            f.Protocol = Granados.SSHProtocol.SSH2; //this sample works on both SSH1 and SSH2
            string host_ip = "192.168.192.165";
			f.UserName = "******";
            string password = "******";
			s.Connect(new IPEndPoint(IPAddress.Parse(host_ip), 22)); //22 is the default SSH port

            //former algorithm is given priority in the algorithm negotiation
            f.PreferableHostKeyAlgorithms = new PublicKeyAlgorithm[] { PublicKeyAlgorithm.RSA, PublicKeyAlgorithm.DSA };
            f.PreferableCipherAlgorithms = new Granados.CipherAlgorithm[] { Granados.CipherAlgorithm.Blowfish, Granados.CipherAlgorithm.TripleDES };
            f.WindowSize = 0x1000; //this option is ignored with SSH1
            Reader reader = new Reader(); //simple event receiver

            Granados.AuthenticationType at = Granados.AuthenticationType.Password;
            f.AuthenticationType = at;

            if (at == Granados.AuthenticationType.KeyboardInteractive)
            {
                //Creating a new SSH connection over the underlying socket
                _conn = Granados.SSHConnection.Connect(f, reader, s);
                reader._conn = _conn;
                Debug.Assert(_conn.AuthenticationResult == Granados.AuthenticationResult.Prompt);
                Granados.AuthenticationResult r = ((SSH2Connection)_conn).DoKeyboardInteractiveAuth(new string[] { password });
                Debug.Assert(r == Granados.AuthenticationResult.Success);
            }
            else {
                //NOTE: if you use public-key authentication, follow this sample instead of the line above:
                //f.AuthenticationType = AuthenticationType.PublicKey;
                f.IdentityFile = AppDomain.CurrentDomain.BaseDirectory + "Key-File.key";
                f.Password = password;
                f.KeyCheck = delegate(Granados.SSHConnectionInfo info)
                {
                    byte[] h = info.HostKeyMD5FingerPrint();
                    foreach(byte b in h) Debug.Write(String.Format("{0:x2} ", b));
                    return true;
                };

                //Creating a new SSH connection over the underlying socket
                _conn = Granados.SSHConnection.Connect(f, reader, s);
                reader._conn = _conn;
                TerminalDemo.ConnectionStatus = 1;
            }

            //Opening a shell
            Granados.SSHChannel ch = _conn.OpenShell(reader);
            reader._pf = ch;

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

			//Go to sample shell
			SampleShell(reader);
        }
Пример #2
0
        /// <summary>
        /// Connect to the ssh server
        /// </summary>
        /// <returns>true if connection was successful</returns>
        public bool Connect()
        {
            // check for buffer
            if (this.buffer == null)
            {
                this.buffer = new byte[RECEIVEBUFFERSIZE];
            }

            // virtual screen
            if (_virtualScreen == null)
            {
                _virtualScreen = new VirtualScreen(this.vsWidth, this.vsHeight, 1, 1);
            }

            /*
             * // set the callbacks
             * if (this.callBackReceive == null)
             *  this.callBackReceive = new AsyncCallback(ReadFromStream);
             * if (this.callBackSend == null)
             *  this.callBackSend = new AsyncCallback(WriteToStream);
             */
            // flags
            this.serverEcho     = false;
            this.clientInitNaws = false;
            this.firstResponse  = true;
            this.nawsNegotiated = false;
            this.forceLogout    = false;

            //Open SSH-Connection
            Granados.SSHConnectionParameter f = new Granados.SSHConnectionParameter();
            f.EventTracer = new Tracer(); //to receive detailed events, set ISSHEventTracer
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            f.Protocol = Granados.SSHProtocol.SSH2; //this sample works on both SSH1 and SSH2
            string host_ip = hostName;

            f.UserName = userName;
            string password = Password;

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

            //former algorithm is given priority in the algorithm negotiation
            f.PreferableHostKeyAlgorithms = new PublicKeyAlgorithm[] { PublicKeyAlgorithm.RSA, PublicKeyAlgorithm.DSA };
            f.PreferableCipherAlgorithms  = new Granados.CipherAlgorithm[] { Granados.CipherAlgorithm.Blowfish, Granados.CipherAlgorithm.TripleDES };
            f.WindowSize     = 0x1000;       //this option is ignored with SSH1
            reader           = new Reader(); //simple event receiver
            reader.parentObj = this;

            Granados.AuthenticationType at = Granados.AuthenticationType.Password;
            f.AuthenticationType = at;

            if (at == Granados.AuthenticationType.KeyboardInteractive)
            {
                //Creating a new SSH connection over the underlying socket
                _conn        = Granados.SSHConnection.Connect(f, reader, s);
                reader._conn = _conn;
                Debug.Assert(_conn.AuthenticationResult == Granados.AuthenticationResult.Prompt);
                Granados.AuthenticationResult r = ((SSH2Connection)_conn).DoKeyboardInteractiveAuth(new string[] { password });
                Debug.Assert(r == Granados.AuthenticationResult.Success);
            }
            else
            {
                //NOTE: if you use public-key authentication, follow this sample instead of the line above:
                //f.AuthenticationType = AuthenticationType.PublicKey;
                f.IdentityFile = AppDomain.CurrentDomain.BaseDirectory + "Key-File.key";
                f.Password     = password;
                f.KeyCheck     = delegate(Granados.SSHConnectionInfo info)
                {
                    byte[] h = info.HostKeyMD5FingerPrint();
                    foreach (byte b in h)
                    {
                        Debug.Write(String.Format("{0:x2} ", b));
                    }
                    return(true);
                };

                //Creating a new SSH connection over the underlying socket
                _conn            = Granados.SSHConnection.Connect(f, reader, s);
                reader._conn     = _conn;
                ConnectionStatus = 1;   //TerminalSSH.ConnectionStatus = 1;
            }

            //Opening a shell
            Granados.SSHChannel ch = _conn.OpenShell(reader);
            reader._pf = ch;

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

            //Go to sample shell
            //SampleShell(reader);

            return(true);
        }