简体   繁体   English

具有for,if和reg查询循环的嵌套批处理脚本

[英]A nested batch script with for , if and reg query loops

I am working on a batch script which will query some registries on other windows computers. 我正在处理批处理脚本,它将查询其他Windows计算机上的某些注册表。 It is going to iterate through all of the live IP addresses and that too after checking if I get the TTL from 120 to 128. Another requirement is to first check if first registry query is successful on every machine. 在检查我的TTL是否从120变为128之后,它将迭代所有活动的IP地址。另一个要求是,首先检查每台计算机上的第一个注册表查询是否成功。 For example it will start from 192.168.1.1, ping it once. 例如,它将从192.168.1.1开始,对其执行ping操作一次。 Get the TTL value. 获取TTL值。 If TTL ranges from 120 to 128 then it is a windows system. 如果TTL在120到128之间,则为Windows系统。 Now query first registry. 现在查询第一个注册表。 If the query was successful then proceed querying other 6 more registries and keep writing the results to a txt file. 如果查询成功,则继续查询其他6个注册表,并将结果继续写入txt文件。 The ping part I was able to get using following: 我能够使用的ping部分如下:

for /l %%a in (1,1,254) do for /f "tokens=8 delims=^= " %%b in ('ping -n 1 192.168.1.%%a ^| find /I "TTL"') do echo 192.168.1.%%a -- Online , TTL: %%b

Now I need to add IF condition to it. 现在我需要添加IF条件。 But it doesn't show any output: 但是它没有显示任何输出:

for /l %%a in (1,1,254) do for /f "tokens=8 delims=^= " %%b in ('ping -n 1 192.168.1.%%a ^| find /I "TTL"') do if %%b==128 echo 192.168.1.%%a -- Online , TTL: %%b

Next I need to query following registry: 接下来,我需要查询以下注册表:

reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName /v computername 2>null >> query_result.txt

If it is success, query rest of the registries. 如果成功,则查询其余注册表。 And this all process has to be repeated for all 254 IP addresses. 并且必须对所有254个IP地址重复此所有过程。 In a plain and slow working way I did it followingly (for example): 我以一种普通而缓慢的方式进行了以下操作(例如):

for /l %%i in (1,1,254) do (reg query \\192.168.1.%%i\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName /v computername >> result.txt & reg query \\192.168.1.%%i\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion /v productname 2>null >> result.txt)

Although above code also works, but it is extremely slow, becoz it tries to query registry on dead IP addresses also. 尽管上面的代码也可以工作,但是它非常慢,因为它也尝试在死IP地址上查询注册表。 I had been following the three methods seperately in pieces using 3 result files. 我使用3个结果文件分别跟踪这三种方法。 I want to club them. 我想和他们打成一片。 How can I add all of them together? 如何将它们全部加在一起? Plz clarify the application of IF conditional statements, on how to use them. Plz阐明了IF条件语句的应用,以及如何使用它们。 I've been trying for 2 days but no success. 我已经尝试了2天,但没有成功。

Thanks marc_kriss 谢谢marc_kriss

for /l %%a in (1,1,254) do for /f "tokens=8 delims=^= " %%b in ('ping -n 1 192.168.1.%%a ^| find /I "TTL"') do if %%b==128 echo 192.168.1.%%a -- Online , TTL: %%b

Close, but it will only output if %%b is exactly 128. If you want %%b in the range 120 to 128 , then you'd need 关闭,但仅在%%b恰好为128时输出。如果您希望%%b120 to 128的范围内,则需要

... do if %%b leq 128 if %%b geq 120 echo ...

where leq means less than or equal to. 其中leq表示小于或等于。 I'll let you guess what geq means... 我会让你猜geq是什么意思...

Instead of the echo here, you could naturally use 除了这里的echo ,您自然可以使用

... geq 120 (
  echo processing 192.168.1.%%a onine TTL %%b
req query...192.168.1.%%a ...
)

to perform the reg query using the selected on-line machine addreses. 使用选定的在线计算机地址执行reg查询。


Response to comment: 对评论的回应:

On my machine, I found 在我的机器上,我发现

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 192.168.0.1: bytes=32 time=1ms TTL=64

Since you didn't post a sample filtered-ping result either, I have to use this data. 由于您也没有发布样本过滤的结果,因此我必须使用此数据。

Now in the first result, 128 appears as %%b` 现在在第一个结果中, 128显示为%% b`

In the second, TTL appears since there's a = as time , not < . 在第二个中,由于出现了= time ,而不是< ,因此出现了TTL So tokens would need to be 9 not 8. 因此令牌必须是9而不是8。

Or you could assign the entire string to an environment variable yourstring in delayedexpansion mode and then use 或者,您可以在delayedexpansion模式下将整个字符串分配给环境变量yourstring ,然后使用

set "yourstring=!yourstring:*TTL=!"
set "yourstring=!yourstring:~1!"

Then you'd need to compare !yourstring! 然后,您需要比较!yourstring! to 120,128 instead of %%b . 到120,128而不是%%b


Revision 调整

@ECHO OFF
Setlocal EnableDelayedExpansion 
for %%a in (192.168.0.1 192.168.0.2 192.168.0.3 127.0.0.1) do for /f "delims=" %%b in ('ping -n 1 %%a ^| find /I "TTL"') do (
 ECHO TTL line found FOR %%a is %%b
 set "yourstring=%%b" 
 set "yourstring=!yourstring:*TTL=!" 
 set "yourstring=!yourstring:~1!"
 ECHO yourstring is "!yourstring!"
 if !yourstring! leq 128 if !yourstring! GEQ 65 (
  echo %%a online , TTL: !yourstring!
  ECHO now DO whatever with "%%a" or "!yourstring!"
  reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName /v computername 2>nul 
 )
 ECHO end report FOR %%a ===============================
)
GOTO :EOF

Results on my machine: 我的机器上的结果:

TTL line found FOR 192.168.0.1 is Reply from 192.168.0.1: bytes=32 time<1ms TTL=64
yourstring is "64"
end report FOR 192.168.0.1 ===============================
TTL line found FOR 192.168.0.2 is Reply from 192.168.0.2: bytes=32 time<1ms TTL=128
yourstring is "128"
192.168.0.2 online , TTL: 128
now DO whatever with "192.168.0.2" or "128"

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName
    computername    REG_SZ    OWEN-PC

end report FOR 192.168.0.2 ===============================
TTL line found FOR 127.0.0.1 is Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
yourstring is "128"
127.0.0.1 online , TTL: 128
now DO whatever with "127.0.0.1" or "128"

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName
    computername    REG_SZ    OWEN-PC

end report FOR 127.0.0.1 ===============================

Note: error in the selection of the string after TTL corrected - a stray ~ sneaked in (corrected) 注意: TTL更正后,选择字符串时出错-流浪~潜入(更正)

I've used a set selection of addresses - you're obviously aware of how to return this to for /l %%a ... and how to build 192.168.1.%%a as required. 我使用了一组地址选择-您显然知道如何将其返回到for /l %%a ...以及如何根据需要构建192.168.1.%%a

I can't test what you appear to want to retrieve from the active machines (computer name?) as I don't have any other machines active. 由于没有其他活动的计算机,因此我无法测试您似乎想从活动的计算机中检索什么(计算机名称?)。 I've just used what came back with a TTL response. 我刚刚使用了TTL响应返回的内容。

Note nul not null Using the latter will create a file called null . 注意nul not null使用后者将创建一个名为null的文件。

Note that for /l takes 3 parameters - there are 4 on your comment. 请注意, for /l需要3个参数-您的注释中有4个。

The caret is not required (but harmless) in "delims== " The characters between the first = and the " are delimiters - but the space must be last (for future reference) - "delims=" turns off all delimiters as the delimiter-count is not reliable in this application. "delims== " ,不需要插入符号(但无害),第一个="之间的字符是定界符-但空格必须为最后一个(以供将来参考)- "delims="关闭所有定界符作为定界符-count在此应用程序中不可靠。

Note also I've removed the redirection to query_result.txt to show the results on-screen. 另请注意,我已删除了对query_result.txt的重定向以在屏幕上显示结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM