-2

I have 32 IPs in a file tracerouteiplistA.txt

In for/while loop of a batch file named tracerouteScriptA.bat,

i want to trace route all of them and store the result in traceA.txt

and if 10. is visible hen i want to write 0 in traceresponseA.txt. if 172. is visible then 1 or else if in case of request time out or fail case i want to write 2.

My current code goes as follows --

@echo off
cd C:\Users\Administrator\Desktop
FOR /F "usebackq delims=*" %%a in (tracerouteiplistA.txt) do (
tracert -h 3 "%%a" > traceA.txt
findstr /m "10." traceA.txt
if %errorlevel%==0 (echo "0" > traceresponseA.txt)
findstr /m "172." traceA.txt
if %errorlevel%==0 (echo "1" > traceresponseA.txt) else (echo "2" > traceresponseA.txt)                   
)
  • Beware of the [delayed expansion trap](https://stackoverflow.com/a/30284028/2128947). Use `if not errorlevel 1` in place of `if %errorlevel%==0` – Magoo Mar 03 '21 at 10:54
  • Also, do you expect 32 parallel process to be able to write to the same file without issues? – Compo Mar 03 '21 at 11:10
  • You have not asked a question. What are you having problems with? Why did you tag this question with `powershell` when all of your code is a `batch-file`? – Squashman Mar 03 '21 at 14:54
  • yes Compo, I would like to have 32 parallel tracer routes if possible – RAJ SRIVASTAVA Jun 25 '21 at 05:14

1 Answers1

0

A Powershell Solution :

#get all IP
$Result=Get-Content "c:\temp\tracerouteiplistA.txt" | foreach{

    #for every IP we do a traceroute
    Test-NetConnection $_ -TraceRoute | foreach{

    $Current=$_

    #one line by hop
    $_.TraceRoute | foreach{

            if ($_ -like "10.*")
            {
                $Analyse="0"
            }
            elseif ($_ -like "172.*")
            {
                $Analyse="1"
            }
            else
            {
               $Analyse="2"
            }

            #create an objet and send to output            
            [pscustomobject]@{
            IP=$Current.RemoteAddress
            HopIP=$_
            Analyse=$Analyse
            }
                                

         }

    }



} 

#result into file, we take the first result in priority order
$Result | sort Analyse | select Analyse -First 1 -ExpandProperty Analyse | Out-File "c:\temp\traceresponseA.txt"

#if you want look result
$Result
Esperento57
  • 16,521
  • 3
  • 39
  • 45