.net Core 定时改变Mac地址
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using Microsoft.Win32; using System.Net.NetworkInformation; using System.Management; using System.Threading; using System.Runtime.InteropServices; using System.Timers; using NETCONLib; using Utils; namespace Service.Thxd { public class ChangeMac { public void changeMac() { try { var needTime = ConfigurationManager.AppSettings["time"]; //ResetMACAddress(needMac); System.Timers.Timer timer = new System.Timers.Timer(Convert.ToInt32(needTime)); timer.Elapsed += new System.Timers.ElapsedEventHandler(ResetMACAddress); timer.Enabled = true; timer.AutoReset = true; timer.Start(); } catch (Exception ex) { log4netUtils.WriteLog("发生异常", ex); } } /// <summary> /// 重设MAC地址 /// </summary> public void ResetMACAddress(object source, System.Timers.ElapsedEventArgs e) { try { RegistryKey rkey; var path = ConfigurationManager.AppSettings["path"]; rkey = Registry.LocalMachine.OpenSubKey(path, true); var mac = (string)rkey.GetValue("NetworkAddress"); string needMac = string.Empty; for (int i = 0; i < mac.Length; i++) { if (i < 6) { needMac += mac[i].ToString(); } } string macAdd = CreateNewMacAddress(needMac); rkey.SetValue("NetworkAddress", macAdd); mac = (string)rkey.GetValue("NetworkAddress"); rkey.Close(); ReConnect(); } catch (Exception ex) { log4netUtils.WriteLog("发生异常", ex); } } /// <summary> /// 重新连接 /// </summary> private void ReConnect() { var name = ConfigurationManager.AppSettings["name"]; log4netUtils.WriteLog($"开始重新连接"); //Disable(name); //Enable(name); //log4netUtils.WriteLog($"结束重新连接"); NetSharingManagerClass netSharingMgr = new NetSharingManagerClass(); INetSharingEveryConnectionCollection connections = netSharingMgr.EnumEveryConnection; foreach (INetConnection connection in connections) { INetConnectionProps connProps = netSharingMgr.get_NetConnectionProps(connection); if (connProps.MediaType == tagNETCON_MEDIATYPE.NCM_LAN) { connection.Disconnect(); //禁用网络 connection.Connect(); //启用网络 log4netUtils.WriteLog($"结束重新连接"); } } } static void Enable(string interfaceName) { System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("netsh", "interface set interface \"" + interfaceName + "\" enable"); System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo = psi; p.Start(); } static void Disable(string interfaceName) { System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("netsh", "interface set interface \"" + interfaceName + "\" disable"); System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo = psi; p.Start(); } ///// <summary> /// 生成随机MAC地址 /// </summary> /// <returns></returns> public string CreateNewMacAddress(string neeedMac) { //return "0016D3B5C493"; int min = 0; int max = 16; Random ro = new Random(); var snOne = string.Format("{0}{1}{2}{3}{4}{5}", ro.Next(min, max).ToString("x"), ro.Next(min, max).ToString("x"), ro.Next(min, max).ToString("x"), ro.Next(min, max).ToString("x"), ro.Next(min, max).ToString("x"),//10 ro.Next(min, max).ToString("x") ).ToUpper(); var sn = neeedMac + snOne; return sn; } } }
这里面Mac地址是通过改变注册表编辑器的内容来改变的.具体注册表地址在计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\000?
然后000? 里面有个DriverDesc 看这个是不是你的驱动器版本验证,如果都对就是这个,然后里面有个NetworkAddress 属性,如果没有手动添加一个.然后他前6位最好不要变.只改后面的。
浙公网安备 33010602011771号