找回密码
 加入
搜索
查看: 745|回复: 25

为什么提示错误?找不到 DHCP服务器地址?

[复制链接]
发表于 2023-8-11 22:47:30 | 显示全部楼层 |阅读模式
本帖最后由 wllx 于 2023-8-16 17:26 编辑


#include <Array.au3>
#include <File.au3>
#include <GuiIPAddress.au3>

Local $dhcpServer = FindDhcpServer()
If $dhcpServer <> '' Then
        WriteConfigFile($dhcpServer)
        MsgBox(64, '成功!', 'DHCP服务器地址已写入配置文件.',3)
Else
        MsgBox(16, '错误', '未找到DHCP服务器地址。'3)
EndIf

Func FindDhcpServer()
        Local $output = Run(@ComSpec & ' /c ipconfig /all', '', @SW_HIDE, $STDOUT_CHILD)
        Local $data = ''
        While 1
                $line = StdoutRead($output)
                If @error Then ExitLoop
                $data &= $line
        WEnd
        Local $pattern = 'DHCP Server .+?: (?:(?:[0-9]{1,3}\.){3}[0-9]{1,3})'
        Local $result = StringRegExp($data, $pattern, 3)
        If @error Then
                Return ''
        Else
                Return $result[0]
        EndIf
EndFunc   ;==>FindDhcpServer
Func WriteConfigFile($dhcpServer)
        Local $filePath = 'config.ini'
        Local $configData = 'DHCP_SERVER=' & $dhcpServer
        If Not FileExists($filePath) Then
                FileWrite($filePath, $configData)
        Else
                Local $fileContent = FileRead($filePath)
                Local $newContent = StringRegExpReplace($fileContent, 'DHCP_SERVER=.+', $configData)
                FileDelete($filePath)
                FileWrite($filePath, $newContent)
        EndIf
EndFunc   ;==>WriteConfigFile
发表于 2023-8-13 21:44:18 | 显示全部楼层
这个不知道,可以从英文的网址上下载
发表于 2023-8-15 08:33:14 | 显示全部楼层
好像基本上都用的GuiIPAddress.au3,是不是谁自定义了一个 IPAddress.au3?
发表于 2023-8-15 18:31:08 | 显示全部楼层
holley 发表于 2023-8-15 08:33
好像基本上都用的GuiIPAddress.au3,是不是谁自定义了一个 IPAddress.au3?

目测AI自定义了一个IPAddress.au3
发表于 2023-8-16 10:45:45 | 显示全部楼层
这一看就是了AI,哈哈,这种情况无解!
发表于 2023-8-17 22:34:16 来自手机 | 显示全部楼层
大概率是正则不匹配。中文系统返回的字符串包含中文。所以没有匹配到对应的ip地址。
发表于 2023-8-18 10:19:38 | 显示全部楼层
#include <Array.au3>
#include <File.au3>
#include <GuiIPAddress.au3>

Local $dhcpServer = FindDhcpServer()
If $dhcpServer <> '' Then
        WriteConfigFile($dhcpServer)
        MsgBox(64, '成功!', 'DHCP服务器地址已写入配置文件.',3)
Else
        MsgBox(16, '错误', '未找到DHCP服务器地址。',3)
EndIf

Func FindDhcpServer()
        Local $output = Run(@ComSpec & ' /c ipconfig /all', '', @SW_HIDE, $STDOUT_CHILD)
        Local $data = ''
        While 1
                $line = StdoutRead($output)
                If @error Then ExitLoop
                $data &= $line
        WEnd
        Local $pattern = 'DHCP 服务器 .+?: (?:(?:[0-9]{1,3}\.){3}[0-9]{1,3})'
        Local $result = StringRegExp($data, $pattern, 3)
        If @error Then
                Return ''
        Else
                Return $result[0]
        EndIf
EndFunc   ;==>FindDhcpServer
Func WriteConfigFile($dhcpServer)
        Local $filePath = 'config.ini'
        Local $configData = 'DHCP_SERVER=' & $dhcpServer
        If Not FileExists($filePath) Then
                FileWrite($filePath, $configData)
        Else
                Local $fileContent = FileRead($filePath)
                Local $newContent = StringRegExpReplace($fileContent, 'DHCP_SERVER=.+', $configData)
                FileDelete($filePath)
                FileWrite($filePath, $newContent)
        EndIf
EndFunc   ;==>WriteConfigFile
 楼主| 发表于 2023-8-18 13:34:37 | 显示全部楼层
本帖最后由 wllx 于 2023-8-18 13:39 编辑

测试了一下,一样找不到 DHCP服务器地址

批处理命令是这样的:

for /f "tokens=2 delims=:" %%a in ('ipconfig /all^|findstr /ic:"DHCP 服务器"') do set PXEIP=%%a
set "PXEIP=%PXEIP: =%"
echo url=%PXEIP% >>ys.ini

发表于 2023-8-18 17:53:56 来自手机 | 显示全部楼层
wllx 发表于 2023-8-18 13:34
测试了一下,一样找不到 DHCP服务器地址

批处理命令是这样的:

win10下测试通过 如果换系统,还要重新修改匹配正则。每个版本的中文翻译是有差异的。
 楼主| 发表于 2023-8-18 20:33:49 | 显示全部楼层
gyp2000 发表于 2023-8-18 17:53
win10下测试通过 如果换系统,还要重新修改匹配正则。每个版本的中文翻译是有差异的。

WIN11下通不过?
 楼主| 发表于 2023-8-19 10:29:54 | 显示全部楼层
wllx 发表于 2023-8-18 20:33
WIN11下通不过?

对头 通不过

发表于 2023-8-25 11:28:25 | 显示全部楼层
本帖最后由 3131210 于 2023-8-25 11:34 编辑
$aa = RunCmd('ipconfig /all')
MsgBox(0,0,$aa)

Func RunCmd($sCommand)
        Local $iPID = Run(@ComSpec & ' /c ' & $sCommand, '', @SW_HIDE, 2)
        ProcessWaitClose($iPID)
        Local $data = StdoutRead($iPID)

        Local $pattern = '\d+[\r\n]\s+?DHCP\s(?:Server|服务器).+?([\d\.]{7,15})'
        Local $result = StringRegExp($data, $pattern, 3)
        If @error Then
                Return ''
        Else
                Return $result[0]
        EndIf
EndFunc   ;==>RunCmd

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2023-8-25 17:55:14 | 显示全部楼层


有点神奇,在我的w11系统里一样不显示

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2023-8-25 20:41:42 | 显示全部楼层
换二进制读取
$aa = RunCmd('ipconfig /all')
MsgBox(0, 0, $aa)

Func RunCmd($sCommand)
        Local $iPID = Run(@ComSpec & ' /c ' & $sCommand, '', @SW_HIDE, 2)
        ProcessWaitClose($iPID)
        Local $data = StdoutRead($iPID, 1, 1)

        Local $pattern = '3\d0D0A(?:20){1,}4448435020(?:536572766572|B7FECEF1C6F7|E69C8DE58AA1E599A8)20.+?203A20((?:(?:3\d){1,3}2E){3}(?:3\d){1,3})'
        Local $result = StringRegExp($data, $pattern, 3)
        If @error Then
                Return ''
        Else
                Return BinaryToString('0x' & $result[0], 1)
        EndIf
EndFunc   ;==>RunCmd
 楼主| 发表于 2023-8-27 20:55:24 | 显示全部楼层

谢谢你!运行之后,还是一样
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-1 23:27 , Processed in 0.083408 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表