开机自动设置电脑IP Autohotkey

本文来自于Thinkai’s Blog,thinkai也是我在ahk上面的引路人,对thinkai感兴趣的朋友请关注他的博客。
本文来自于Thinkai’s Blog,thinkai也是我在ahk上面的引路人,对thinkai感兴趣的朋友请关注他的博客。
thinkai的博客简洁、有趣,即便是转载过来我也尽量保持作者原有的风格,主要是方便大家查阅。
功能:开机自动设置活动网卡的IP等信息,并保存配置到同目录ini文件。
;--------------------------开机启动------------------
RegRead, var, HKCU, SoftwareMicrosoftWindowsCurrentVersionRun, SetIP
last := A_LastError
if last=2
    RegWrite, REG_SZ, HKCU, SoftwareMicrosoftWindowsCurrentVersionRun, SetIP, "%A_ScriptFullPath%"
;--------------------------开机启动 结束------------------
 
mac := RegExReplace(GetMacAddress(),"-",":")
 
;检查配置文件
IfExist, %A_ScriptDir%ipinfo.ini
{
    IniRead, ip, %A_ScriptDir%ipinfo.ini, ipinfo, ip
    IniRead, netmask, %A_ScriptDir%ipinfo.ini, ipinfo, netmask
    IniRead, gateway, %A_ScriptDir%ipinfo.ini, ipinfo, gateway
    IniRead, dns, %A_ScriptDir%ipinfo.ini, ipinfo, dns
}
else
{
    MsgBox, 4112, 错误, %A_ScriptDir%ipinfo.ini 文件不存在,已生成,请修改后再使用!
    IniWrite, ip, %A_ScriptDir%ipinfo.ini, ipinfo, ip
    IniWrite, 子网掩码, %A_ScriptDir%ipinfo.ini, ipinfo, netmask
    IniWrite, 默认网关, %A_ScriptDir%ipinfo.ini, ipinfo, gateway
    IniWrite, dns, %A_ScriptDir%ipinfo.ini, ipinfo, dns
    ExitApp
}
 
adaptors := GetAdaptors() ;获取网卡列表
for id,adaptor in adaptors
{
    if(adaptor.mac = mac)
    {
        name := adaptor.name
        Runwait, %ComSpec% /c netsh interface ip set address name=`"%name%`" source=static addr=%ip% mask=%netmask% gateway=%gateway% gwmetric=1, , Hide
        RunWait, %ComSpec% /c netsh interface ip set dns name=`"%name%`" source=static addr=%dns%, ,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
}

使用说明

该脚本用于开机时自动将活动网卡的 IP 设置为固定值,并保存到同目录 ipinfo.ini 配置文件,适合 DHCP 不稳定或需固定内网地址的场景。

使用步骤:

  • 1. 首次运行会在脚本目录生成 ipinfo.ini,按提示填入 ip(IP 地址)、netmask(子网掩码)、gateway(默认网关)、dns(DNS 服务器)四项后保存。
  • 2. 脚本会自动写入注册表 HKCU\Software\Microsoft\Windows\CurrentVersion\Run 实现开机自启;如不想要自启,删除该注册表项即可。
  • 3. 设置 IP 通过调用系统 netsh 命令完成,因此需要以管理员权限运行,否则可能无法修改网络配置。

AHK v1 / v2 迁移提示:本脚本为 AHK v1 语法。迁移到 v2 时注意:%A_ScriptDir%ipinfo.ini 等百分号拼接需改为表达式拼接(如 A_ScriptDir . "\ipinfo.ini");IfExist 改为 FileExist();IniRead/IniWrite 命令改为函数调用;RunWait %ComSpec% 拼接改为 "cmd /c ..." 表达式。核心逻辑(通过 MAC 匹配活动网卡后调用 netsh 设置静态 IP)在 v2 下仍适用。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA捐赠
共{{data.count}}人
人已捐赠
教程

毫秒 时间字符串互转 Autohotkey

2020-2-16 10:06:34

AI 自动化教程

VaxSIPUserAgentOCX.ocx 软电话控件简单使用 AutoHotkey

2020-2-16 10:14:35

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
❯
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索