how to get a device name for a given ip address - c#

I need to get the network device name for the given IP address.For example a printer connected in a network.I have the IP address of the printer but I need to access its name using the corresponding IP address.
I tried with dns.getHostByaddress(), but it didn't worked out properly.Let me know if any other solution exists.
string c = Dns.GetHostByAddress("xxx.xxx.x.xxx").HostName;
pdoc.PrinterSettings.PrinterName =c ;
It is a windows stand alone application using c# vs2010.

You cannot get the "DeviceName" by the ip itself. DNS-Names only exist at a DNS-Server (or netbios in such an environment) which hold the information about it - if you call "Dns.GetHostByAddress" you are always query the DNS-Server. If you are using DHCP on the devices, the dns entry will be updated automatically (if configured well). Else you have to configure the DNS-Server ip-address on the device and hope that this device is able to Register at the DNS (permissions and supported by your device).
Another way (which i not prefer) is to add the entries to the DNS manually.
You can force dnsregistration on a Windows machine by executing "ipconfig /registerdns"

Related

c# dns.GetHostEntry() not returning valid ip address

I inherited code that makes the call to Dns.GetHostEntry("10.1.12.180") (or by using the dns of the machine) and the IPHostEntry that is returned has a different ip address "10.100.160.18" If I run the code on the same subdomain (10.1.12) it works. Another developer using WireShark said he doesn't see the call being made and that I must be overriding the GetHostEntry call to return that specific address (we used to have that address on the network). I'm not overriding the call.
I know that I can circumvent the issue by using IPAddress.Parse() since I do have the correct ip address.
Any ideas why it would return the incorrect ip? It used to work. I have checked with IT and they don't have any mappings to the incorrect IP address. The only difference it that the machine with ip address 10.1.12.180 was updated to windows 10 (from Windows 7).
I have isolated the call so that I have a project that just makes the Dns.GetHostEntry() call.
IPHostEntry returns a list of addresses, not a single IP address. You should iterate IPHostEntry.AddressList to see all of them.
See here for example usage of Dns.GetHostEntry.
Also be aware Windows can override DNS lookups via the hosts file. This is, unfortunately, a common way to hijack DNS lookups on a specific machine.

Max Mind Geolocation anomalies

I have a site that geolocates the user based on the IP Address using a MaxMind local database. I am finding the results on my desktop to be different than when on a phone. For example:
I am geolocating using the X-Forwarded-For header
My phone locates me in the zip code 18036, which is about 50 miles
off.
I look up the ip address on my phone using IPv4/IPv6 Dual-Stack Test
Using a browser extension on a desktop, I set my X-Forwarded-For header to the same IP Address
The desktop places me in 19428, which is still wrong but is MUCH
closer to correct
Why is there a difference?
Here is how I am performing my lookup:
var ip = httpContext.Request.Headers["X-Forwarded-For"];
CityResponse cityResponse;
using (_dbReader = new DatabaseReader(fullPath, FileAccessMode.Memory))
{
cityResponse = _dbReader.City(ip);
}
UPDATE
Is your phone connected to the same WiFi as your desktop? Or is it connected over 3G/4G?
No. To ensure phone and desktop were looking at the same things:
My site explicitly looks at IP for location - I am not tapping into browser location services, which may use a variety of methods to determine your location.
When searching on my phone, I turned off wifi so it was definitely using 3G/4G
After locating on my phone, I retrived my phone's IP address using "What my IP" in a browser
I spoofed the phone's IP in my browser using the X-Forwarded-For header and ensured my website was looking at that header instead of the actual IP (which would have been an internal IP)
If you are using Wifi, and on the same network as your computer, there won't be any difference because the public IP address (WAN IP address) will be the same.
If your phone is on a network other than the one your computer is on, there's no telling which ISP you would be assigned to (at the given time) and which IP address was assigned to your phone. If a cellular tower happens to be down or overloaded, your connection would be

.NET Remoting Bind to All NICs

I am working on my companies .NET remoting server application to try and make it bind to all available NICs, so that it listens on multiple subnets.
The current way we do it is by telling the server to bind to a specific IP address and that has worked in the past, but when hosted on a server with multiple subnets the clients all need to be on the same subnet or they won't be able to speak to the server.
One solution I tried was to tell the server to bind to the machine name of the server it was running on using the following code:
// If there is an IP present in the config to bind to, then bind on that specific IP.
if (!string.IsNullOrEmpty(remotingBindTo))
{
props.Add("bindTo", remotingBindTo);
}
// Otherwise bind on the machine name, meaning the server should bind on all IP addresses.
else
{
props.Add("machineName", Environment.MachineName);
props.Add("useIpAddress", false);
}
// Create a new channel
IChannel channel = new TcpServerChannel(props, null);
// Register the channel
ChannelServices.RegisterChannel(channel, remotingSecure);
That basically says if the servers config setting "RemotingBindTo" is set then we bind to the provided IP address otherwise we bind to the machine name.
The problem with this is if you use a VPN connection to connect a client to the server. When the client is creating a proxy to a server object it is trying to connect via the machine name and the client PC can not resolve that to an IP address. A work around to this was to place the name into the Windows hosts file and tell it to resolve to the VPN IP address, but because the VPN gives a different IP address every time this is not a viable solution.
I suppose my question is: Is it possible AT ALL to get a .NET remoting server to bind to all available NICs/IP addresses?
Has anyone else experienced anything similar?

How to get my correct IP address?

I try to get my correct IP but I can't
I'm using this code:
{
IPHostEntry host;
string localIP = "?";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
localIP = ip.ToString();
}
}
return localIP;
}
But it does not work for me!
For example my real IP is 151.246.147.86 but with this code I get 192.168.1.2.
Note: I have 4 Network adapters and in DOC with IPConfig I see this:
Network adapter: Local Area Connection 6
Media state : Media disconnected
Network adapter: Local Area Connection 4
Media state : Media disconnected
Network adapter: Local Area Connection 3
IP Address: 10.10.255.222
Network adapter: Local Area Connection
IP Address: 192.168.1.2
Now I connect to net and using with "Local Area Connection"; the public IP of my WAN connection is 151.246.147.86. I want to get this public IP (151.246.147.86), how can I do that?
Note : I don't want (and I can't, since I am using a VPN) use third party websites to get my IP
Please help!
-------EDIT :-------------
Note : I using from VPN and my VPN IP (for example) is : 176.227.197.111. But the IP of my WAN is: 151.246.147.86 and i want to get this address.
You (probably) are using a router/modem, therefore 192.168.1.2 is your "real IP". Your modem/router will be on the same (private) network, and its public interface (WAN) will have a public IP. So you need to get the IP on the public interface of your modem/router.
How to get the WAN IP of your modem depends on your mark and model; if it supports UPnP you can probably do it, or maybe if it is an enterprise class router it may also support SNMP...
You should specify your make and model.
Another way without using external sites: do a tracert to a known site (google?) the first hop will be your route.
Also, if you are on a VPN, you may be able to use the same technique. For example, if you know another host on the VPN (A server maybe?) you can ping/tracert it and discover your router from there. I don't know if in this case you will obtain what you call "a real IP" (by the way, how do you know this IP in the first place? You may be able to obtain it in the same way, programmatically).
Another solution for your VPN-based scenario: you can use Windows to help you. Windows has some kind of VPN management (RAS) which may help you; I would suggest starting from here to understand the basics, and then look for a library/SDK to help you (a quick google returned http://dotras.codeplex.com/).
What you ask is not possible unless there is some way to query your router/modem/external-most endpoint for its WAN address. The only IP address your computer knows about is its own (internal IP).
Technical note: there is nothing non-"real" about the IP address 192.168.1.2 - this is your computer's address. It is simply local to your given internal network and all but useless to anything outside.
If your router supports uPnP, you will need to query GetExternalIPAddress (starting point Is there a UPnP Library for .NET (C# or VB.NET)?). However since uPnP is considered dangerous and many security-conscious users turn it off, I would not count on it being enabled.
Querying an external service will be your most reliable bet for getting your external IP, whether it is one you write, or a third party service (see also https://superuser.com/questions/420969/services-that-just-return-your-public-ip-as-text).
There are many methods and tutorials that teach you how to find an ip address,I use this tool http://www.ipgp.net/ to display information about any ip address, just enter any IP address into the box and you will get the country, city, region, ISP, street address and the satellite location map for every query.
you're trying to get your IP address from your computer or a server? It will make a lot of difference. If you're trying to get your IP address from your computer/laptop use http://www.howtochangeipaddress.com. Your IP will pop up in front of you soon you enter the site.

C# - How to get IP Address when connected to (RAS) VPN

Good afternoon,
Can anyone give any examples of how to obtain the IP Address of the local machine when it's connected to a remote windows domain network via VPN (RAS)? i.e. I need the VPN address and not the remote users local network address.
For example, my Server Side Windows Service communicates with my client side application and needs to create a log of all connected users and their IP Addresses.
This solution is easy enough when using a computer on the local network, but I wondered how I can go about getting the IP addresses of the users who are connected to the server via VPN. Please note that the IP address get method will be executed client side and sent to the server.
Here's my current code that works only when locally connected to the domain network:
public static string GetLocalIPv4()
{
string ipv4Address = String.Empty;
foreach (IPAddress currrentIPAddress in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (currrentIPAddress.AddressFamily.ToString() == System.Net.Sockets.AddressFamily.InterNetwork.ToString())
{
ipv4Address = currrentIPAddress.ToString();
break;
}
}
return ipv4Address;
}
Our internal network is controlled by Windows SBS and uses a domain such as mycompany.local.
Thank you very much for your time and I look forward to reading your responses.
Rob
As the comment from #MarcB notes, cannot think of a good reason why you might want that info... so would be interesting if you could explain a use for this in an application just out of curiosity.
However, there are a lot of incorrect answers on here and online regarding enumerating IP addresses for a machine by using Dns.GetHostAddresses. It appears most people are not realizing the difference between looking up a machine name in the configured DNS resolver versus enumerating the machine address. These are very different things and while it might seem to return the right results in many cases, this is absolutely the wrong way to go about it because they are not the same thing. For example, see this link to an article on here where the original poster flagged an incorrect response as the answer but the correct response is below by #StephanCleary. See:
Get IPv4 Addresses
The difference is you want to look at the machines configuration and enumerate whatever IP address you are interested in locating from the machines own TCPIP stack. The code above and many of the incorrect responses try to lookup the name in the DNS resolver. Once you have that part correct, then you should be able to determine the VPN connection based on the network adaptor (by name or other attribute).

Categories

Resources