user2593869 user2593869 - 5 months ago 16
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 4.2.2.2 just for example)

Reply from 4.2.2.2: 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 4.2.2.2 ^| find "TTL"') do echo %a


It gives me the value
TTL=45
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
set
variables and use
IF-ELSE
loops.

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


Thanks,
kriss

Answer

In vbscript you can do something like this :

strHost = "4.2.2.2"
if Ping(strHost) = True then
    Wscript.Echo "Host " & strHost & " contacted"
Else
    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
        else
            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
    next
    Wscript.echo Msg
End Function 
'***************************************************************************************

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

I tested with this file : file.txt

4.2.2.2
www.google.com
www.google.fr
www.facebook.com
www.stackoverflow.com
www.yahoo.com
www.yahoo.fr
www.developpez.net

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%
)
echo.
color 0A
echo Hit any key to open the LogFile "%LogFile%"
pause>nul
Start "" %LogFile%
Exit /b

:VBS 
(
    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 "4.2.2.2" is : 
53
TTL for "www.google.com" is : 
51
TTL for "www.google.fr" is : 
51
TTL for "www.facebook.com" is : 
81
TTL for "www.stackoverflow.com" is : 
53
TTL for "www.yahoo.com" is : 
48
TTL for "www.yahoo.fr" is : 
48
TTL for "www.developpez.net" is : 
48