; 快捷弹出网络连接:Run ncpa.cpl

Name := NetConnectionName()

; 将ipv4设为自动获取
RunWait %ComSpec% /c netsh interface ip set address name="%Name%" source=dhcp, , Hide
RunWait %ComSpec% /c netsh interface ip set dns name="%Name%" source=dhcp, ,Hide


; 将ipv4设为指定值【IP地址、子网掩码、默认网关,第二个RunWait为:DNS】
; Runwait %ComSpec% /c netsh interface ip set address name=`"%name%`" source=static addr=10.1.23.77 mask=255.255.255.0 gateway=10.1.23.1 gwmetric=1, , Hide
; RunWait %ComSpec% /c netsh interface ip set dns name=`"%name%`" source=static addr=10.1.23.1, ,Hide
Return


NetConnectionName() {
  wmiService := ComObjGet("winmgmts:\\.\root\CIMV2")
  ; 执行WQL查询获取所有网络适配器配置,寻找启用的且非虚拟的且已连接的
  adaptersConfig := wmiService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = true")

  ; 通过网络适配器配置找到活动的网络适配器
  For adapterConfig in adaptersConfig {
    ; 使用该Index查询Win32_NetworkAdapter以获取更多信息(如MAC地址)
    adapter := wmiService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE Index = " adapterConfig.Index).ItemIndex(0)
    
    ; 检查这个适配器是否是当前活动的
    If (adapter.NetConnectionStatus = 2) ; 2 表示已连接状态
      break ; 假设只有一个活动的网络连接,找到后即退出循环
  }

  ; MsgBox % "活动网络适配器:" adapter.Name "`nMAC地址:" adapter.MACAddress "`n网络连接名称:" . adapter.NetConnectionID
  Return adapter.NetConnectionID
}

/*     ; 老版本用CMD获取备份

mac := StrReplace(GetMacAddress(),"-",":")

adaptors := GetAdaptors() ;获取网卡列表
For _, adaptor in adaptors {
  if (adaptor.mac = mac) {
    name := adaptor.name
    ; 将ipv4设为自动获取
    RunWait, %ComSpec% /c netsh interface ip set address name="%name%" source=dhcp, , Hide
    RunWait, %ComSpec% /c netsh interface ip set dns name="%name%" source=dhcp, ,Hide
    
    ; 将ipv4设为指定值
    ; Runwait, %ComSpec% /c netsh interface ip set address name=`"%name%`" source=static addr=10.1.23.77 mask=255.255.255.0 gateway=10.1.23.1 gwmetric=1, , Hide
    ; RunWait, %ComSpec% /c netsh interface ip set dns name=`"%name%`" source=static addr=10.1.23.1, ,Hide
  }
}

GetAdaptors(){
  adaptors := {}
  ipconfig := cmd("ipconfig /all") ;cmd方式兼容XP
  lines := StrSplit(ipconfig,"`n","`r")
  aid = 0
  for id,line in Lines
  {
    if (!RegExMatch(line,"^\s*$"))
    {
      if RegExMatch(line, "^.*(适配器|adapter)\s([^\s].*):$", mn)
      {
        aid++
        adaptors[aid] := {}
        adaptors[aid].name := mn2
      }
      Else
      {
        if RegExMatch(line, "^.*(物理地址|Physical Address).*:\s(\w{2})-(\w{2})-(\w{2})-(\w{2})-(\w{2})-(\w{2})$", mm)
          adaptors[aid].mac := mm2 ":" mm3 ":" mm4 ":" mm5 ":" mm6 ":" mm7
      }
    }
  }
  return adaptors
}

GetMacAddress(){ ;获取当前活动网卡MAC
  res := cmd("getmac /NH")
  RegExMatch(res, ".*?([0-9A-Z].{16})(?!\w\\Device)", mac)
  return %mac1%
}
 
 
cmd(sCmd, sEncoding:="CP0", sDir:="", ByRef nExitCode:=0) {
  DllCall( "CreatePipe",       PtrP,hStdOutRd, PtrP,hStdOutWr, Ptr,0, UInt,0 )
  DllCall( "SetHandleInformation", Ptr,hStdOutWr, UInt,1, UInt,1         )
 
      VarSetCapacity( pi, (A_PtrSize == 4) ? 16 : 24,  0 )
  siSz := VarSetCapacity( si, (A_PtrSize == 4) ? 68 : 104, 0 )
  NumPut( siSz,    si,  0,              "UInt" )
  NumPut( 0x100,   si,  (A_PtrSize == 4) ? 44 : 60, "UInt" )
  NumPut( hStdOutWr, si,  (A_PtrSize == 4) ? 60 : 88, "Ptr"  )
  NumPut( hStdOutWr, si,  (A_PtrSize == 4) ? 64 : 96, "Ptr"  )
 
  If ( !DllCall( "CreateProcess", Ptr,0, Ptr,&sCmd, Ptr,0, Ptr,0, Int,True, UInt,0x08000000
                  , Ptr,0, Ptr,sDir?&sDir:0, Ptr,&si, Ptr,&pi ) )
    Return ""
    , DllCall( "CloseHandle", Ptr,hStdOutWr )
    , DllCall( "CloseHandle", Ptr,hStdOutRd )
 
  DllCall( "CloseHandle", Ptr,hStdOutWr ) ; The write pipe must be closed before reading the stdout.
  While ( 1 )
  { ; Before reading, we check if the pipe has been written to, so we avoid freezings.
    If ( !DllCall( "PeekNamedPipe", Ptr,hStdOutRd, Ptr,0, UInt,0, Ptr,0, UIntP,nTot, Ptr,0 ) )
      Break
    If ( !nTot )
    { ; If the pipe buffer is empty, sleep and continue checking.
      Sleep, 100
      Continue
    } ; Pipe buffer is not empty, so we can read it.
    VarSetCapacity(sTemp, nTot+1)
    DllCall( "ReadFile", Ptr,hStdOutRd, Ptr,&sTemp, UInt,nTot, PtrP,nSize, Ptr,0 )
    sOutput .= StrGet(&sTemp, nSize, sEncoding)
  }
 
  ; * SKAN has managed the exit code through SetLastError.
  DllCall( "GetExitCodeProcess", Ptr,NumGet(pi,0), UIntP,nExitCode )
  DllCall( "CloseHandle",    Ptr,NumGet(pi,0)          )
  DllCall( "CloseHandle",    Ptr,NumGet(pi,A_PtrSize)      )
  DllCall( "CloseHandle",    Ptr,hStdOutRd           )
  Return sOutput
}
*/

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。