Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: d1dac en 6 Noviembre 2012, 03:10 am



Título: Aporte-Duda DownNET
Publicado por: d1dac en 6 Noviembre 2012, 03:10 am
Hola comunidad, hace ya un tiempo quise codear un programa que apagara todas las maquinas de la red local, lo había abandonado pero lo volví a retomar y parece que va en buen camino.

La primera versión que hice tenia un defecto que al enviar la señal de apagado a las maquinas, apagaba por ejemplo maquina 1,maquina 2,m 3,m 4,mi maquina. pero ya no apagaba el resto. m 5,m6
ya que mi maquina estaba en el medio.

Decidí arreglarlo y parece estar ok. pero me gustaría saber en que puede ser optimizado o añadir opciones, o incluso si es que funciona

Aqui mi code

Código
  1. using System;
  2. using System.Text;
  3. using System.Diagnostics;
  4. using System.Collections;
  5. using System.IO;
  6. using System.Threading;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10.    class Program
  11.    {
  12.        static Process u;
  13.        static Process v;
  14.        static string usuario;
  15.        static string[] maquina;
  16.        static int i=0;
  17.  
  18.        static void Main(string[] args)
  19.        {
  20.            //*-*-*-*-*-*-*-*-*-*-*
  21.            //Lan Dow Net by D1dac
  22.            //*-*-*-*-*-*-*-*-*-*-*
  23.            try
  24.            {
  25.                File.Delete("user.txt");
  26.                File.Delete("maquinas.txt");
  27.            }
  28.            catch { }
  29.  
  30.            Thread nv = new Thread(netView);
  31.            nv.Start();
  32.  
  33.            Thread nu = new Thread(netUser);
  34.            nu.Start();
  35.  
  36.            Console.ForegroundColor = ConsoleColor.Green;
  37.            Console.Title = "Net Shutdown by D1dac 1.0";
  38.  
  39.  
  40.            Console.WriteLine("En cuanto tiempo quieres que se apague" + "\nPD:Tu maquina tambien se incluye en la lista");
  41.            string tiempo=Console.ReadLine();
  42.  
  43.  
  44.            for (int a = 0; a <= i-1; a++)
  45.            {
  46.                if (!maquina[a].Equals((usuario)))
  47.                {
  48.                    ProcessStartInfo info = new ProcessStartInfo("CMD.EXE", "/c shutdown -s -t " + tiempo + " -m " + maquina[a]);
  49.                    info.WindowStyle = ProcessWindowStyle.Hidden;
  50.                    info.Verb = "open";
  51.                    Process.Start(info);
  52.                }
  53.            }
  54.  
  55.            ProcessStartInfo autoS = new ProcessStartInfo("CMD.EXE", "/c  shutdown -s -t " + tiempo);
  56.            autoS.WindowStyle = ProcessWindowStyle.Hidden;
  57.            autoS.Verb = "open";
  58.            Process.Start(autoS);
  59.  
  60.        }
  61.  
  62.  
  63.  
  64.        static void netView()
  65.        {
  66.            ProcessStartInfo info1 = new ProcessStartInfo("CMD.EXE", "/C net view > maquinas.txt");
  67.            info1.Verb = "open";
  68.            info1.WindowStyle = ProcessWindowStyle.Hidden;
  69.            v= Process.Start(info1);
  70.  
  71.            string line;
  72.            maquina = new string[50];
  73.  
  74.            v.WaitForExit();
  75.  
  76.            StreamReader file = new StreamReader("maquinas.txt");
  77.  
  78.            while ((line = file.ReadLine()) != null)
  79.            {
  80.                if (line.Contains("\\"))
  81.                {
  82.                    maquina[i] = (line.Substring(2, line.Length - 2)).TrimEnd();
  83.                    Console.WriteLine(i + " : " + maquina[i]);
  84.                    i++;
  85.                }
  86.            }
  87.  
  88.            file.Close();
  89.  
  90.            try { File.Delete("maquinas.txt"); } catch{};
  91.        }
  92.  
  93.        static void netUser()
  94.        {
  95.            ProcessStartInfo user = new ProcessStartInfo("CMD.EXE", "/C net user > user.txt");
  96.            user.Verb = "open";
  97.            user.WindowStyle = ProcessWindowStyle.Hidden;
  98.            u= Process.Start(user);
  99.  
  100.            u.WaitForExit();
  101.  
  102.            string path = "user.txt";
  103.            string readText = File.ReadAllText(path, Encoding.UTF8);
  104.            usuario = readText.Substring(26, 10).ToString().ToUpper();
  105.  
  106.            try { File.Delete("user.txt"); }catch { };
  107.        }
  108.  
  109.  
  110.    }
  111. }
  112.  


Cabe resaltar que debes tener privilegios en la maquina remota.


Título: Re: Aporte-Duda DownNET
Publicado por: GonzaFz en 6 Noviembre 2012, 03:35 am
Wow muy interesante hasta la parte de que hay que tener privilegios en las otras computadoras :S!
¿Que tipo de privilegios? Es que en mi colegio tenemos las netbooks de conectar igualdad y casi nadie tiene contraseña en su computadora, cuando quieres ingresar a la misma te pide usuario y contraseña. El usuario es Alumno pero la contraseña no tiene y si la dejas en blanco no funciona entonces no se como hacer..

Si me ayudas con eso y le puedo apagar la netbook al profesor (porque el muy maldito me habia quitado mi netbook) te agradeceria jaja!
Otra cosa, al que le apagas tiene alguna señal de que tu lo hiciste? Y una funcion que se podria agregar es elegir que computadoras deseas apagar, como por ejemplo elijas Todas o X computadora.

Saludos!


Título: Re: Aporte-Duda DownNET
Publicado por: Novlucker en 7 Noviembre 2012, 15:28 pm
No lo he probado, pero por que redirigir la salida de los comandos "net view" y "net user" a archivos de texto en lugar de usarlos directamente desde la aplicación?

Saludos


Título: Re: Aporte-Duda DownNET
Publicado por: Keyen Night en 7 Noviembre 2012, 16:06 pm
Novlucker se refiere a obtener los datos por el StandardOutput Stream así como lo hizo aquí:

http://foro.elhacker.net/net/systemdiagnosticsprocess_y_office_2010-t374970.0.html;msg1797411#msg1797411