为什么提示错误?找不到 DHCP服务器地址?
本帖最后由 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 .+?: (?:(?:{1,3}\.){3}{1,3})'
Local $result = StringRegExp($data, $pattern, 3)
If @error Then
Return ''
Else
Return $result
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
这个不知道,可以从英文的网址上下载 好像基本上都用的GuiIPAddress.au3,是不是谁自定义了一个 IPAddress.au3? holley 发表于 2023-8-15 08:33
好像基本上都用的GuiIPAddress.au3,是不是谁自定义了一个 IPAddress.au3?
目测AI自定义了一个IPAddress.au3 这一看就是了AI,哈哈,这种情况无解! 大概率是正则不匹配。中文系统返回的字符串包含中文。所以没有匹配到对应的ip地址。 #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 服务器 .+?: (?:(?:{1,3}\.){3}{1,3})'
Local $result = StringRegExp($data, $pattern, 3)
If @error Then
Return ''
Else
Return $result
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 本帖最后由 wllx 于 2023-8-18 13:39 编辑
gyp2000 发表于 2023-8-18 10:19
测试了一下,一样找不到 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
wllx 发表于 2023-8-18 13:34
测试了一下,一样找不到 DHCP服务器地址
批处理命令是这样的:
win10下测试通过 如果换系统,还要重新修改匹配正则。每个版本的中文翻译是有差异的。 gyp2000 发表于 2023-8-18 17:53
win10下测试通过 如果换系统,还要重新修改匹配正则。每个版本的中文翻译是有差异的。
WIN11下通不过? wllx 发表于 2023-8-18 20:33
WIN11下通不过?
对头 通不过
本帖最后由 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
EndIf
EndFunc ;==>RunCmd
3131210 发表于 2023-8-25 11:28
有点神奇,在我的w11系统里一样不显示
换二进制读取
$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, 1)
EndIf
EndFunc ;==>RunCmd
3131210 发表于 2023-8-25 20:41
换二进制读取
谢谢你!运行之后,还是一样
页:
[1]
2