[英]Test if computer is available on network
I work on a company which has many Shop. 我在一家有很多商店的公司工作。 Each shop has his own IP address : SHOP 1 = 192.168.1.0/24, SHOP 2 = 192.168.2.0/24, SHOP 3 = 192.168.3.0/24, etc...
每个商店都有自己的IP地址:SHOP 1 = 192.168.1.0/24,SHOP 2 = 192.168.2.0/24,SHOP 3 = 192.168.3.0/24,等等...
I have a number of Windows computers (between 1 and 9) on the same subnet (255.255.255.0) on each shop. 我在每个商店的同一子网(255.255.255.0)上有许多Windows计算机(介于1到9之间)。 Each computer has a static IP address : 1st = 192.168.1.10, 2nd = 192.168.1.20, 3rd = 192.168.1.30, 4th = 192.168.1.40, etc.
每台计算机都有一个静态IP地址:1st = 192.168.1.10,2nd = 192.168.1.20,3rd = 192.168.1.30,4th = 192.168.1.40,依此类推。
The HQ of my company can deploy softs ONLY on the first computer on each shop (licences issue). 我公司的总部只能在每个商店的第一台计算机上部署软件(许可证发行)。 Here, we will try to install Xerox drivers on ALL windows computer on all shop.
在这里,我们将尝试在所有商店的所有Windows计算机上安装Xerox驱动程序。
I made 3 batch script : 我做了3个批处理脚本:
The third, launched as the superadmin of the computer, try to install Xerox drivers on other computers of the same shop : 第三,以计算机的超级管理员身份启动,尝试在同一家商店的其他计算机上安装Xerox驱动程序:
(for /l %%G in (...) do (...)
(for /l %%G in (...) do (...)
The first and the second script works perfectly, we'll not speak about them. 第一个和第二个脚本运行完美,我们不再赘述。
Where I need help is on the thrid. 我急需帮助的地方非常重要。
I try 3 scripts : 我尝试3个脚本:
The first : 首先 :
set subnet=192.168.1.
for /L %%G in (20, 10, 90) do (
ping -n 1 %subnet%%%G | findstr "TTL="
if %errorlevel%==0 (
echo Ping %subnet%%%G : OK >> log_ping_errorlevel.txt
) else (
echo Ping %subnet%%%G : KO >> log_ping_errorlevel.txt
)
)
Here the log_ping_errorlevel.txt with 2 computers on my subnet : 这是我的子网中有2台计算机的log_ping_errorlevel.txt:
Ping 192.168.1.20 : OK
Ping 192.168.1.30 : OK
Ping 192.168.1.40 : OK
Ping 192.168.1.50 : OK
Ping 192.168.1.60 : OK
Ping 192.168.1.70 : OK
Ping 192.168.1.80 : OK
Ping 192.168.1.90 : OK`
I tried to surround %errorlevel% and 0 by simple quote but I get the same result. 我尝试用简单的引号将%errorlevel%和0括起来,但得到的结果相同。
Here an extract of the output if I run the batch in cmd window : 这是我在cmd窗口中运行批处理时的输出摘录:
C:\Temp\Xerox_Install>(
ping -n 1 192.168.1.90 | findstr "TTL="
if '0'=='0' (echo "Ping 192.168.1.90 : OK" 1>>log_ping_errorlevel.txt ) el
se (echo "Ping 192.168.1.90 : KO" 1>>log_ping_errorlevel.txt )
)
It seems that the errorlevel is always = 0 in my script, IDK why. 在我的脚本中,似乎错误级别始终为0,IDK为什么如此。
The second : 第二 :
set subnet=192.168.1.
for /L %%G in (20, 10, 90) do (
ping -n 1 %subnet%%%G && set pingtest=ok
if '%ping_test%'=='ok' (
echo Ping %subnet%%%G : OK >> log_ping_errorlevel.txt
) else (
echo Ping %subnet%%%G : KO >> log_ping_errorlevel.txt
)
)
Here the log_ping_set.txt with 2 computers on my subnet : 这是我的子网中有2台计算机的log_ping_set.txt:
Ping 192.168.1.20 : OK
Ping 192.168.1.30 : OK
Ping 192.168.1.40 : OK
Ping 192.168.1.50 : OK
Ping 192.168.1.60 : OK
Ping 192.168.1.70 : OK
Ping 192.168.1.80 : OK
Ping 192.168.1.90 : OK`
I think the ping command result is always 0 then the setter pingtest can be executed. 我认为ping命令结果始终为0,则可以执行setter pingtest。
The third : 第三 :
Because I'm stuck with the ping command, I tried the if exist folder on remote computer : 因为我一直使用ping命令,所以我尝试了远程计算机上的if exist文件夹:
set subnet=192.168.1.
for /L %%G in (20, 10, 90) do (
if exist \\%subnet%%%G\C$\Temp\ (
echo Remote folder reachable >> log_folder_reachable.txt
) else (
echo ERROR 404 - Remote folder not found >> log_folder_reachable.txt
)
)
Here the log_folder_reachable.txt with 2 computers on my subnet : 这是我的子网中有2台计算机的log_folder_reachable.txt:
Remote folder reachable
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
It works BUT the if exist timeover is about 160 seconds and I don't want to wait for 160 seconds before going to the next computer... 它的工作原理是,如果存在的超时时间约为160秒,而我不想等待160秒再转到下一台计算机...
Can someone help me with any method but not using external tools ? 有人可以通过任何方法帮助我,但不使用外部工具吗?
You should read SO items about delayedexpansion
. 您应该阅读有关
delayedexpansion
。
if %errorlevel%==0 (
in the first script should be 在第一个脚本中应该是
if not errorlevel 1 (
[obviously, reverse the logical condition and remove the "not"] [显然,颠倒逻辑条件并删除“ not”]
if errorlevel 1
means "if errorlevel
is 1 or greater than 1 " and is interpreted on the run-time value of errorlevel
. if errorlevel 1
表示“如果errorlevel
为1 或大于1 ”,并根据errorlevel
的运行时值进行解释。
Next: 下一个:
ping -n 1 %subnet%%%G && set pingtest=ok
if '%ping_test%'=='ok' (
should be 应该
set "ping_test="
ping -n 1 %subnet%%%G && set "ping_test=ok"
if defined ping_test (
Notes 笔记
you are setting pingtest
in your ping
command, then you are attempting to test ping_test
您在
ping
命令中设置pingtest
,然后尝试测试ping_test
Even had the variablename been correct, the purpose for the quotes in an if
statement is to combine the contents as a single string. 即使变量名正确,
if
语句中引号的目的也是将内容组合为单个字符串。 Single-quotes don't cut the mustard, double-quotes are required. 单引号不会切芥末,双引号是必需的。
With the code you have, ping_test
once set will retain its value for the next loop, you need to clear it before the ping
. 使用您拥有的代码,一旦设置
ping_test
,它将保留其值用于下一个循环,您需要在ping
之前将其清除 。
The replacement code first sets the value of ping_test
to nothing . 替换代码首先设置的值
ping_test
不了了之 。 The quotes delimit the variable name and value so that trailing spaces on the line are not included in the value assigned. 用引号将变量名和值定界,以使行的尾部空格不包括在分配的值中。 The
ping
command then may set the value of ping_test
to (ok, but any value other than nothing (ie something ) is valid). 该
ping
的命令则可以设置的值ping_test
来(OK,但有总比没有 (即东西之外的任何值)是有效的)。 The if defined
command interprets the run-time value of the variable. if defined
命令将解释变量的运行时值。
The third part - well, getting desperate, but no need to go this far as the changes I've outlined should solve the problem. 第三部分-绝望,但是不必走太远,因为我概述的更改应该可以解决问题。
In regards to your first set of code where you said you tried to use delayed expansion it should look like this. 关于您说过尝试使用延迟扩展的第一组代码,它应该看起来像这样。 Tested and works on my network.
经过测试,可以在我的网络上工作。
@echo off
setlocal enabledelayedexpansion
set subnet=192.168.1.
for /L %%G in (20, 10, 90) do (
ping -n 1 %subnet%%%G | findstr "TTL="
if !errorlevel!==0 (
echo Ping %subnet%%%G : OK >> log_ping_errorlevel.txt
) else (
echo Ping %subnet%%%G : KO >> log_ping_errorlevel.txt
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.