Get local IP address
To get local Ip Address:
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}
To check if you're connected or not:
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
How to get the primary IP address of the local machine on Linux and OS X?
Use grep
to filter IP address from ifconfig
:
ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
Or with sed
:
ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'
If you are only interested in certain interfaces, wlan0, eth0, etc. then:
ifconfig wlan0 | ...
You can alias the command in your .bashrc
to create your own command called myip
for instance.
alias myip="ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'"
A much simpler way is hostname -I
(hostname -i
for older versions of hostname
but see comments). However, this is on Linux only.
Finding local IP addresses using Python's stdlib
import socket
socket.gethostbyname(socket.gethostname())
This won't work always (returns 127.0.0.1
on machines having the hostname in /etc/hosts
as 127.0.0.1
), a paliative would be what gimel shows, use socket.getfqdn()
instead. Of course your machine needs a resolvable hostname.
How do I get the IP address of this website?
Pings usually don't include the protocol. The command below worked for me.
ping sfbay.craigslist.org
Getting the IP address of the current machine using Java
import java.net.DatagramSocket;
import java.net.InetAddress;
try(final DatagramSocket socket = new DatagramSocket()){
socket.connect(InetAddress.getByName("8.8.8.8"), 10002);
ip = socket.getLocalAddress().getHostAddress();
}
This way works well when there are multiple network interfaces. It always returns the preferred outbound IP. The destination 8.8.8.8
is not needed to be reachable.
Connect
on a UDP socket has the following effect: it sets the destination for Send/Recv, discards all packets from other addresses, and - which is what we use - transfers the socket into "connected" state, settings its appropriate fields. This includes checking the existence of the route to the destination according to the system's routing table and setting the local endpoint accordingly. The last part seems to be undocumented officially but it looks like an integral trait of Berkeley sockets API (a side effect of UDP "connected" state) that works reliably in both Windows and Linux across versions and distributions.
So, this method will give the local address that would be used to connect to the specified remote host. There is no real connection established, hence the specified remote ip can be unreachable.
Edit:
As @macomgil says, for MacOS you can do this:
Socket socket = new Socket();
socket.connect(new InetSocketAddress("google.com", 80));
System.out.println(socket.getLocalAddress());
Related Topics
Resharper "Cannot Resolve Symbol" Even When Project Builds
What's the Difference Between System.Valuetuple and System.Tuple
Itextsharp Insert Text to an Existing PDF
Is It Considered Acceptable to Not Call Dispose() on a Tpl Task Object
Extension Methods Must Be Defined in a Non-Generic Static Class
Views in Separate Assemblies in ASP.NET MVC
Calling JavaScript Function from Codebehind
How to Build a JSON Object to Send to an Ajax Webservice
How to Configure the Web.Config to Allow Requests of Any Length
Is Shifting Bits Faster Than Multiplying and Dividing in Java? .Net
How to Pass Parameters by Reference in Java
Differencebetween a Mutable and Immutable String in C#
Unzip Files Programmatically in .Net
Unauthorised Webapi Call Returning Login Page Rather Than 401