-2

I have to get the Router/GateWay IP and then incrementing the last octet everytime to scan all the devices.I get the GateWay IP using a function NetworkGateway().

        //gets the GateWay IP
        string gate_ip= NetworkGateway();

        //mechanism to parse ,get the last octet and increment it everytime goes here so that    ping_var = "192.168.0" + "." + i; have a fixed "192.168.0"

            for (int i = 2; i <= 255; i++)
            {
                 //changes i for every unique device
                string ping_var = "192.168.0" + "." + i;
            }




     IPAddress address = IPAddress.Parse(ipAddress);


  // gives 192.168.0.1 when passed "192.168.0.1 "
  Console.WriteLine("Parsing your input string: " + "\"" + ipAddress + "\"" + " produces this address (shown in its standard notation): "+ address.ToString());


//Returns  the Bytes of IP
     Byte[] bytes = address.GetAddressBytes();
    for (int i = 0; i < bytes.Length; i++) 
       {
         Console.Write(bytes[i]);
       }  

For this purpose I used firstly IPAddress.Parse(ipAddress) and then GetAddressBytes() but it just returned xxxxxxxx which couldn't be used for incrementing.

The AddOne method suggested here at SO also didn't return it.

Any good idea.

Community
  • 1
  • 1
Khan
  • 185
  • 1
  • 4
  • 15
  • Where's the relevant code? `IPAddress.Parse` isn't a new method, if it didn't work people would have noticed a decade ago. Also what does `xxx` mean? `GetAddressBytes returns a byte array, not a string. – Panagiotis Kanavos Dec 18 '14 at 09:47
  • Updated the code above –  Khan Dec 18 '14 at 10:05
  • `GetAddressBytes()` gives `19216801` when passed to it a parameter `192.168.0.1` .What can i do of `19216801` to get the requirement –  Khan Dec 18 '14 at 10:06
  • As I said, GetAddressBytes returns a byte array. `19216801` isn't a byte array. You still haven't posted the *relevant* code, so helping you is very hard. You can easily change the value of the last byte from 1 to 255 and create a new IPAddress from the array – Panagiotis Kanavos Dec 18 '14 at 10:12
  • Okay i did it and posted as an answer –  Khan Dec 18 '14 at 10:29
  • You may check the updated Question code now and revive your downvote ..Also,yeah agree `GetAddressBytes` returns a byte array, not a string. –  Khan Dec 18 '14 at 10:34
  • I didn't downvote, although the code is still not relevant to the actual question. I still think you misunderstood what each class, method does but you never posted the code so ... – Panagiotis Kanavos Dec 18 '14 at 10:38
  • I will try it later and play with it...but for now i got what i needed .Thank YOu –  Khan Dec 18 '14 at 10:45

1 Answers1

0

Here's a simple use of Split() and i did it .

    string gate_ip= NetworkGateway();
        string[] array = gate_ip.Split('.');

            for (int i = 2; i <= 255; i++)
            {

                string ping_var = array[0] +"."+array[1]+"."+array[2]+ "." + i;
                // do scanning here

            }
Khan
  • 185
  • 1
  • 4
  • 15