Run command based on IP address from text file

与世无争的帅哥 提交于 2019-12-12 03:43:39

问题


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) Run a batch file that will get the IP address, then run the 2nd batch based on that IP address.

OR

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?

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).


回答1:


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

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
::*************************************************************************************


来源:https://stackoverflow.com/questions/41102657/run-command-based-on-ip-address-from-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!