user2593869 user2593869 - 3 months ago 7x
Python Question

nested for loop, set variable and if else in a batch script

I am trying to make a batch script which will write specific IP addresses in a file.
I have a txt file (created by one python script) having a list of IP addresses (one in each new-line), Now I need to ping each of these IPs using for loop, check if the TTL value is between 100 to 128 (windows hosts) and write the IP addresses to a new file. I've been trying to adjust into setting the variables and for loops but this all becomes too complex to run.

So far I've reached:-
EDIT: corrected below command

for /f %i in (ip.txt) do ping -n 1 %i | find "TTL"

This will give me multiple lines Ping output, showing here only for single line (I am using just for example)

Reply from bytes=32 time=1 ms ttl=45

If I do ping to single IP I can pick the TTL field but not TTL's exact value

for /f "tokens=6 delims= " %a in ('ping -n 1 ^| find "TTL"') do echo %a

It gives me the value
whereas I needed 45 for the comparison.
I read about
setlocal enabledelayedexpansion
which is useful, but I am not able to combine these all into a single line and use
variables and use

Plz give a little description on how to achieve the IP selection.



In vbscript you can do something like this :

strHost = ""
if Ping(strHost) = True then
    Wscript.Echo "Host " & strHost & " contacted"
    Wscript.Echo "Host " & strHost & " could not be contacted"
end if
Function Ping(strHost)
    dim objPing, objRetStatus
    set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strHost & "'")
    for each objRetStatus in objPing
        if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode <> 0 then
            Ping = False
            WScript.Echo "Status code is " & objRetStatus.StatusCode
            Ping = True
            Msg = Msg & " Pingging " & strHost & vbCrlf & vbCrlf 
            Msg = Msg & "Bytes = " & objRetStatus.BufferSize & vbCrlf 
            Msg = Msg & "Time (ms) = " & objRetStatus.ResponseTime & vbCrlf 
            Msg = Msg & "TTL (s) = "  & objRetStatus.ResponseTimeToLive 
        end if
    Wscript.echo Msg
End Function 

EDIT : On 30/06/2016 @ 19:11

I tested with this file : file.txt

this batch file :

@echo off
Title Get TTL from IP adress
set vbsfile=%Tmp%\%~n0.vbs
set IP_File=E:\vb-ping\ip.txt
set LogFile=Log.txt
If Exist %LogFile% Del %LogFile%
For /f %%a in ('Type %IP_File%') Do ( 
    echo TTL for "%%a" is : & Call:VBS "%%a"
    ( echo TTL for "%%a" is : & Call:VBS "%%a" )>> %LogFile%
color 0A
echo Hit any key to open the LogFile "%LogFile%"
Start "" %LogFile%
Exit /b

    echo wscript.echo TTL(WScript.Arguments(0^)^)
    echo '**********************************************************************************************************
    echo Function TTL(strHost^)
    echo     dim objPing, objRetStatus
    echo     set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}"^).ExecQuery _
    echo       ("select * from Win32_PingStatus where address = '" ^& strHost ^& "'"^)
    echo     for each objRetStatus in objPing
    echo         if IsNull(objRetStatus.StatusCode^) or objRetStatus.StatusCode ^<^> 0 then
    echo            Ping = False
    echo           WScript.Echo "Status code is " ^& objRetStatus.StatusCode
    echo        else
    echo            Ping = True
    echo            TTL = objRetStatus.ResponseTimeToLive 
    echo        end if
    echo     next
    echo End Function 
    echo '**********************************************************************************************************
)> "%vbsfile%"
Cscript /Nologo "%vbsfile%" "%~1"
Exit /b

And i got as output result like that :

TTL for "" is : 
TTL for "" is : 
TTL for "" is : 
TTL for "" is : 
TTL for "" is : 
TTL for "" is : 
TTL for "" is : 
TTL for "" is :