1:通過java運行cmd命令,來通過arp命令獲取同一網(wǎng)絡下設備信息,對于支持linux 和windows的設備有效,像一些非智能設備,就無力回天了
2:使用android手機通過向子網(wǎng)內(nèi)所有設備先發(fā)送一遍udp包,實現(xiàn)與在線的設備都進行通信一遍,這樣對應的路由信息就自動存儲在本地手機中,然后在通過讀取android 本機的arp緩存表,來獲取設備信息
好,下面詳細進行第二種方式的描述:
Step1:首先,獲取本機所處的子網(wǎng)段,方法詳細代碼如下:
/**
* 獲取ip地址
*
* @return
*/
private static String getHostIP() {
String hostIp = null;
try {
//這里以eth0為例
hostIp = getIpAddress("eth0");
} catch (SocketException e) {
e.printStackTrace();
}
return hostIp;
}
/**
* Get Ip address 自動獲取IP地址
* 可以傳入eth1,eth0,wlan0,等
*
* @throws SocketException
*/
public static String getIpAddress(String ipType) throws SocketException {
String hostIp = null;
try {
Enumeration nis = NetworkInterface.getNetworkInterfaces();
InetAddress ia = null;
while (nis.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) nis.nextElement();
if (ni.getName().equals(ipType)) {
Enumeration<InetAddress> ias = ni.getInetAddresses();
while (ias.hasMoreElements()) {
ia = ias.nextElement();
if (ia instanceof Inet6Address) {
continue;// skip ipv6
}
String ip = ia.getHostAddress();
// 過濾掉127段的ip地址
if (!"127.0.0.1".equals(ip)) {
hostIp = ia.getHostAddress();
break;
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
Log.d(ipType, "get the IpAddress--> " + hostIp + "");
return hostIp;
}
Step2:new出一個線程,向網(wǎng)段內(nèi)所有在線設備發(fā)送消息。
/**
* 創(chuàng)建一個線程向本地所有ip發(fā)送一個數(shù)據(jù)
*/
public static void sendDataToLocal() {
//局域網(wǎng)內(nèi)存在的ip集合
final List<String> ipList = new ArrayList<>();
final Map<String, String> map = new HashMap<>();
//獲取本機所在的局域網(wǎng)地址
String hostIP = getHostIP();
int lastIndexOf = hostIP.lastIndexOf(".");
final String substring = hostIP.substring(0, lastIndexOf + 1);
new Thread(new Runnable() {
@Override
public void run() {
DatagramPacket dp = new DatagramPacket(new byte[0], 0, 0);
DatagramSocket socket;
try {
socket = new DatagramSocket();
int position = 2;
while (position < 255) {
Log.e("Scanner ", "run: udp-" + substring + position);
dp.setAddress(InetAddress.getByName(substring + String.valueOf(position)));
socket.send(dp);
position++;
if (position == 125) {
//分兩段掉包,一次性發(fā)的話,達到236左右,會耗時3秒左右再往下發(fā)
socket.close();
socket = new DatagramSocket();
}
}
socket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
執(zhí)行完后會自動寫入到/proc/net/arp
目錄下,如下:
Step3:延時3秒后,讀取本機proc/net/arp目錄下的內(nèi)容,得到結(jié)果:文章來源:http://www.zghlxwxcb.cn/news/detail-525985.html
/**
* 讀取/proc/net/arp并且解析出來ip,mac,flag
* flag 為0x00說明目前不在局域網(wǎng)內(nèi),曾經(jīng)在過.0x02代表在局域網(wǎng)內(nèi)
*/
public static void readArp() {
try {
BufferedReader br = new BufferedReader(new FileReader("/proc/net/arp"));
String line = "";
String ip = "";
//flag 為0x00說明目前不在局域網(wǎng)內(nèi),曾經(jīng)在過.0x02代表在局域網(wǎng)內(nèi)
String flag = "";
String mac = "";
if (br.readLine() == null) {
Log.e("scanner", "readArp: null");
}
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.length() < 63)
continue;
if (line.toUpperCase(Locale.US).contains("IP"))
continue;
ip = line.substring(0, 17).trim();
flag = line.substring(29, 32).trim();
mac = line.substring(41, 63).trim();
if (mac.contains("00:00:00:00:00:00"))
continue;
Log.e("scanner", "readArp: mac= " + mac + " ; ip= " + ip + " ;flag= " + flag);
}
br.close();
} catch (Exception ignored) {
}
}
打印日志如下
最后附上完整代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-525985.html
public class DiscoverNetIpUtil{
/**
* 獲取ip地址
*
* @return
*/
private static String getHostIP() {
String hostIp = null;
try {
//這里以eth0為例
hostIp = getIpAddress("eth0");
} catch (SocketException e) {
e.printStackTrace();
}
return hostIp;
}
/**
* Get Ip address 自動獲取IP地址
* 可以傳入eth1,eth0,wlan0,等
*
* @throws SocketException
*/
public static String getIpAddress(String ipType) throws SocketException {
String hostIp = null;
try {
Enumeration nis = NetworkInterface.getNetworkInterfaces();
InetAddress ia = null;
while (nis.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) nis.nextElement();
if (ni.getName().equals(ipType)) {
Enumeration<InetAddress> ias = ni.getInetAddresses();
while (ias.hasMoreElements()) {
ia = ias.nextElement();
if (ia instanceof Inet6Address) {
continue;// skip ipv6
}
String ip = ia.getHostAddress();
// 過濾掉127段的ip地址
if (!"127.0.0.1".equals(ip)) {
hostIp = ia.getHostAddress();
break;
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
Log.d(ipType, "get the IpAddress--> " + hostIp + "");
return hostIp;
}
/**
* 創(chuàng)建一個線程向本地所有ip發(fā)送一個數(shù)據(jù)
*/
public static void sendDataToLocal() {
//局域網(wǎng)內(nèi)存在的ip集合
final List<String> ipList = new ArrayList<>();
final Map<String, String> map = new HashMap<>();
//獲取本機所在的局域網(wǎng)地址
String hostIP = getHostIP();
int lastIndexOf = hostIP.lastIndexOf(".");
final String substring = hostIP.substring(0, lastIndexOf + 1);
new Thread(new Runnable() {
@Override
public void run() {
DatagramPacket dp = new DatagramPacket(new byte[0], 0, 0);
DatagramSocket socket;
try {
socket = new DatagramSocket();
int position = 2;
while (position < 255) {
Log.e("Scanner ", "run: udp-" + substring + position);
dp.setAddress(InetAddress.getByName(substring + String.valueOf(position)));
socket.send(dp);
position++;
if (position == 125) {
//分兩段掉包,一次性發(fā)的話,達到236左右,會耗時3秒左右再往下發(fā)
socket.close();
socket = new DatagramSocket();
}
}
socket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
/**
* 讀取/proc/net/arp并且解析出來ip,mac,flag
* flag 為0x00說明目前不在局域網(wǎng)內(nèi),曾經(jīng)在過.0x02代表在局域網(wǎng)內(nèi)
*/
public static void readArp() {
try {
BufferedReader br = new BufferedReader(new FileReader("/proc/net/arp"));
String line = "";
String ip = "";
//flag 為0x00說明目前不在局域網(wǎng)內(nèi),曾經(jīng)在過.0x02代表在局域網(wǎng)內(nèi)
String flag = "";
String mac = "";
if (br.readLine() == null) {
Log.e("scanner", "readArp: null");
}
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.length() < 63)
continue;
if (line.toUpperCase(Locale.US).contains("IP"))
continue;
ip = line.substring(0, 17).trim();
flag = line.substring(29, 32).trim();
mac = line.substring(41, 63).trim();
if (mac.contains("00:00:00:00:00:00"))
continue;
Log.e("scanner", "readArp: mac= " + mac + " ; ip= " + ip + " ;flag= " + flag);
}
br.close();
} catch (Exception ignored) {
}
}
}
到了這里,關(guān)于Android 查詢局域網(wǎng)內(nèi)所有ip和mac地址的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!