如果我想运行此脚本
@echo off
setlocal enabledelayedexpansion
set OUTPUT_FILE=results.txt
>nul copy nul %OUTPUT_FILE%
for /f %%i in (servers.txt) do (
set SERVER_ADDRESS=ADDRESS N/A
for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
if %%x==Pinging set SERVER_ADDRESS=%%y
if %%x==Reply set SERVER_ADDRESS=%%z
if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
)
echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE!
echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! >>%OUTPUT_FILE%
)
从文件夹
C:\ Documents and Settings \ Admin \Рабочийстол\
然后cmd.exe打开并在里面我看到每行代码发布到cmd.exe并且bat不起作用。或cmd.exe在关闭cmd.exe时运行并变黑并且脚本循环无限次。
如果我把这个脚本放到c:\并运行它,那就像requeried一样。
如何从桌面运行bat文件?
答案 0 :(得分:1)
请参阅this code snippet for analysing ping.exe
output for IPv4(保存并在下面以42046325_30852528.bat
运行)。运行它会显示原始脚本(保存并在42046325old.bat
下面运行)可能会返回完全错误的结果,例如192.168.1.12
IPv4地址Destination host unreachable
。
无论是否运行
,两个脚本都会给出相同的结果cmd
窗口输入路径。注意cmd
中的chcp
command:我需要更改活动控制台Code Page to 1251
(Russian)或更改为65001
(UTF-8),因为我的默认系统区域设置为1250
<强>输出强>:
d:\bat> chcp 1251
Active code page: 1251
d:\bat> "D:\bat\SO\Рабочий стол\42046325_30852528.bat" b
hostname OP IPv4_address explanation
-------- -- ------------ -----------
foo.bar ## foo.bar Ping request could not find host
google.com == 216.58.201.110 Reply from 216.58.201.110: bytes=32 time=9ms TTL=55
www.seznam.cz == 77.75.79.53 Reply from 77.75.79.53: bytes=32 time=9ms TTL=247
192.168.1.1 == 192.168.1.1 Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
192.168.1.12 ?= 192.168.1.12 Reply from 192.168.1.100: Destination host unreachable.
bmw.com =? 160.46.244.131 Pinging bmw.com [160.46.244.131]: Request timed out.
origin.bmw.com =? 160.46.244.131 Pinging origin.bmw.com [160.46.244.131]: Request timed out.
Press any key to continue . . .
d:\bat> "D:\bat\SO\Рабочий стол\42046325old.bat"
foo.bar [ADDRESS N/A] is DOWN
google.com [216.58.201.110] is UP
77.75.79.53 [77.75.79.53] is UP
192.168.1.1 [192.168.1.1] is UP
192.168.1.12 [192.168.1.100] is UP
bmw.com [bmw.com] is DOWN
160.46.244.131 [160.46.244.131] is DOWN
Press any key to continue . . .
d:\bat>
顺便说一句,分析Internet Protocol version 6 (IPv6)的ping.exe
输出将是完全不同的任务。