C # To obtain router external network IP,MAC address implementation Code _c# tutorial

Source: Internet
Author: User

C # Implementation of the acquisition router MAC address, router extranet address. For the router MAC address, it is important to know the user name and password of the router Web management system. As for the access to the router's extranet IP address, you do not need to know the Router Web Management system username and password, but need to have a proxy page to obtain the client public network IP address, so that C # request this page can get to the router public IP address. Such as

Getip.ashx

Test route for Mercury MR804, Mercury MR808, can successfully restart Routing and get to router mac and extranet IP address

Source

Using System.Text;
Using System.Net;
Using System.Text.RegularExpressions;
Using System.IO; public class Router {Encoding gb2312 = encoding.getencoding (936);//router's Web Management system default encoding is gb2312///<summary>///makes Send request with HttpWebRequest object///</summary>///<param name= "url" ></param>///<param name= "encoding" & gt; encoding </param>///<param name= "cache" > Voucher </param>///<returns></returns> private Stati C 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>///Get routing mac and extranet IP address///</summary>///<param name= "Routerip" > routed IP Address, which is the gateway address, default 19 2.168.1.1</param>///<param name= "UserName" > Username </param>///<param name= "PASSOWRD" > Password </ param>///<returns></returns> private String Loadmacwanip (string routerip,string username,string Passo
   WRD) {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 gets extranet IP address and MAC address

IP address access:

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 page http://www.ip138.com/ip2city.asp myhttpfile= (chttpfile* Mysession.openurl (csaddress),//Read network address while (Myhttpfile->readstring (cssource) {//loop read the downloaded page text//code convert cha R *pstr = (char*) cssource.getbuffer (Cssource.getlength ()); Gets the original string int nbuffersize = MultiByteToWideChar (Cp_utf8, 0,pstr,-1, NULL, 0) of the Str object;  Number of wchar_t *pbuffer = (wchar_t*) malloc (nbuffersize * sizeof (wchar_t)) to obtain the required cache;//Request Cache Space MultiByteToWideChar (Cp_utf8, 0, Pstr,-1, pbuffer, nbuffersize*sizeof (wchar_t));//transcoding//messagebox (pbuffer); 
  Display Cssource.format (_t ("%s"), pbuffer); Free (pbuffer); 
  Releases the cache int begin=0; 
  Begin=cssource.find (_t ("["), 0); 
   if (begin!=-1)//If found "[", then find "]" in parentheses in the text is your extranet IP {int end=cssource.find (_t ("]"));Csipaddress.format (_t ("%s"), Cssource.mid (begin+1,end-begin-1));//Pull out IP return csipaddress; 
} return csipaddress;  }

MAC Address acquisition:

CString getmacaddress (void) 
{ 
 //cstring strip,strgateway,strsubnetmask; 
 CString Strmac; 
 Strmac.format (_t ("")); 
 U_char pmac[6]; 
 Pip_adapter_info ADP = NULL; 
 ULONG ulong=0; 
 Request memory for Adapter 
 :: GetAdaptersInfo (Adp,&ulong); 
 ADP = (pip_adapter_info):: GlobalAlloc (Gptr,ulong); 
 
 Get local Adapter structure information 
 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);//Gateway 
   //strip.format (_t ("%s"),adp-> IpAddressList.IpAddress.String);//ip 
   //strsubnetmask.format (_t ("%s"), adp->ipaddresslist.ipmask.string) ;//Subnet Mask 
   //messagebox (STRMAC); 
  } 
 } 
 return STRMAC; 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.