I'm new here...been digging around for some help but figured I would join and ask for some guidance.
I'm looking to create an app that can create multiple "fake" devices. They need an IP Address and I'm guessing able to respond to ping. Being able to respond to WMI would also be nice. Kinda like a simulator. I'd like to create up to 50,000 devices but even starting with 1 would help.
What is needed for such an app? TCP Client/Listener? I've never done something like this before so please be gentle :)
You may install Virtual Network Adapter's (driver is included with Windows OS), but i have never used this. Driver for Virtual Network Adapter is here: %WINDIR%\Inf\Netloop.inf
You may use Command line tool called DevCon to add devices by script, like this:
devcon -r install %WINDIR% \Inf\Netloop.inf *MSLOOP
Installation unfortunatelly takes few seconds (on my Core Duo 2.0 laptop).
If you need to configure a lot of network cards you may use command line netsh.
Examples:
netsh in ip set address "Local Area Connection" static 10.0.0.1 255.0.0.0 10.0.0.1 1
netsh in ip add address "Local Area Connection" 10.0.0.2 255.0.0.0
netsh in ip set address "Local Area Connection 2" 10.0.0.3 255.0.0.0
netsh in ip set address "Local Area Connection 3" 10.0.0.4 255.0.0.0
netsh in ip set dns "Local Area Connection" static 10.0.0.250
netsh in ip set wins "Local Area Connection" static 10.0.0.250
You may dump/export current network configuration to a file (to see how current config looks):
netsh interface dump > file.txt
More netsh examples
Edit: removed informations not useful in this case.
If I'm understanding you correctly, unfortunately this will not be easy as you need to virtualize network adapters to do the job you want. an IP address is bound to a nic (physical or logical), not something that can be specified in higher layer code. VMWare Workstation does include a plugin for Visual studio, so perhaps you can use it to generate many virtual nics and assign them ip's programatically, but otherwise you need to write virtual network card drivers (probably in a non-.net language) to do it, if you don't use an existing virtualization tech. you can stack many IP addresses on a nic, but the computer communicating with it will know they are all the same network entity. if thats fine with you, then just add all the IPs you want to the card you have.
on to the second part of your query, since you want the IPs to be able to recieve and send data, their addresses will have to be routable, so you can't just pick any old IP address. if you are fine being behind a NAT wall, you could use 10.x.y.z to address them, but on the outside of the nat they would all appear to be using the same public IP to the outside world. in order to expose 50k publicly routable IP addresses, you would first have to register and buy them.
lastly you can't use TCPClient to do Echo/Ping, since they use the ICMP protocol, but instead use the System.Net and System.Net.NetworkInformation namespace. Here is some VB code to send a ping just to give you the flavor of it:
Imports System
Imports System.Net
Imports System.Net.NetworkInformation
Public Class Pinger
<System.Diagnostics.DebuggerNonUserCode()> _
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
End Sub
Public Shared Function CanHostBePinged(ByVal IPAddr_DNS_OR_Host_Name As String) As Boolean
Dim p As New Ping
Dim po As New PingOptions
po.Ttl = 256
po.DontFragment = False
Dim stringOut As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDE"
Dim streamOut As Byte() = System.Text.Encoding.ASCII.GetBytes(stringOut)
Try
Dim reply As PingReply = p.Send(IPAddr_DNS_OR_Host_Name, 30, streamOut)
If reply.Status = IPStatus.Success Then
Return True
Else
Return False
End If
Catch ex As Exception
Return False
End Try
End Function
End Class
I know this thread is very old, but I am posting my idea for anyone who might visit this question.
The previous answers already made clear that it is very difficult to achieve what the OP is trying to achieve. But I think if anybody needs such functionality for testing purposes there is a very easy way to achieve it.
We can create a simple web app in Node or .NET or whatever environment is comfortable. The web app's UI will allow us to do the following operations.
Create a device with IP
Mark an IP as Online or Offline.
If IP is online it is pingable, else not.
At the same time, the web app also exposes an API that when supplied with an IP says to us whether the IP is pingable or not. This way we mock the ping operation to an IP. Let's name this web app as PingMock.
Now in the original app, we can create a TestPingService which instead of pinging, sends an HTTP request to PingMock. This way we will be able to test our business logic without having access to actual IPs.
For testing business logic the final output of the PingService matters and not from where the output comes. This is how unit testing is conducted.
The PingMock web app is just an example. We can mock the Ping operation in whichever way we like.
Related
I am currently using Httpclient and I can successfully gather my data with a specific network/internet-connection at the place that has the data.
However when I try to gather the data at home with another internet-connection I receive an "NameResolutionFailure" error.
My goal is to be able to reach the data from any type of connection but I am not sure what I am quite missing here. (I am also new in this area).
This is the code that I use when I talk to the db:
string dataurl = "my-url-here";
var http = new HttpClientHandler()
{
Credentials = new NetworkCredential("user", "password", "domain"),
};
var httpClient = new HttpClient(http);
try
{
var result = await httpClient.GetStringAsync(dataurl);
System.Diagnostics.Debug.WriteLine(result);
}
catch (HttpRequestException ex)
{
if (ex.GetBaseException() != null)
{
System.Diagnostics.Debug.WriteLine(ex.GetBaseException().Message); //this is where i recieve the NameResolutionFailure error
}
else
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
How come I can only reach the data when I am on a certain network and not with every network? Am I missing code or is there something else in play here?
Appreciate every help, tips, code-examples i can get!
The problem is likely to be in the string dataurl = "my-url-here"; and where that's accessible from. There are likely to be two obstacles:
Name resolution
Network Access
While your error message only mentions Name resolution, I'm guessing you'll need to do something about network access as well.
Name resolution (or DNS) is about translating a host name into an I.P. address.
When you're on a work network, there'll be a name resolution service that resolves local computer names to I.P. addresses on the network. Normally these local computer names are not visible to public DNS servers. If you connect your device to a different network (e.g. a mobile network), it uses the public DNS servers, which know nothing about the local domain named computers.
For example MyServer might resolve on your local network because it's part of your local domain, and the local network infrastructure will sort that out. MyServer.MyCompany.com is usually similar, as by default machines names aren't exposed externally.
For a mobile application, you're going to need a public domain name. Something like MyServer.MyDomain.com (or www.google.com is the same thing, essential). A public DNS server translates this name to an I.P. address.
This is probably where the problem you're experiencing is occurring. You're probably using a local host name, that the public DNS servers don't know about.
If you're working for an organisation they may already have a domain, or you may need to purchase a domain for your application.
In the meantime you could look at one of the dynamic DNS solutions that may allow you to progress for development purposes.
For my Xamarin app, I use the name of the local machine when I'm developing, and the mobile device is on the same network.
If I'm not on the same network, I have a VPN that I can use. This connects into the work network as if I'm on the same network. If I'm developing at home and both devices are on my home network, I use the I.P. address of my development box, because I haven't made local DNS work on my home wifi.
When we go to release we use a public URL, like api.MyApp.com - which public DNS points to our prod server.
Network Access might be a thing that you need to deal with too.
A major part of a Network Engineer's job is to keep the hackers out. When your mobile device is on the same network as the server (i.e. when it's working for you), this isn't a problem because because mostly networks are configured so that two devices on the same network can see each other. It sounds like this is the sort of network you have, if your app can see your server on one connection.
But if you're needing to connect to your server from a mobile network, you need a way to tell your network router to forward specific traffic from the internet to your server.
This gets complicated, but for development purposes, strategies I've seen work are:
A VPN - we have a VPN that I fire up on the mobile device, enter my work network credentials, and then I can access my development box as if I'm on the same network
Virtual server / port forwarding - if you're at home, you can probably configure your modem to forward a particular port to your development box. Every modem is different, so you'd have to search up instructions for your particular one.
Network Engineer - if you're in a corporate, and want traffic from outside to get to a server that you're managing (and don't have a VPN), you probably need to talk to your networks department. Good luck.
I have a windows form application in C# and I'm trying to get the host name of all the clients that I have in a list. Given below is a code example by ra00l from this link: GetHostEntry is very slow (I have a similar code made but this one is cleaner)
private delegate IPHostEntry GetHostEntryHandler(string ip);
public string GetReverseDNS(string ip, int timeout)
{
try
{
GetHostEntryHandler callback = new GetHostEntryHandler(Dns.GetHostEntry);
IAsyncResult result = callback.BeginInvoke(ip,null,null);
if (result.AsyncWaitHandle.WaitOne(timeout, false))
{
return callback.EndInvoke(result).HostName;
}
else
{
return ip;
}
}
catch(Exception)
{
return ip;
}
}
When it's given an IP of a Windows Machine in the network, it shows the correct host name given that you enter a reasonable timeout. After the tests that I made, I didn't get any response for the host names of android and apple devices. For example, the picture below is the DHCP Client List of the router that I have. It shows android, apple and laptop devices. I'm using the laptop 'Nathu-Laptop' giving me an IP address of '192.168.1.106'.
If I enter '192.168.1.105' in the C# function, the result is 'Nandwani-PC' but if I input '192.168.1.103', '192.168.1.104', '192.168.1.101', '192.168.1.100', I don't get any hostname.
I also tried using nbtstat but it only gets the laptops in the network.
When trying this out on my iPod, I ensure that there is a network activity going on. This is to keep the connection alive because it disconnects from the network when it's on standby.
EDIT:
So I found out that DNS.GetHostEntry calls getaddrinfo if IPv6 is enabled, otherwise, call gethostbyaddr and these functions may access the data from \System32\drivers\etc\hosts or maybe from the NETBIOS. The thing is that the NETBIOS is legacy right? but how about for mobile devices?
About NetBIOS:
In order to answer your specific questions about NetBIOS, and name resolution on network, I'll give more details. If you don't have dns server running on your network, name resolution will rely only on NetBIOS resolution.
It is a standard and implemented on several operating systems. However, it's not very fast.
Even if we're old, we're not legacy from the past and obsolete
You can check on Microsoft support the way names are resolved on Windows and NetBIOS is the last one.
However, NetBios name resolution is not always fully functional (like this bug on Android which was fixed in 2014) on all mobile platforms (depends on Android version for example).
If you want to improve the performance, I suggest you to install a DNS server in the network.
Did you try ping -a <IP address> or nslookup <IP address> to check if the results are in line with your expectations ?
If your problem still persist, then you may investigate the .Net implementation, thanks to the above links. You can also check a more up to date version of .Net DNS implementation here
I have seen many methods for easily retrieving the internal IP address of a machine or website. However, I can't seem to find a good way to retrieve the external IP address.
To clarify, if I provide a URL like bitbucket.org, I want to get the external IP address of bitbucket. Is there a web service out there that can easily do this?
EDIT: Suppose, for this case, that I am on the same network as bitbucket.org.
I am filling a database with information about all the websites our company manages. We want to keep track of the info and note periodic changes, with specific data. This program will be deployed on one of the local servers on the same network as the servers that the websites are running from. I believe the only good way of retrieving the external IP address for each site is to use an external web service.
You can use System.Net.Dns.GetHostEntry() to get IP address by the host name.
You could query an external public DNS server, e.g. Google's one at 8.8.8.8. From the command line
nslookup bitbucket.org 8.8.8.8
or in Linux dig bitbucket.org #8.8.8.8. There's a few C# libraries out there that will let you query a specific DNS server e.g. DnsNet built on top of this CodeProject article (found searching - I haven't tried it to recommend it). This does rely on Google continuing the service, though, but that seems safe.
You can use the ping utility. In windows, open up a command window by hitting the windows key + r and type
ping bitbucket.org
I think you can just use Dns.GetHostAddresses to get the IP Address. From the MSDN Documentation:
The GetHostAddresses method queries a DNS server for the IP addresses associated with a host name.
UPDATE:
If you are looking for a web service, try looking at whoisxmpapi.com. As you can see from this sample, they do provide the name server IP Address in their XML output. You can then use something like this to get the IP address directly from the name server!
If you are trying to get your "wan" ip instead of your local IP you could try something like this.
You could also add code like this inside a webservice and add it to the PC bitbucket is on (if it is really on your network and you can have access to install webservices).
Public Shared ReadOnly Property IpAddress() As String
Get
If String.IsNullOrWhiteSpace(_ipAddress) Then
Try
Dim webClient As New WebClient
Dim result As String = webClient.DownloadString("http://checkip.dyndns.org")
Dim fields = result.Split(" ")
_ipAddress = fields.Last
_ipAddress = _ipAddress.Replace("</body></html>", "")
Catch ex As Exception
_ipAddress = "errorFindingIp"
End Try
End If
Return _ipAddress
End Get
End Property
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.
Got an asp.net web page in c#. One thing we would like to do is track hits to the site including their IP address. I implemented some code (thanks to SO) but the logged IP address always seem to be local, ie: 192.168.x.x. I have tried it from a variety of devices, even my phone and Version MiFi just to make sure its not something weird with the ISP but the log always list the same 2-3 different internal ip addresses (seems to change a little as the day goes on).
Here is my function that gets the IP (again thanks to postings here on SO):
protected IPAddress GetIp(HttpRequest request)
{
string ipString;
if (string.IsNullOrEmpty(request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
ipString = request.ServerVariables["REMOTE_ADDR"];
else
ipString = request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
IPAddress result;
if (!IPAddress.TryParse(ipString, out result))
result = IPAddress.None;
return result;
}
public void logHit()
{
IPAddress ip = GetIp(Request);
string sIP = ip.ToString();
}
I tried this as well which yields the same result:
HttpContext.Current.Request.UserHostAddress;
When I do a call on the client side using something like the service on appspot, it works just fine:
<script type="application/javascript">
function getip(json) {
//txtIP is a input box on the form
document.getElementById("txtIP").value = json.ip;
}
</script>
<script type="application/javascript" src="http://jsonip.appspot.com/?callback=getip"></script>
I suppose I could do a round-about way by hitting that appspot link and parse it out but that seems like a whole lot of trouble for something that should be simple.
Could it be the IIS on the server? Some kind of redirect going on? The ip addresses logged are NOT the servers. The problem is I dont have direct access to it so I have to talk to the guys that admin it and would like to give them some direction before they just start changing things.
Thanks
Ernie
If the HTTP_X_FORWARDED_FOR header is truly supported, then I think this would not be either forward or reverse proxy server causing this, but more likely Dynamic Network Address Translation or Dynamic Port Address Translation, which is happening below the application layer on the TCP/IP stack and thus would not affect an HTTP request header.
There are many ways to configure NAT, most of which would not cause these symptoms, but it is certainly possible to configure NAT in a way that would present this problem. Dynamic NAT or Dynamic PAT would be two such examples, and I would suggest this is what you ask your network administrators.
For more on Dynamic NAT/PAT, with good examples, you could review: http://www.cisco.com/en/US/docs/security/asa/asa82/configuration/guide/nat_dynamic.html
In a typical NAT scenario, the request packets reach the NAT device (firewall or router) as:
FROM - 5.5.5.5 (public address of the client)
TO - 6.6.6.6 (the public address of the server)
The "typical" NAT configuration would rewrite only the destination, as follows:
FROM - 5.5.5.5
TO - 192.168.6.6 (the private address of the server)
In this typical case, the server would still see REMOTE_ADDR as 5.5.5.5, as that is the source address on the incoming request. Then, the packets would be returned to 5.5.5.5, and the response would return to the client successfully.
Now, in the case of dynamic PAT, for example, the request would reach the NAT device as follows:
FROM - 5.5.5.5
TO - 6.6.6.6
Then, the NAT device would rewrite both source and destination packets, maintaining this "dynamic" mapping for only the lifetime of the request:
FROM - 192.168.1.1:12345 (the dynamic PAT address)
TO - 192.168.6.6 (the private address of the server)
Now, when the server sees this request, it appears to be from private address 192.168.1.1. In fact, with a strict PAT all requests will appear to be from this address. In your case, there are 2 or 3 of these addresses, probably because you may have enough traffic that you risk running out of ports if you use only a single dynamic PAT address.
So, your REMOTE_ADDR is 192.168.1.1, because that is actually the source address on the request packets. There is no HTTP_X_FORWARDED_FOR, because the Dynamic PAT is occurring at a lower TCP/IP layer (address and not application).
Finally, the response is sent back to 192.168.1.1:12345, which routs to the NAT device, which for the duration of the request/response (see the Cisco documentation above) maps it back to 5.5.5.5, and then drops the "dynamic" mapping.
Everything worked perfectly, the client gets the response back, except that you have no idea of the actual client address from the viewpoint of the server. And if it is dynamic NAT in play, I don't see how you could get this information from the server.
Fortunately, you did exactly the right thing to get the information in javascript on the client, so this likely solves your problem as well as it could be solved.
It depends on your network structure. Simply a firewall or load balancer can change the variables which you are checking.
if you are using a load balancer check this:
How to get visitor IP on load balancing machine using asp.net
if your sever is behind a firewall check this:
Find if request was forwarded from firewall to IIS