コード例 #1
0
ファイル: ControlSystem.cs プロジェクト: clarkp26/SSH_Test
        /// <summary>
        /// ControlSystem Constructor. Starting point for the SIMPL#Pro program.
        /// Use the constructor to:
        /// * Initialize the maximum number of threads (max = 400)
        /// * Register devices
        /// * Register event handlers
        /// * Add Console Commands
        ///
        /// Please be aware that the constructor needs to exit quickly; if it doesn't
        /// exit in time, the SIMPL#Pro program will exit.
        ///
        /// You cannot send / receive data in the constructor
        /// </summary>
        public ControlSystem()
            : base()
        {
            try
            {
                Thread.MaxNumberOfUserThreads = 20;

                //Subscribe to the controller events (System, Program, and Ethernet)
                CrestronEnvironment.SystemEventHandler        += new SystemEventHandler(_ControllerSystemEventHandler);
                CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(_ControllerProgramEventHandler);
                CrestronEnvironment.EthernetEventHandler      += new EthernetEventHandler(_ControllerEthernetEventHandler);

                UcEngine ucEngine = new UcEngine(0x03, this);

                //WebSocketClient webSocketclient = new WebSocketClient
                //{
                //    KeepAlive = false,
                //    VerifyServerCertificate = false,
                //    Port = 443,
                //    //Host = "C4-54B203910FDC"
                //    Host = "10.25.94.48"
                //    //SSL = true
                //};
                //WebSocketClientAsyncConnectCallback webSocketClientAsyncConnectCallback = del;
                //var err = webSocketclient.ConnectEx();
                //ErrorLog.Info("URL: " +(int) err);
                //string send = "reboot";
                //byte[] bytes = Encoding.ASCII.GetBytes(send);
                //System.Security.Cryptography.X509Certificates.X509Certificate x509Certificate = new X509Certificate();
                //Crestron.SimplSharp.Cryptography.X509Certificates.X509Certificate x509Certificate1 = new Crestron.SimplSharp.Cryptography.X509Certificates.X509Certificate(bytes);


                //webSocketclient.SetClientCertificate(x509Certificate1);
                //webSocketclient.Connect();
                ////webSocketClient.Host = "10.25.94.48";
                //ErrorLog.Info("Client Connected: " + webSocketClient.Connected);
                //webSocketClient.Port = 443;
                //webSocketClient.SSL = true;


                //var connectCode = webSocketClient.Connect();

                //ErrorLog.Info("Err Code: " + (int)connectCode);

                //string server = "10.25.94.48";
                //TcpClient client = new TcpClient(server, 41797);
                //SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
                ////ErrorLog.Info("SSL: " + sslStream.SslProtocol);
                string send   = "reboot\r";
                byte[] bytes  = Encoding.ASCII.GetBytes(send);
                string send1  = "reboot";
                byte[] bytes1 = Encoding.ASCII.GetBytes(send1);
                //using (sslStream)
                //{
                //    sslStream.AuthenticateAsClient(server);
                //    ErrorLog.Info("SSL: " + sslStream.SslProtocol);
                //    ErrorLog.Info("Can Write: " + sslStream.CanWrite);
                //    //sslStream.Begi
                //    sslStream.Write(bytes);
                //    ErrorLog.Info("Can Write: " + sslStream.CanWrite);

                // This is where you read and send data
                //}
                //client.Close();



                SecureTCPClient secureTCPClient = new SecureTCPClient("10.25.94.48", 41797, bytes.Length);
                var             err1            = secureTCPClient.ConnectToServer();
                ErrorLog.Info("Secure Error Code after connect: " + (int)err1);
                var sendErr = secureTCPClient.SendData(bytes, bytes.Length);
                ErrorLog.Info("Secure Error Code after Send: " + (int)sendErr);
                ErrorLog.Info("Server Address: " + secureTCPClient.AddressClientConnectedTo);
                ErrorLog.Info("Socket Status: " + (int)secureTCPClient.ClientStatus);


                var sendErr2 = secureTCPClient.SendData(bytes1, bytes1.Length);
                ErrorLog.Info("Secure Error Code after Send2: " + (int)sendErr2);
                var ids = secureTCPClient.DisconnectFromServer();
                ErrorLog.Info("Secure Error Code after Disconnect: " + (int)ids);


                //    TCPClient tCPClient = new TCPClient("10.25.94.48", 41797, 1000);
                //    var err = tCPClient.ConnectToServer();
                //    ErrorLog.Info("Err code: " + (int)err);
                //tCPClient.SendData(bytes,0,1000);
                //    tCPClient.DisconnectFromServer();


                //System.Net.Sockets.TcpClient csnative = new System.Net.Sockets.TcpClient("10.25.94.48", 443);
                //    ErrorLog.Info("Connected: " + csnative.Connected);
                //    SslStream sslStream = new SslStream(csnative.GetStream());
                //    ErrorLog.Info("SSL: " + sslStream.IsAuthenticated);
                //    sslStream.AuthenticateAsClient("10.25.94.24");

                //    ErrorLog.Info("SSL: " + sslStream.SslProtocol);
                //sslStream.AuthenticateAsClient("10.25.94.48");
                //SshClient sshClient = new SshClient("C4-54B203910FDC", 22, "admin", "sfb");
                //sshClient.ErrorOccurred += SshClient_ErrorOccurred1;
                //ErrorLog.Info("Connected? " + (int)tCPClient.ClientStatus);

                //SocketErrorCodes err = tCPClient.ConnectToServer();
                //ErrorLog.Info("SocketErrorCode: " + (int)err);
                //ErrorLog.Info("Connected? " + (int)tCPClient.ClientStatus);

                //sslStream.Write(bytes);
                //tCPClient.SendData(bytes, 0, 1000);
                //SshClient sshClient = new SshClient("C4-54B203910FDC", 4443, "hrav", "2Loughmuck!");
                //sshClient.ErrorOccurred += SshClient_ErrorOccurred;
                //sshClient.Connect();

                //IEnumerable<ForwardedPort> s = sshClient.ForwardedPorts;
                //List<ForwardedPort> list = s.ToList();
                //foreach( var a in list)
                //{
                //    ErrorLog.Info("" + a.IsStarted);

                //}

                //SshClient sshClient = new SshClient("10.25.94.48", 4443, "hrav", "2Loughmuck!");


                //var stream = sshClient.CreateShellStream("terminal", 80, 24, 800, 600, 1024);
                //stream.Write("Reboot");
                //SshCommand sshCommand = new SshCommand();
                //ErrorLog.Info("Connected: " + sshClient.IsConnected);
                //SshCommand cmd = sshClient.RunCommand("info");
                //string res = cmd.Execute();
                //ErrorLog.Info("Response: " + res);
            }
            catch (Exception e)
            {
                //if(e.GetType == SshConnectionException)
                //ErrorLog.Info("Errrrrr : " + (int)e.DisconnectReason);
                ErrorLog.Error("Error in the constructor: {0}", e.Message);
                ErrorLog.Error("Error in the constructor: {0}", e.InnerException);
                ErrorLog.Error("Error in the constructor: {0}", e.ToString());
                //ErrorLog.Error("Error in the constructor: {0}", e.Data);
            }
        }