简体   繁体   English

测试计算机是否在网络上可用

[英]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个批处理脚本:

  1. The first, launched as the superadmin of the computer, install a new user with admin rights to install drivers 第一个以计算机的超级管理员身份启动,安装具有管理员权限的新用户来安装驱动程序
  2. The second, launched as the new user, install Xerox drivers and set as default the printer. 第二个以新用户身份启动,安装Xerox驱动程序并将打印机设置为默认打印机。
  3. The third, launched as the superadmin of the computer, try to install Xerox drivers on other computers of the same shop : 第三,以计算机的超级管理员身份启动,尝试在同一家商店的其他计算机上安装Xerox驱动程序:

    • Loop over my 9 ip address (for /l %%G in (...) do (...) 循环遍历我的9个IP地址(for /l %%G in (...) do (...)
    • For each IP address, test if it ping. 对于每个IP地址,测试是否可以ping通。
    • Do a ftp transfer between the computer 1 and the IP address (drivers packages + scripts) 在计算机1和IP地址(驱动程序包+脚本)之间进行ftp传输
    • PsExec64.exe to run the first script PsExec64.exe运行第一个脚本
    • PsExec64.exe to run the second script PsExec64.exe运行第二个脚本

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.

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