C#获取IP及MAC地址 方法
C#获取IP及MAC地址 方法,比较齐全
using System.Net;
using System;
using System.Management;
using System.Runtime.InteropServices; public class getIP
{
[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip); //获取本机的IP public string getLocalIP()
{
string strHostName = Dns.GetHostName(); //得到本机的主机名 IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本机IP string strAddr = ipEntry.AddressList[].ToString();
return (strAddr);
}
//获取本机的MAC public string getLocalMac()
{
string mac = null;
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
if (mo["IPEnabled"].ToString() == "True")
mac = mo["MacAddress"].ToString();
}
return (mac);
} //获取远程主机IP public string[] getRemoteIP(string RemoteHostName)
{
IPHostEntry ipEntry = Dns.GetHostByName(RemoteHostName);
IPAddress[] IpAddr = ipEntry.AddressList;
string[] strAddr = new string[IpAddr.Length];
for (int i = ; i < IPADDR.LENGTH; I++)
{
strAddr[i] = IpAddr[i].ToString();
}
return (strAddr);
}
//获取远程主机MAC public string getRemoteMac(string localIP, string remoteIP)
{
Int32 ldest = inet_addr(remoteIP); //目的ip Int32 lhost = inet_addr(localIP); //本地ip try
{
Int64 macinfo = new Int64();
Int32 len = ;
int res = SendARP(ldest, , ref macinfo, ref len);
return Convert.ToString(macinfo, );
}
catch (Exception err)
{
Console.WriteLine("Error:{0}", err.Message);
}
return .ToString();
} public static void Main(string[] args)
{
getIP gi = new getIP();
Console.WriteLine("本地网卡信息:");
Console.WriteLine(gi.getLocalIP() + " - " + gi.getLocalMac()); Console.WriteLine("\n\r远程网卡信息:");
string[] temp = gi.getRemoteIP("scmobile-tj2");
for (int i = ; i < TEMP.LENGTH; I++)
{
Console.WriteLine(temp[i]);
}
Console.WriteLine(gi.getRemoteMac("192.168.0.3", "192.168.0.1"));
}
}
C#获取IP及MAC地址 方法的更多相关文章
- C语言实现Windows下获取IP和MAC地址。
C语言实现Windows下获取IP和MAC地址. #include <winsock2.h> #include <stdio.h> #include <stdlib.h& ...
- Android -- 获取IP和MAC地址
通过InetAddress.getLocalHost()得到始终是“127.0.0.1”,要想得到真正的网络ip地址要通过下面的方法: 首先新建一个工程,修改AndroidManifest.xml文件 ...
- 获取IP和mac地址
1.获取IP static string GetLocalIp() { string hostname = Dns.GetHostName();//得到本机名 //IPHostEntry localh ...
- .net获取IP和MAC地址
获取IP 解决request.UserHostAddress取不到真实IP private string GetClientIP() { string result = HttpConte ...
- js获取IP和MAC地址
1.IP 百度一下有很多 可以用这个 <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> ...
- 使用ARP获取局域网内设备IP和MAC地址
根据Arp列表数据,查询本地设备在线状态 使用 arp -a 获得所有内网地址,首先看Mod对象 public struct MacIpPair { public string HostName; p ...
- 获取客户机的ip和mac地址
只获取clientIP package com.ppms.utils; import javax.servlet.http.HttpServletRequest; /** * Created by l ...
- 获取本机IP、mac地址、计算机名
python获取本机IP.mac地址.计算机名 在python中获取ip地址和在php中有很大不同,我们先来看一下python 获得本机MAC地址: >>> import uuid ...
- Linux 获取本机IP、MAC地址用法大全
getifaddrs()和struct ifaddrs的使用,获取本机IP ifaddrs结构体定义如下: struct ifaddrs { struct ifaddrs *ifa_next; /* ...
随机推荐
- 【shell】1、变量的声明、引用及作用域
shell程序 以文件形式存放==批量的Linux命令集合==,该文件能够被Shell解释执行,这种文件就是Shell脚本程序 通常由一段Liunx命令.Shell命令.控制语句以及注释语句构成 Sh ...
- Prometheus监控学习笔记
K8S最佳实战(包括了K8S的Prometheus监控和EFK日志搜集) https://jimmysong.io/kubernetes-handbook/practice/ Prometheus-b ...
- TPCH 22条SQL语句分析
使用TPC-H进行性能测试,需要有很多工作配合才能获得较高性能,如建立索引,表数据的合理分布(使用表空间和聚簇技术)等.本文从查询优化技术的角度,对TPC-H的22条查询语句和主流数据库执行每条语句对 ...
- salt state.sls windows 传输文件
salt 安装 - 传输文件 一.master -- minion linux 下 master 拷贝到 minion 上 1.cp.get_file 拷贝文件 # salt '172.16.3. ...
- C# 对IOC的理解 依赖的转移
原文:https://blog.csdn.net/huwei2003/article/details/40022011 系统 可方便的替换 日志类 自己的理解: 依赖接口,日志的实例化 不直接写在依赖 ...
- printf的使用和test的使用
1.printf的使用 printf的转义序列 序列 说明 \a 警告字符,通常为ASCII的BEL字符 \b 后退 \c 抑制(不显示)输出结果中任何结尾的换行字符(只在%b格式指示符控制下的参数字 ...
- 在mybtis的映射文件中判断集合大小
<if test="groupIds != null and groupIds.size>0"> and (group_id in<foreach coll ...
- python 日期
python datetime库使用和时间加减计算 来自:https://www.cnblogs.com/linkenpark/p/8079337.html datetime库使用 一.操作当前时间 ...
- Ubuntu 命令行连接WiFi
查看是否已经正确安装无线网卡 iwconfig .启动无线网卡, 如果网卡是wlan0 # 方式1 ifconfig wlan0 up # 或者方式2 ip link set wlan0 up .扫描 ...
- Oracle数据库安装以及使用脚本创建数据库授权
安装数据库 事实上Oracle安装 1.安装准备 Oracle的安装包下载以后是两个压缩包,同时选中两个压缩包右击进行解压 2.解压完成如下图所示 3.双击 setup.exe 文件进行安装,会弹出以 ...