如何获取局域网内所有IP地址 java代码
发布时间:2025-05-25 08:08:07 发布人:远客网络
一、如何获取局域网内所有IP地址 java代码
1.得到局域网网段,可由自己机器的IP来确定(也可以手动获取主机IP-CMD-ipconfig/all)
2.根据IP类型,一次遍历局域网内IP地址
JAVA类,编译之后直接运行便可以得到局域网内所有IP,具体怎样使用你自己编写相应代码调用便可
static public HashMap ping;//ping后的结果集
public HashMap getPing(){//用来得到ping后的结果集
//当前线程的数量,防止过多线程摧毁电脑
public void Ping(String ip) throws Exception{
public void PingAll() throws Exception{
InetAddress host= InetAddress.getLocalHost();
String hostAddress= host.getHostAddress();
k=hostAddress.lastIndexOf(".");
String ss= hostAddress.substring(0,k+1);
for(int i=1;i<=255;i++){//对所有局域网Ip
public static void main(String[] args) throws Exception{
java.util.Set entries= ping.entrySet();
Iterator iter=entries.iterator();
Map.Entry entry=(Map.Entry)iter.next();
String key=(String)entry.getKey();
String value=(String)entry.getValue();
System.out.println(key+"-->"+value);
Process p= Runtime.getRuntime().exec("ping"+ip+"-w 300-n 1");
InputStreamReader ir= new InputStreamReader(p.getInputStream());
LineNumberReader input= new LineNumberReader(ir);
String line= input.readLine();
if(line.length()<17|| line.substring(8,17).equals("timed out"))
二、java中如何获取到本机的外网ip地址
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
*思想是访问网站,得到返回的文本后解析出本机在外网的IP地址
public class ExternalIpAddressFetcher{
private String externalIpProviderUrl;
private String myExternalIpAddress;
public ExternalIpAddressFetcher(String externalIpProviderUrl){
this.externalIpProviderUrl= externalIpProviderUrl;
String returnedhtml= fetchExternalIpProviderHTML(externalIpProviderUrl);
*从外网提供者处获得包含本机外网地址的字符串
*从返回的字符串如下
*<html><head><title>Current IP Check</title></head><body>Current IP Address: 123.147.226.222</body></html>
private String fetchExternalIpProviderHTML(String externalIpProviderUrl){
HttpURLConnection httpConn= null;
URL url= new URL(externalIpProviderUrl);
httpConn=(HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("User-Agent",
"Mozilla/4.0(compatible; MSIE 6.0; Windows 2000)");
in= httpConn.getInputStream();
byte[] bytes=new byte[1024];//此大小可根据实际情况调整
while(offset< bytes.length
&&(numRead=in.read(bytes, offset, bytes.length-offset))>= 0){
String receivedString=new String(bytes,"UTF-8");
} catch(MalformedURLException e){
*使用正则表达式解析返回的HTML文本,得到本机外网地址
private void parse(String html){
Pattern pattern=Pattern.compile("(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})", Pattern.CASE_INSENSITIVE);
Matcher matcher=pattern.matcher(html);
myExternalIpAddress=matcher.group(0);
*得到本机外网地址,得不到则为空
public String getMyExternalIpAddress(){
public static void main(String[] args){
ExternalIpAddressFetcher fetcher=new ExternalIpAddressFetcher("");
System.out.println(fetcher.getMyExternalIpAddress());
三、java如何获得局域网的外网ip
获取局域网内特定IP的MAC地址,可以使用Java编程语言。例如,对于IP地址192.168.1.187对应的网卡MAC地址,可以编写如下代码:
System.out.println("192.168.1.187对应网卡的MAC是:");
NetworkInterface ne= NetworkInterface.getByInetAddress(InetAddress.getByName("192.168.1.187"));
byte[] mac= ne.getHardwareAddress();
String mac_s= hexByte(mac[0])+":"+ hexByte(mac[1])+":"+ hexByte(mac[2])+":"+ hexByte(mac[3])+":"+ hexByte(mac[4])+":"+ hexByte(mac[5]);
程序运行的结果为:192.168.1.187对应网卡的MAC是: 00:0c:f1:20:75:58
需要注意的是,这里的MAC地址是以十六进制表示的,且每两个字节之间用冒号分隔。如果想要将其他IP地址转换为对应的MAC地址,只需将代码中的192.168.1.187替换为需要查询的IP地址即可。
类似地,获取局域网内计算机的名称,可以通过查询工作组信息实现。具体操作可以在本地计算机的管理工具中进行,也可以编写相应的代码实现自动化查询。
在进行局域网内IP地址与MAC地址的查询时,需要确保目标网络中的计算机允许网络访问,并且网络配置正确。
以上代码和方法适用于局域网内IP地址与MAC地址的查询,但在实际应用中可能需要根据具体网络环境进行调整。
此外,获取局域网外的公网IP地址则需要通过外部服务器查询,常见的服务有IP.cn、ipinfo.io等。
在编写相关代码时,需要注意处理可能出现的异常情况,例如网络不通或目标IP不存在等,以提高程序的健壮性。
通过上述方法,可以实现局域网内IP地址与MAC地址的查询,并获取公网IP地址,为网络管理和维护提供了有力的支持。