C#获取路由器外网IP,MAC地址的实现代码
作者:mdxy-dxy 时间:2021-07-03 06:01:32
C#实现的获取路由器MAC地址,路由器外网地址。对于要获取路由器MAC地址,一定需要知道路由器web管理系统的用户名和密码。至于获取路由器的外网IP地址,可以不需要知道路由器web管理系统的用户名和密码,但是需要有一个代理页面获取客户端公网ip地址的,这样C#请求此页面即可获取到路由器公网ip地址。如
//getip.ashx
测试路由为水星 MR804,水星 MR808,都可以成功重启路由和获取到路由器MAC和外网IP地址
源代码
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
public class Router
{
Encoding gb2312 = Encoding.GetEncoding(936);//路由器的web管理系统默认编码为gb2312
/// <summary>
/// 使用HttpWebRequest对象发送请求
/// </summary>
/// <param name="url"></param>
/// <param name="encoding">编码</param>
/// <param name="cache">凭证</param>
/// <returns></returns>
private static string SendRequest(string url, Encoding encoding,CredentialCache cache)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
if (cache != null)
{
request.PreAuthenticate = true;
request.Credentials = cache;
}
string html = null;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader srd = new StreamReader(response.GetResponseStream(), encoding);
html = srd.ReadToEnd();
srd.Close();
response.Close();
}
catch (Exception ex) { html = "FALSE" + ex.Message; }
return html;
}
/// <summary>
/// 获取路由MAC和外网IP地址
/// </summary>
/// <param name="RouterIP">路由IP地址,就是网关地址了,默认192.168.1.1</param>
/// <param name="UserName">用户名</param>
/// <param name="Passowrd">密码</param>
/// <returns></returns>
private string LoadMACWanIP(string RouterIP,string UserName,string Passowrd)
{
CredentialCache cache = new CredentialCache();
string url = "http://" + RouterIP + "/userRpm/StatusRpm.htm";
cache.Add(new Uri(url), "Basic", new NetworkCredential(UserName, Passowrd));
return SendRequest(url, gb2312, cache);
}
}
MFC 获取外网IP地址和MAC地址
ip地址获取:
CString GetSystemIp(void)
{
CString csSource;
CString csAddress;
CString csIPAddress;
csIPAddress.Format(_T(" "));
CInternetSession mySession(NULL,0);
CHttpFile* myHttpFile=NULL;
csAddress=_T("http://iframe.ip138.com/ic.asp");//ip138网页 http://www.ip138.com/ip2city.asp
myHttpFile=(CHttpFile*)mySession.OpenURL(csAddress);//读取网络地址
while(myHttpFile->ReadString(csSource))
{ //循环读取下载来的网页文本
//code 转换
char *pStr = (char*)csSource.GetBuffer(csSource.GetLength()); //取得str对象的原始字符串
int nBufferSize = MultiByteToWideChar(CP_UTF8, 0,pStr, -1, NULL, 0); //取得所需缓存的多少
wchar_t *pBuffer = (wchar_t*)malloc(nBufferSize * sizeof(wchar_t));//申请缓存空间
MultiByteToWideChar(CP_UTF8, 0, pStr, -1 , pBuffer, nBufferSize*sizeof(wchar_t));//转码
//MessageBox(pBuffer); //显示
csSource.Format(_T("%s"),pBuffer);
free(pBuffer); //释放缓存
int begin=0;
begin=csSource.Find(_T("["),0);
if(begin!=-1)//如果找到"[", 则找"]" 中括号内的文本则是 你的外网ip
{
int end=csSource.Find(_T("]"));
csIPAddress.Format(_T("%s"),csSource.Mid(begin+1,end-begin-1));//提取外网ip
return csIPAddress;
}
}
return csIPAddress;
}
MAC地址获取:
CString GetMacAddress(void)
{
//CString strIP,strGateWay,strSubnetMask;
CString strMac;
strMac.Format(_T(""));
u_char pMac[6];
PIP_ADAPTER_INFO adp = NULL;
ULONG uLong=0;
//为适配器申请内存
::GetAdaptersInfo(adp,&uLong);
adp = (PIP_ADAPTER_INFO)::GlobalAlloc(GPTR,uLong);
//取得本地适配器结构信息
if(::GetAdaptersInfo(adp,&uLong) == ERROR_SUCCESS)
{
if(adp != NULL)
{
//strMacAdd.Format("%s",adp->Address);
memcpy(pMac,adp->Address,6);
strMac.Format(_T("%02X-%02X-%02X-%02X-%02X-%02X"),pMac[0],pMac[1],pMac[2],pMac[3],pMac[4],pMac[5]);
//strGateWay.Format(_T("%s"),adp->GatewayList.IpAddress.String);// 网关
//strIP.Format(_T("%s"),adp->IpAddressList.IpAddress.String);//IP
//strSubnetMask.Format(_T("%s"),adp->IpAddressList.IpMask.String);//子网掩码
//MessageBox(strMac);
}
}
return strMac;
}
标签:C#,外网IP,MAC地址
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一文详解Spring是怎么读取配置Xml文件的
2023-07-30 00:27:37
![](https://img.aspxhome.com/file/2023/4/70364_0s.webp)
深入了解JAVA HASHMAP的死循环
2023-11-28 00:32:12
Java中Servlet的生命周期详解
2023-09-10 03:57:59
![](https://img.aspxhome.com/file/2023/7/61057_0s.png)
Android多渠道打包神器ProductFlavor详解
2021-06-11 19:48:22
![](https://img.aspxhome.com/file/2023/6/94286_0s.png)
Android使用Websocket实现聊天室
2023-07-07 13:01:59
![](https://img.aspxhome.com/file/2023/6/60386_0s.png)
springboot接收别人上传的本地视频实例代码
2023-03-22 01:55:39
SpringCloud Zuul自定义filter代码实例
2023-12-03 02:49:50
Mybatis-Plus的SQL语句组拼原理说明
2021-07-03 20:39:22
![](https://img.aspxhome.com/file/2023/5/84285_0s.png)
浅谈SpringBoot中的Bean初始化方法 @PostConstruct
2022-06-11 07:21:17
![](https://img.aspxhome.com/file/2023/1/80251_0s.jpg)
Java实现月饼的制作、下单和售卖功能
2023-03-06 18:26:24
![](https://img.aspxhome.com/file/2023/4/85574_0s.png)
Quartz.Net调度框架配置解析
2021-10-13 01:35:02
聊聊Unity 自定义日志保存的问题
2021-11-28 15:38:55
![](https://img.aspxhome.com/file/2023/9/69199_0s.png)
Java实现扑克牌程序
2023-11-11 12:09:52
![](https://img.aspxhome.com/file/2023/0/59340_0s.jpg)
C#调用存储过程详解(带返回值、参数输入输出等)
2023-12-06 01:06:35
Java 使用getClass().getResourceAsStream()方法获取资源
2023-07-23 08:59:56
java操作mongodb之多表联查的实现($lookup)
2023-08-08 10:24:07
从C#程序中调用非受管DLLs的方法
2023-03-23 08:43:41
java二叉树面试题详解
2021-06-13 08:40:23
![](https://img.aspxhome.com/file/2023/6/63016_0s.png)
Netty如何设置为Https访问
2021-12-06 02:00:40
Android集成微信支付功能
2023-07-14 10:38:11