How to get a list of all valid IP addresses in a local network?
Install nmap,
sudo apt-get install nmap
then
nmap -sP 192.168.1.*
or more commonly
nmap -sn 192.168.1.0/24
will scan the entire .1 to .254 range
This does a simple ping scan in the entire subnet to see which hosts are online.
List the IP Address of all computers connected to a single LAN
You're not really going to find anything more reliable than ping
ing or arping
ing addresses on the same subset. I implemented this for a certain piece of software back in the day on my first internship and, last time I checked (to be fair it was several years ago), that is what they were still using for this functionality. I take that to mean that they haven't found anything better.
It is not hard to find the source code for these and translate them to C#. ping
, arping
. Alternatively, you just shell out to a command prompt and execute ping
and then parse the results.
List of IP addresses/hostnames from local network in Python
If by "local" you mean on the same network segment, then you have to perform the following steps:
- Determine your own IP address
- Determine your own netmask
- Determine the network range
- Scan all the addresses (except the lowest, which is your network address and the highest, which is your broadcast address).
- Use your DNS's reverse lookup to determine the hostname for IP addresses which respond to your scan.
Or you can just let Python execute nmap externally and pipe the results back into your program.
How to get IP address and names of all devices in local network on Android
The main problem is that you're grabbing the wrong IP address. InetAddress.getLocalHost() is returning 127.0.0.1 and that's just your device.
Use the Wifi IP address in instead:
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiInfo connectionInfo = wm.getConnectionInfo();
int ipAddress = connectionInfo.getIpAddress();
String ipString = Formatter.formatIpAddress(ipAddress);
Here is a quick and dirty AsyncTask that does that:
static class NetworkSniffTask extends AsyncTask<Void, Void, Void> {
private static final String TAG = Constants.TAG + "nstask";
private WeakReference<Context> mContextRef;
public NetworkSniffTask(Context context) {
mContextRef = new WeakReference<Context>(context);
}
@Override
protected Void doInBackground(Void... voids) {
Log.d(TAG, "Let's sniff the network");
try {
Context context = mContextRef.get();
if (context != null) {
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiInfo connectionInfo = wm.getConnectionInfo();
int ipAddress = connectionInfo.getIpAddress();
String ipString = Formatter.formatIpAddress(ipAddress);
Log.d(TAG, "activeNetwork: " + String.valueOf(activeNetwork));
Log.d(TAG, "ipString: " + String.valueOf(ipString));
String prefix = ipString.substring(0, ipString.lastIndexOf(".") + 1);
Log.d(TAG, "prefix: " + prefix);
for (int i = 0; i < 255; i++) {
String testIp = prefix + String.valueOf(i);
InetAddress address = InetAddress.getByName(testIp);
boolean reachable = address.isReachable(1000);
String hostName = address.getCanonicalHostName();
if (reachable)
Log.i(TAG, "Host: " + String.valueOf(hostName) + "(" + String.valueOf(testIp) + ") is reachable!");
}
}
} catch (Throwable t) {
Log.e(TAG, "Well that's not good.", t);
}
return null;
}
Here are the permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Not all routers allow this, so to get the names in a other way is to send the mac adress to an api and get the brand name back in return.
String macAdress = "5caafd1b0019";
String dataUrl = "http://api.macvendors.com/" + macAdress;
HttpURLConnection connection = null;
try {
URL url = new URL(dataUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.flush();
wr.close();
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuffer response = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {response.append(line);response.append('\r');}
rd.close();
String responseStr = response.toString();
Log.d("Server response", responseStr);
} catch (Exception e) {e.printStackTrace();} finally {if (connection != null) {connection.disconnect();}}
How to get all ip address in LAN?
This might do the trick for you
foreach(NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
{
if(!ip.IsDnsEligible)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
// All IP Address in the LAN
}
}
}
}
The Only drawback of this code is that the information returned by instances of UnicastIPAddressInformation
is not available for operating systems earlier than Windows XP.
Related Topics
Use Ssh to Start a Background Process on a Remote Server, and Exit Session
Difference Between Patch and Diff Files
Error: Service "Xxx" Uses an Undefined Network "Xxx"
Linux Wildcard Usage in Cp and Mv
Bash Script Read All the Files in Directory
Using Linux How to Pass the Contents of a File as a Parameter to an Executable
Sorting Scientific Number with Unix Sort
Link a Static Library to a Shared One During Build
Setting Limit to Total Physical Memory Available in Linux
Android Studio 2.3 Using Emulator from Console, "/Dev/Kvm Device: Permission Denied" for Root User
Define Alias That References Other Aliases
Identify Other End of a Unix Domain Socket Connection
Help with Understanding a Very Basic Main() Disassembly in Gdb
Bash Script Read All the Files in Directory
Apt-Get Error: Sub-Process /Usr/Bin/Dpkg Returned an Error Code (1)