I need to get the actual (not the gateway) ip address of my computer but all the solutions I have tried are returning the gateway IP address. For instance, all suggestions from this link Get the client IP address using PHP return the gateway address. Is there a way I can get the actual ip address (public) of my computer - either in php or java?
From the comments, it sounds like you want to create a unique identifier for each computer accessing the server. There is no one piece of information that you can get that will act as such an identifier, here's why:
IP Address
The IP address that you can see is the public address, not the private address of a device behind a NAT'd router.
MAC Address
The MAC address of each network card in each computer is unique, but that information is not available outside of the LAN segment.
You may be able to use a combination of pieces of information (like the IP address and user agent) to make an educated guess that a given request is coming from a unique user, but this would be a guess.. there's no way to be certain of it.
Use $_SERVER['REMOTE_ADDR'] to get the user's public address, and then $_SERVER['USER_AGENT'] to get their user agent. If the combination of these two are unique, then there's a good chance that the request is coming from a unique user.
Another way to do this would be to set a cookie that has a distant future expiration date, or a session ID. But this all depends on how long you need to keep track of the user, if you need to keep track of them after they've closed their browser and then later returned, etc. The only full-proof way is to create an account where they authenticate on your site.
java's InetAddress class might be useful.
InetAddress IP=InetAddress.getLocalHost();
System.out.println("IP is := "+IP.getHostAddress());
Output: IP is := 10.100.95.228
Related
I tried getting Public IP address from HttpServletRequest object using
httpServletRequest.getRemoteAddress();
which returns simple public ip address e.g. 123.21.21.12 but what I need is 132.21.21.12/8.
I have checked this How to get client's IP address using javascript only? but all of these provide simple ip addresses not with CIDR bits.
Thank you.
As others stated in the comments to your question, there is no way to extract subnet mask information just from the IP address alone, and there is no reliable way to get this information from the server. Firstly, you are communicating with a single IP address and not the whole subnet, so the correct CIDR is, as others said, /32. Secondly, network mask is network layer information and the way IP routing works doesn't require any other information to be provided other than a destination IP address.
It is important to know what exactly are trying to accomplish and are using this information for. Do you need this just to fulfill some format requirements or you really need the subnet mask. Anyway, if we're talking about ordinary web client/server I could see two different scenarios:
1) You need exact information about client network configuration
Even if you would be able to programmatically obtain such data, I'm not sure how useful it would be. Because of the shortage of IPv4 addresses and the way ISPs allocate addresses to customers most of the clients are behind their home router which does NAT or in some cases behind carrier grade NAT. Having bunch of clients that all have the same 192.168.1.2/24 address would be a bit pointless.
2) You need the public information (used for global routing) to group customers or something related to that.
This means getting the public IP address (the one you get with httpServletRequest.getRemoteAddress();). There's no way to obtain the exact subnet with 100% accuracy but you can get the route object from a RIR (Regional Internet Registry) database that delegated the scope that IP belongs to. LIRs (organizations that got the address block) have the obligation to update this database with various information and one of those is creating these route objects. For that you can use Whois protocol which is really simple and easily implemented. Basically you just need to open TCP connection to a port 43, send a command that contain the IP address you need the info on and parse the output.
For your example (123.21.21.12) the route object would look like:
~ # whois -T route 123.21.21.12
% [whois.apnic.net]
% Whois data copyright terms http://www.apnic.net/db/dbcopyright.html
% Information related to '123.21.16.0/20AS45899'
route: 123.21.16.0/20
descr: VietNam Post and Telecom Corporation (VNPT)
descr: VNPT-AS-AP
country: VN
origin: AS45899
remarks: mailto: noc#vnn.vn
notify: hm-changed#vnnic.net.vn
mnt-by: MAINT-VN-VNPT
changed: hm-changed#vnnic.net.vn 20100810
source: APNIC
% This query was served by the APNIC Whois Service version 1.69.1-APNICv1r0 (UNDEFINED)
We can see that that IP address belongs to a AS45899 (VNPT) and that the route object is 123.21.16.0/20, which gives you the CIDR you wanted.
And if we query that IP address from a router that has a full BGP table:
# show bgp ipv4 unicast 123.21.21.12
BGP routing table entry for 123.21.16.0/20, version 71369881
Paths: (3 available, best #1, table default)
we do see that indeed the Whois database is updated and that IP belongs to 123.21.16.0/20 route.
Keep in mind that this /20 could be aggregated route, but that's the best you can get. That's the routing information that's globally available and used by routers around the world to route traffic. How that /20 prefix is used inside the ISP network is up to their internal organization and policies and you can't check that. You also shouldn't get routes smaller than /24 this way.
I want to know in advance what network interface will be used for a given IP address X.
I could do that by browsing the result of java.net.NetworkInterface.getNetworkInterfaces() and checking X against the IP address and subnet mask of each InterfaceAddress returned by getInterfaceAddresses(), but it doesn’t seem like a lot of fun and I could end up with a different result from the one chosen by the IP stack if several choices are valid.
Why do I need that?
I need to know the IP address which can be used to reach me from the peer having the IP address X.
There's an answer here that I feel solves your problem.
But to expand on it a bit, the following code prints out the IP address of the interface you would use to route an IP packet from your host to the IP x.x.x.x.
DatagramSocket s = new DatagramSocket();
s.connect(InetAddress.getByAddress(new byte[]{x,x,x,x}), 0);
NetworkInterface n = NetworkInterface.getByInetAddress(s.getLocalAddress());
InetAddress yourInterfaceIP = n.getInetAddresses().nextElement();
System.out.println(yourInterfaceIP.getHostAddress());
Setting up a UDP datagram socket doesn't send anything. It just simply checks permissions and routing between the two end points. The IP you specify doesn't need to be reachable either for this to work.
This uses the routing present on the host your running this on. Obviously it doesn't use the routing on the remote host. So really, it returns what it considers a reachable interface from the perspective of X.
This obviously won't work behind a proxy, for which you'll need to use a whats-my-ip type service to look up what your proxy address is.
There are four people who are sharing a same wifi connection,
according to below definition there IP must be unique.
An IP address is a numerical label assigned to each device participating in a computer network that uses the Internet Protocol for communication.
Q1. IS IT SO OR NOT?
For uniquely identifying them I tried:
request.remoteAddress
But I get same IP for those all four person.
Q2.HOW UNIQUELY IDENTIFY THEM?(should I say them to pass some token like thing to identify them uniquely? but I will prefer unique IP)
The correct phrasing is that they all 4 are behinds a NAT firewall -- that is;
They all have unique IP addresses on their internal network (like 192.168.x.x or other internal IP address) see http://en.wikipedia.org/wiki/Private_network and RFC1918 for details.
They are connecting to the public internet though a firewall/gateway which does network address translation (see http://en.wikipedia.org/wiki/Network_address_translation for details).
This is the common way for most home/office networks to connect to the internet, for two reasons -- it require much fewer public internet addresses, and it also makes the network more secure as avoiding public addresses for internal resources means that there is less likelyhood for that you fail to configure you firewalls correctly.
The downside is that IP addresses are by no means a unique ID for the users -- that is what Cookies are for.
It may be that these 4 persons are connected from the same network. In this case the ip address will be seen as the same ip address for the external networks.
Requests sent from a router to a server outside that local network will all have the same IP address. This is one reason session ids are useful. If you need an identifier that MUST be unique for each user even if they are underneath the same local network you will need to use session variables.
Consider looking at this post
Where is session.id in Play 2.0?
Essentially create your unique ID. (Code taken from referenced SO post)
// Generate a unique id
String uuid=session("uuid");
if(uuid==null) {
uuid=java.util.UUID.randomUUID().toString();
session("uuid", uuid);
}
All the 4 users are accessing using the same internet line. Thus the Public IPAddress of all of them will be same. If you have a native application, then on first request that you make to your server, send the device ID with the request. Device ID combined with IP Address should be fairly unique, unless the device ID on all the devices are same (very rare). Another solution can be that you generate a unique Id (JSessionID when session is created on server can be used) on the first request to the server and send it in response. Then pass the unique Id on all subsequent request.
If you are using web browser of the mobile, then you can create session (JsessionId cookie will be generated by server). Mobile browser will take care of sending the jsessionid cookie with each request.
I am afraid there is no easy way to identify users since they can always modify the data they send you. That is why almost all the web services require log in (their own system or Facebook, Google, etc.). The problem, however, is, if someone registers enough accounts they can keep requesting you all the time. To avoid this problem, you may want to introduce Captcha verification every so often even after registration.
Also, not every user has static IP meaning they can change IP every time they get blocked (if you do block by IP, if I understand the question correctly).
They all have unique IP addresses on their internal network (like 192.168.x.x or other internal IP address) see http://en.wikipedia.org/wiki/Private_network and RFC1918 for details.
They are connecting to the public internet though a firewall/gateway which does network address translation (see http://en.wikipedia.org/wiki/Network_address_translation for details).
I have this snippet in Java:
final InetAddress address = InetAddress.getLocalHost();
final NetworkInterface ni = NetworkInterface.getByInetAddress(address);
key = new String(ni.getHardwareAddress());
"key", on my PC returns:
▲╔UiÎ
What would be the equivalent to get the same output in VB.Net? I have tried getting 'MAC Address' and 'Physical Address' but they do not compare to the output in Java at all?
I can't help you with the VB, but maybe I can clear a few things up.
In my experience, InetAddress.getLocalHost() is useless. It gives different results depending on the OS, on the Java version, on the brand of router you're connecting to, on the phase of the moon, etc. Stay away from it. If you want the loopback IP address, just hard-code it: 127.0.0.1. If you want the addresses of the network interfaces on your machine, call NetworkInterface.getInterfaces() and iterate through them.
The MAC address is the unique ID of an ethernet interface. Normally, you don't have to worry about it at all, unless you're doing something weird with the network hardware. It's got nothing to do with IP addresses.
Lastly: if you're looking to determine the IP address that others can use to connect to you, just be aware that it's not so simple. A computer may have multiple network interfaces - how do you know which one to pick? And if you're behind a router (as most machines are these days) then the computer is assigned a "local" address, which is completely useless to anyone outside the local network.
I want to get all the usernames or IPaddresses from the LAN using java.
Can anyone suggest me how to approach?
Thank you.
This is a slightly strange question. What is your purpose, what do you want to do with the information? A list of user names is quite different from a list of IP addresses, it's hard to understand why either is acceptable.
Also, consider that just because an IP address "exists" on a LAN, that doesn't say anything about the type of host that has the IP address. It might not have a "user" associated with it, for instance.
For a list of IPs, try finding the subnetwork mask, and pinging the broadcast address.