Avatar billede j3ppah Novice
06. februar 2009 - 12:52 Der er 6 kommentarer og
1 løsning

Read %ERRORLEVEL%

Dav eksperter... har et mindre problem. har skrevet noget kode som skal pigne osv.. problemet er i 3 while sætning at jeg ikke kan få den til at læse ERRORLEVEL og få den til at arbejde på det i en switch

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;


namespace Online_check
{
    class Program
    {

        static void Main(string[] args)
        {
            int a = 10;
            int b = 129;
            int c = 1;
            int d = 1;

            string Errorlevelstring;
            int Errorlevel = 2;
           
            while (a == 10 && b < 255 && c < 255 && d < 256)
            {

                string ip = a + "." + b + "." + c + "." + d;
                Process p = new Process();
                p.StartInfo = new ProcessStartInfo("cmd", "/c \"ping -n 1 " + ip + "\"")

                {

                    RedirectStandardOutput = true,

                    UseShellExecute = false,

                    CreateNoWindow = true

                };

                p.Start();

                string output = p.StandardOutput.ReadToEnd();

                p.WaitForExit();

                Console.WriteLine(output);
                //Console.ReadLine();
                if (output.Contains("TTL expired"))
                {
                    Console.WriteLine("Nothing on "+ip);
                }
                if (output.Contains("TTL="))
            {
                    Console.WriteLine("Success "+ip);
                    Console.WriteLine("Pinging network "+ip);
                    while (a == 10 && d < 255)
                    {
                        string ip1 = a + "." + b + "." + c + "." + d;
                        Process p1 = new Process();
                        p1.StartInfo = new ProcessStartInfo("cmd", "/c \"ping -n 1 " + ip1 + "\"")

                        {

                            RedirectStandardOutput = true,

                            UseShellExecute = false,

                            CreateNoWindow = true

                        };

                        p1.Start();
                       
                        string output1 = p1.StandardOutput.ReadToEnd();

                        Process p3 = new Process();
                        p3.StartInfo = new ProcessStartInfo("cmd", "/c \"%ERRORLEVEL%")
                        {

                            RedirectStandardOutput = true,

                            UseShellExecute = false,

                            CreateNoWindow = true

                        };
                        p3.Start();


                        Errorlevelstring = p.StandardOutput.ReadToEnd();
                        if (Errorlevelstring.Contains("1"))
                        {
                            Errorlevel = 1;
                        }
                        if (Errorlevelstring.Contains("2"))
                        {
                            Errorlevel = 2;
                        }

                        p.WaitForExit();
                        switch (Errorlevel)
                        {
                            case 1:
                                {
                                    Console.WriteLine("Nothing on " + ip1);
                                    break;
                                }
                            case 0:
                                {
                                    Console.WriteLine("Success " + ip1);
                                    break;
                                }
                            default:
                                {
                                    break;
                                }
                        }
                        d++;
                    }
                }

                //Console.Read();
                c++;
            }
        }
    }
}
Avatar billede j3ppah Novice
06. februar 2009 - 13:15 #1
Ps. OG JA. Ved godt det var nemmere at bruge .net'liben. Men fandt jeg først ud af for sent :D
Avatar billede arne_v Ekspert
06. februar 2009 - 15:26 #2
Hvad med:

Environment.GetEnvironmentVariable("ERRORLEVEL")

?
Avatar billede arne_v Ekspert
06. februar 2009 - 15:26 #3
Og var det ikke hurtigere at omskrive til at bruge .NET ping end at kaempe med den status ?
Avatar billede j3ppah Novice
08. februar 2009 - 23:11 #4
Environment.GetEnvironmentVariable("ERRORLEVEL")
virker ikke optimalt :/...

Hmm, kan godt være jeg skal tage mig sammen og gører det arne... Ville nu ellers være rart at kunne gøre det på denne måde ;).. er der også lidt mere udfordring i ;)!
Avatar billede arne_v Ekspert
09. februar 2009 - 01:01 #5
Hvad giver:

p1.Start();
p1.WaitForExit();
int res = p1.ExitCode;

?
Avatar billede j3ppah Novice
10. februar 2009 - 17:08 #6
Arne, jeg vender lige tilbage til det, tror det kommer til at virke, men har lige ferie nu, så checker det lige når jeg kommer tilbage fra ferien.
Avatar billede j3ppah Novice
16. februar 2009 - 11:46 #7
Fandt en anden løsning, da jeg ikke kunne få det andet til at virke.

Løsningen var som følger...



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;


namespace Online_check
{
    class Program
    {

        static void Main(string[] args)
        {
            int a = 10;
            int b = 129;
            int c = 1;
            int d = 1;
            int d1 = 1;
           
            while (a == 10 && b < 255 && c < 255 && d < 256)
            {

                string ip = a + "." + b + "." + c + "." + d;
                Process p = new Process();
                p.StartInfo = new ProcessStartInfo("cmd", "/c \"ping -n 1 " + ip + "\"")

                {

                    RedirectStandardOutput = true,

                    UseShellExecute = false,

                    CreateNoWindow = true

                };

                p.Start();
               
                string output = p.StandardOutput.ReadToEnd();

                p.WaitForExit();

                //Console.WriteLine(output);
                //Console.ReadLine();
                if (output.Contains("TTL expired"))
                {
                    Console.WriteLine("Nothing on "+ip);
                }

                if (output.Contains("TTL="))
                {
                    Console.WriteLine("Success " + ip);
                    Console.WriteLine("\nPinging network " + ip);
                    while (a == 10 && d1 < 255)
                    {
                        string ip1 = a + "." + b + "." + c + "." + d1;
                        Process p1 = new Process();
                        p1.StartInfo = new ProcessStartInfo("cmd", "/c \"ping -n 1 " + ip1 + "\"")

                        {

                            RedirectStandardOutput = true,

                            UseShellExecute = false,

                            CreateNoWindow = true

                        };

                        p1.Start();

                        string output1 = p1.StandardOutput.ReadToEnd();

                        p1.WaitForExit();

                        if (output1.Contains("Request timed out"))
                        {
                            Console.WriteLine("    Nothing on " + ip1);
                        }
                        if (output1.Contains("Reply from"))
                        {
                            Console.WriteLine("            Success " + ip1);
                        }
                        d1++;


                    }
                }
                if (output.Contains("100% loss"))
                {
                    Console.WriteLine("Nothing on"+ip);
                }
                c++;
                d1 = 1;
            }
        }
    }
}
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester