C#获取路由器外网IP,MAC地址的实现代码
程序员文章站
2022-03-26 08:05:21
c#实现的获取路由器mac地址,路由器外网地址。对于要获取路由器mac地址,一定需要知道路由器web管理系统的用户名和密码。至于获取路由器的外网ip地址,可以不需要知道路由...
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; }