Scanneur de réseau

Description

Ce programme permet de scanner un réseau est d'en ressortir les adresses ip des hôtes et leurs adresses mac. Un classe particulière a été créé pour les fonctions qui touche la réseautique:
- Faire un ping
- A partir d'une adresse ip, avoir l'adresse mac
- Calculer première et dernière adresse ip selon le masque
- calculer le nombre d'hôte ...

Source / Exemple :

//--------------------Programme------------------------------------
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
            
        }

        int AdresseIpDebut, AdresseIpDebut2, AdresseIpDebut3, AdresseIpDebut4, AdresseIpFin, AdresseIpFin4, AdresseIpFin2, AdresseIpFin3;
        int Out;
        int NbDAdresse;
        PingReply maReponsePing;
        Ping monPing = new Ping();
        Thread myThread;
        String Ip;
        int IpPlus, IpPlus2, IpPlus3, IpPlus4;
        double resultat;
           
           public int NombreDAdresse(int Ip1, int Ip2)
            { 
                Out= Ip2 - Ip1;
                return Out;  
            }
            
            public int Convertion(String Nb)
            {
                return Convert.ToInt16(Nb);
            }
           
            public  void ThreadLoop()
            {

                while (Thread.CurrentThread.IsAlive)
                {
                    //Thread.Sleep(500);

                    AdresseIp();
                    try
                        {
                           
                            maReponsePing = monPing.Send(Ip, 4);
                            
                            if (maReponsePing.Status == IPStatus.Success)
                            {
                                AppendTextBox(Ip);
                            } 
                        }
                        catch { }

                }
                MessageBox.Show("Scanne terminé");
                myThread.Abort();
                   
            }

            private void button3_Click(object sender, EventArgs e)
            {
                myThread.Abort();
                progressBar1.Value = 0;
            }
            public void AppendTextBox(string value)
            {
                if (InvokeRequired)
                {
                    this.Invoke(new Action<string>(AppendTextBox), new object[] { value });
                    return;
                }
                
                //----------------------------------------------------------
                ProcessStartInfo startInfo = new ProcessStartInfo();
                // startInfo.WorkingDirectory = MonAppli.workingDirectory;
                Process process = new Process();
                startInfo.FileName = "arp";
                startInfo.Arguments = " -a " + Ip;

                startInfo.RedirectStandardOutput = true;
                startInfo.CreateNoWindow = true;

                startInfo.UseShellExecute = false;

                process = Process.Start(startInfo);
                process.WaitForExit();
                string stdout;
                try
                {
                    stdout = process.StandardOutput.ReadToEnd();
                    stdout = stdout.Substring(112, 17);
                }
                catch
                {
                    stdout = "?";
                }

                process.Close();

                //-------------------------------------------------------------
                richTextBox1.Text += value +"                               " + stdout + System.Environment.NewLine;
                richTextBox1.Text += "--------------------------------------------------------------------------------------------------" 
                + System.Environment.NewLine;
            }

            public void BardeProgression(int value)
            {
                if (InvokeRequired)
                {
                    this.Invoke(new Action<int>(BardeProgression), new object[] { value });
                    return;
                }
                progressBar1.Value = value;
            }
            public String AdresseIp()
            {

                if (IpPlus4 >= AdresseIpFin4 && IpPlus3 >= AdresseIpFin3 && IpPlus2 >= AdresseIpFin2 && IpPlus >= AdresseIpFin)
                {
                    IpPlus4 = AdresseIpFin4;
                    IpPlus3 = AdresseIpFin3;
                    IpPlus2 = AdresseIpFin2;
                    IpPlus = AdresseIpFin;
                    myThread.Abort();
                }
                else if (IpPlus4 <= AdresseIpFin4 || IpPlus3 <= AdresseIpFin3 || IpPlus2 <= AdresseIpFin2)
                {
                   
                    IpPlus++;
                    if (IpPlus > 254)
                    {
                        IpPlus = 0;
                        IpPlus2++;
                    }
                    if (IpPlus2 > 254)
                    {
                        IpPlus2 = 0;
                        IpPlus3++;
                    }
                    if (IpPlus3 > 254)
                    {
                        IpPlus3 = 0;
                        IpPlus4++;
                    }
                     
                }
                

                Ip = IpPlus4.ToString() + "." + IpPlus3.ToString() + "." + IpPlus2.ToString() + "." + IpPlus.ToString();
                return Ip ;
            }

           
           
            

            private void button4_Click(object sender, EventArgs e)
            {
                AdresseIpDebut = Convertion(textBox1.Text);
                IpPlus = AdresseIpDebut;
                AdresseIpDebut2 = Convertion(textBox3.Text);
                IpPlus2 = AdresseIpDebut2;
                AdresseIpDebut3 = Convertion(textBox4.Text);
                IpPlus3 = AdresseIpDebut3;
                AdresseIpDebut4 = Convertion(textBox5.Text);
                IpPlus4 = AdresseIpDebut4;

                AdresseIpFin = Convertion(textBox2.Text);
                AdresseIpFin2 = Convertion(textBox6.Text);
                AdresseIpFin3 = Convertion(textBox7.Text);
                AdresseIpFin4 = Convertion(textBox8.Text);
            }

            public String NbDAdresses(int a, int b, int c, int d)
            {
                return  Binaire(a) + Binaire(b) + Binaire(c) + Binaire(d);
            }
            public String Binaire(int Bin)
            {
                string StBin;
                StBin=Convert.ToString(Bin, 2);
                while (StBin.Length != 8)
                {
                    StBin = "0" + StBin;
                }
                return StBin;
            }
            public double BinEnDec(string a)
            {
                char[] c = a.ToCharArray();
                resultat = 0;

                for (int i = 0; i <= 7; i++)
                {
                    if (c[i] == '1')
                    {
                        resultat = resultat + Math.Pow(2, i);
                    }
                }
                return resultat;
                  
            }
            
    }
}
//-----------------------------------------Classe---------------------------------------------
class Reseau
    {
        
            static public String AdresseMac(string Ip) 
            {
                //-----------------------------------------------------
                //-----------------------------------------------------
                //-----------------------------------------------------
                /*
                Fonction qui retourne l'adresse mac par commande Ms-Dos
                string Ip correspond à l'adresse ip de l'adresse mac chercher
                using System.Diagnostics;
                retourne l'adresse mac
                */
                //-----------------------------------------------------
                //-----------------------------------------------------
                //-----------------------------------------------------
                ProcessStartInfo startInfo = new ProcessStartInfo();
                Process process = new Process();

                startInfo.FileName = "arp";
                startInfo.Arguments = " -a " + Ip;

                startInfo.RedirectStandardOutput = true;
                startInfo.CreateNoWindow = true;

                startInfo.UseShellExecute = false;

                process = Process.Start(startInfo);
                process.WaitForExit();
                string AdMac;
                try
                {
                    AdMac = process.StandardOutput.ReadToEnd();
                    AdMac = AdMac.Substring(112, 17);
                }
                catch
                {
                    AdMac = "----";
                }

                process.Close();
                return AdMac;
        }

            static public String Ping(string Ip)
            {
                //-----------------------------------------------------
                //-----------------------------------------------------
                //-----------------------------------------------------
                /*
                Fonction qui effectu un ping d'une adresse ip
                string Ip correspond à l'adresse ip ou l'on veut effectuer le ping
                using System.Net.NetworkInformation;
                retourne un 1 si le ping fonctionne et un 0 si le ping ne fonctionne pas
                Inspirer de http://www.codequake.com/post/Comment-faire-un-Ping-en-C-.aspx
                */
                //-----------------------------------------------------
                //-----------------------------------------------------
                //-----------------------------------------------------
                PingReply maReponsePing;
                Ping monPing = new Ping();

                maReponsePing = monPing.Send(Ip, 4);
                
                if (maReponsePing.Status == IPStatus.Success)
                {
                    return "1";
                }
                else
                {
                    return "0";
                }

            }

            static public int AdresseIp(string Ip, int Masque, out string IpDebut, out string IpFin)
            {
                //-----------------------------------------------------
                //-----------------------------------------------------
                //-----------------------------------------------------
                /*
                Fonction qui retourne la premier adresse ip et la derniere
                string Ip correspond à l'adresse ip de l'adresse
                int Masque correspond au masque ex: /25->25
                out string IpDebut correspond a l'adresse ip de debut
                out string IpFin correspond a l'adresse ip de fin
                using System.Net;
                Inspirer de http://stackoverflow.com/questions/1470792/how-to-calculate-the-ip-range-when-the-ip-address-and-the-netmask-is-given
                */
                //-----------------------------------------------------
                //-----------------------------------------------------
                //-----------------------------------------------------
                
                var ip = IPAddress.Parse(Ip);
                int bits = Masque;
                
                uint mask = ~(uint.MaxValue >> bits);

                // Convert the IP address to bytes.
                byte[] ipBytes = ip.GetAddressBytes();

                // BitConverter gives bytes in opposite order to GetAddressBytes().
                byte[] maskBytes = BitConverter.GetBytes(mask).Reverse().ToArray();

                byte[] startIPBytes = new byte[ipBytes.Length];
                byte[] endIPBytes = new byte[ipBytes.Length];

                // Calculate the bytes of the start and end IP addresses.
                for (int i = 0; i < ipBytes.Length; i++)
                {
                    startIPBytes[i] = (byte)(ipBytes[i] & maskBytes[i]);
                    endIPBytes[i] = (byte)(ipBytes[i] | ~maskBytes[i]);
                }

                // Convert the bytes to IP addresses.
                IPAddress startIP = new IPAddress(startIPBytes);
                IPAddress endIP = new IPAddress(endIPBytes);
                IpDebut = startIP.ToString();
                IpFin = endIP.ToString();

                
                double resultat=-2;
                resultat = resultat + Math.Pow(2, (32 - bits));

                return Convert.ToInt32(resultat);
            }
           
            static public void IpSeparateur(string IpDebut, String IpFin, out int IpD4, out int IpD3, out int IpD2, out int IpD1, out int IpF4, out int IpF3, out int IpF2, out int IpF1)
        {
            var results1 = IpDebut.Split('.');
            var results2 = IpFin.Split('.');

            string NamedPartA = results1[0];
            IpD4 = Convert.ToInt16(NamedPartA);

            string NamedpartB = results1[1];
            IpD3 = Convert.ToInt16(NamedpartB);

            string NamedPartC = results1[2];
            IpD2 = Convert.ToInt16(NamedPartC);

            string NamedpartD = results1[3];
            IpD1 = Convert.ToInt16(NamedpartD);

            string NamedPartAA = results2[0];
            IpF4 = Convert.ToInt16(NamedPartAA);

            string NamedpartBB = results2[1];
            IpF3 = Convert.ToInt16(NamedpartBB);

            string NamedPartCC = results2[2];
            IpF2 = Convert.ToInt16(NamedPartCC);

            string NamedpartDD = results2[3];
            IpF1 = Convert.ToInt16(NamedpartDD);
        }

            

    }
}

Conclusion :

Avez vous des améliorations à me proposer ?

Ps: a partir de quand on peut se qualifier de niveau d'initié et non débutant ?

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.