C obtains the router external network IP MAC address implementation code
- 2021-11-13 02:33:14
- OfStack
C # implementation of the acquisition router MAC address, router external network address. To obtain the router MAC address, 1 must know the user name and password of the router web management system. As for obtaining the external network IP address of the router, you don't need to know the user name and password of the router web management system, but you need to have an agent page to obtain the client public network ip address, so C # can obtain the router public network ip address by requesting this page. Such as
//getip.ashx
The test routes are Mercury MR804 and Mercury MR808, which can successfully restart the route and obtain the address to router MAC and external network IP
Source code
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
public class Router
{
Encoding gb2312 = Encoding.GetEncoding(936);// Router's web The default code of the management system is gb2312
/// <summary>
/// Use HttpWebRequest Object sends a request
/// </summary>
/// <param name="url"></param>
/// <param name="encoding"> Code </param>
/// <param name="cache"> Vouchers </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>
/// Get a route MAC And extranet IP Address
/// </summary>
/// <param name="RouterIP"> Route IP Address, is the gateway address, default 192.168.1.1</param>
/// <param name="UserName"> User name </param>
/// <param name="Passowrd"> Password </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 obtains the IP address and MAC address of the external network
ip Address Acquisition:
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 Web page http://www.ip138.com/ip2city.asp
myHttpFile=(CHttpFile*)mySession.OpenURL(csAddress);// Read the network address
while(myHttpFile->ReadString(csSource))
{ // Loop to read the downloaded web page text
//code Conversion
char *pStr = (char*)csSource.GetBuffer(csSource.GetLength()); // Acquire str The original string of the object
int nBufferSize = MultiByteToWideChar(CP_UTF8, 0,pStr, -1, NULL, 0); // Get how much cache is required
wchar_t *pBuffer = (wchar_t*)malloc(nBufferSize * sizeof(wchar_t));// 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); // Release cache
int begin=0;
begin=csSource.Find(_T("["),0);
if(begin!=-1)// If found "[" , Then look for "]" The text in brackets is Your external network ip
{
int end=csSource.Find(_T("]"));
csIPAddress.Format(_T("%s"),csSource.Mid(begin+1,end-begin-1));// Extracting extranet 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;
}