views:

993

answers:

1

Hey all. I have written a program that sequentially scans certain parts of a LAN for computers (code will be provided). However, when I run this code, it only returns the DNS HostName of the computer it is running on. I looked into using WMI, but I cannot, as I will not always have priveleges to the computers being found. Is there any other way to find a local computers HostName?

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

namespace CheckLocalNetwork
{
    class PingCheck
    {
        public static string fullip;

        public void CheckSequentialIP()
        {
            IPHostEntry IpEntry = Dns.GetHostEntry(fullip);

            Ping pingSender = new Ping();

            PingOptions options = new PingOptions();
            options.DontFragment = true;

            string data = "a";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            int timeout = 120;
            PingReply reply = pingSender.Send(fullip, timeout, buffer, options);

            if (reply.Status == IPStatus.Success)
            {
                Console.WriteLine("Address: {0}", reply.Address.ToString());
                Console.WriteLine("Host Name: {0}", IpEntry.HostName);
                Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
                Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
                Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
                Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
                Console.WriteLine("");
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Press enter to search for ip adresses that begin with 192.168.1");
            Console.ReadLine();

            for (int endofip = 1; endofip < 101; endofip++)
            {
                fullip = "192.168.1." + Convert.ToString(endofip);
                PingCheck checkfullip = new PingCheck();
                checkfullip.CheckSequentialIP();

            }
            Console.ReadLine();
    }

All help is much appreciated.

+1  A: 

Hmm - your code sample behaves as expected on my machine - i.e. it returns the hostname of the machine being scanned.

To investigate your problem deeper, have you tried using nslookup to check the ip addresses resolve?

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Rob>nslookup                  <-- type this at a command prompt
Default Server:  mydns.mydomain.co.uk  <--- these two lines indicate the dns server being used to resolve your queries
Address:  192.168.0.1                  <----|

> 192.168.0.5                          <---- type in the ip address of one of the machines in question
Server:  mydns.mydomain.co.uk
Address:  192.168.0.1

Name:    myworkstation.mydomain.co.uk  <---- this is the hostname, as reported by the DNS using a reverse lookup
Address:  192.168.0.5

If this doesn't return the machine name, then you may have a name resolution issue that is not related to your code.

If this all looks ok, then it might also be worth enumerating the IpEntry.Aliases collection. Are there any entries here, and do they make sense?

Finally - is the code you have above exactly the code that is going wrong for you, or is it a "distilled" example? The reason I ask is that the documentation for Dns.GetHostEntry states

"When an empty string is passed as the host name, this method returns the IPv4 addresses of the local host."

I also notice you're holding "fullip" in a static. If this is not the exact code that is causing the problem, especially if this runs multithreaded, is there a chance you are not initialising "fullip" before the Dns.GetHostEntry is called?

I may be way off, but I thought is was worth giving a brain dump of what occured to me as I looked at your problem :)

[EDIT:] - your comment to kdt has clarified something I misunderstood. I thought you were saying you always got back the hostname for your local machine, no matter which machine you "scanned" - which is very odd behaviour. In fact I think you are saying you just get back IP addresses for other machines (their IP address), and only get a hostname for your local. Disregard my last bit about the threading and the empty argument.

This is far more easily explained - your machine is almost certainly just not able to resolve the machine names - I expect my nslookup test I suggested will not return the machine names either.

In order to resolve these IP's to host names, your machine needs a DNS that has entries for these machines, or to have them in its local hosts file; your machine isn't actually asking the remote machine for its name when you do this call so it won;t be able to find it out without help from one of its usual name resolution paths.

It works for me, because my local DNS really does have entries for all the machines on my network, resolving their host names to ip addresses and vice-versa.

Rob Levine
I was writing another "not so" comment when your edit appeared. And you were right, the nslookup did not return anything useful for the other addresses that had appeared when running my program. That is the program in its entirety. Suggest a correct question to fix this problem, then? A sure way to get the DNS HostName without using WMI is what is needed here, it still sounds like.
Bloodyaugust
The problem here is not really code related - it is to do with your network setup. Basically, if one machine on the network wants the hostname of another, given only its ip address, it only has a few choices. 1) See if there is a mapping in the local hosts file - not helpful in your case. 2) Ask a DNS. Apart from some legacy NetBIOS approaches, that's all you've got. WMI won't help either.This is really a sysadmin issue - your DNS servers should have entries for these machines. I really can't think of a solution without this off the top of my head.
Rob Levine
Thanks very much for all your help.
Bloodyaugust
actually - minor change to my comment above - WMI would help if you could do it as you can use WMI to actually connect to the remote machine and ask it for its host name (I'm pretty sure you can anyway). But between WMI privelege issues you mention, and DNS issues, I fear both solutions are out of your hands.
Rob Levine