根据文本文件中的IP地址运行命令 [英] Run command based on IP address from text file

查看:131
本文介绍了根据文本文件中的IP地址运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近换了工作,开始忙于一些我一直想学习的脚本。给我一个现有的批处理文件,并想对其进行修补。以前,此批处理文件将扫描提示您输入的IP地址。我想更改此设置以基于文本文件中的IP地址列表来循环命令,只有这样做有问题。

I've recently changed jobs and I get to get my hands dirty on some scripting which I've always wanted to learn. I was given an existing batch file and wanted to gussy it up. Previously, this batch file would scan an IP addressed that you are prompted to enter. I want to change this to loop the command based on a list of IP addresses from a text file, only I'm having problems doing that.

我认为可以用以下两种方法之一进行操作:

I figured that I can do this one of two ways:

1)运行一个批处理文件获取IP地址,然后根据该IP地址运行第二批。

1) Run a batch file that will get the IP address, then run the 2nd batch based on that IP address.

OR

2)只是使用现有的一个批处理文件,然后根据文本文件每一行的IP地址将其更改为循环。

2) Just use the one existing batch file and change that to loop based on the IP address on each line of the text file.

什么是更好的处理方式,以及如何

What would be the better way to go, and how would you accomplish both?

对于#1,我试图这样做,但是我不知道如何根据输入的内容运行命令。这样的示例是运行batch.bat 192.168.1.1,在batch.bat中它将尝试ping 192.168.1.1(或输入的任何内容)。

For #1 I tried to do this, but I don't know how to run the command based on what I'm entering. An example of this would be to run batch.bat 192.168.1.1, which in batch.bat it would attempt to ping 192.168.1.1 (Or whatever entered).

推荐答案

假定您已经有一个文本文件: IP_List.txt ,其内容如下:

Suppose that you have already a text file named : IP_List.txt with this contents :

192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7
192.168.1.8
192.168.1.9
192.168.1.10
192.168.1.11
192.168.1.12
192.168.1.13
192.168.1.14
192.168.1.15
192.168.1.16
192.168.1.17
192.168.1.18
192.168.1.19
192.168.1.20
www.google.com
www.stackoverflow.com

您可以尝试使用此批处理文件: MultiPingTester.bat

You can give a try for this batch file : MultiPingTester.bat

@echo off
Title Multi-Ping hosts Tester with colors by Hackoo 2016
call :init
set "MyFile=IP_List.txt"
If Not exist %MyFile% goto error
mode con cols=65 lines=30
set "LogFile=PingResults.txt"
If exist %LogFile% Del %LogFile%
echo(
call :color 0E "      ------- Ping status of targets hosts -------" 1
echo(
(
    echo ******************************************************
    echo   PingTest executed on %Date% @ Time %Time%
    echo ******************************************************
    echo(
) > %LogFile%
Setlocal EnableDelayedExpansion
for /f "usebackq delims=" %%a in ("%MyFile%") do (
    ping -n 1 %%a | find "TTL=" >nul
    if errorlevel 1 (
        call :color 0C " Host %%a not reachable KO" 1 & echo Host %%a not reachable KO >>%LogFile%
    ) else (
        call :color 0A " Host %%a reachable OK" 1 & echo Host %%a reachable OK >>%LogFile%
    )
)
EndLocal
Start "" %LogFile%
pause>nul & exit
::************************************************************************************* 
:init
prompt $g
for /F "delims=." %%a in ('"prompt $H. & for %%b in (1) do rem"') do set "BS=%%a"
exit /b
::*************************************************************************************
:color
set nL=%3
if not defined nL echo requires third argument & pause > nul & goto :eof
if %3 == 0 (
    <nul set /p ".=%bs%">%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
) else if %3 == 1 (
    echo %bs%>%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
)
exit /b
::*************************************************************************************
:error
mode con cols=70 lines=3
color 0C
cls
echo(
 echo       ATTENTION !!!  Check if the file "%MyFile%" exist !
pause>nul & exit
::*************************************************************************************

这篇关于根据文本文件中的IP地址运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆