Using Ping and Tracert – We write a batch script!
In this article I will show you how to create a batch script on Windows. Before that, you’ll learn what a ping is, what tracert is, and why just querying PING isn’t necessarily enough to test the accessibility of a website. As an example, I will write a script that tests the reachability of Google.com (ping and tracert).
So that you can see the difference of commands and confusing notations yourself, we basically use two batch scripts, which all achieve almost the same result. What you need to write and run my script yourself is just a computer with a Windows system, a text editing program like Editor and just a little bit of time.
What are batch scripts and how are they started?
Batch scripts are small or larger programs which are written with commands for the Windows command prompt, as in this case. The Windows command prompt can process the commands of the programs accordingly and also run them automatically. A batch script is started by opening a .bat file. You can also include commands in a batch script which, for example, ensure that other .bat files are executed.
What is Ping?
Ping indicates the latency (the duration of the route in milliseconds) from your computer to a requested host / IP address and back. Ping can be used to calculate the exact time it took to reach the destination. For a ping query, the command ping google.com is entered in the Windows command prompt. Ping can be used to determine whether a server can be reached at all or whether, for example, a high ping indicates poor accessibility.
What does the Tracert command do?
The command tracert Google.com checks all necessary hops from your computer to Google.com. Hops are basically intermediate stations (e.g. routers) in the network, from your starting point (your computer) to the destination (Google.com). In the process, a maximum number of hops is usually already set with the number 30. This means that requests are being sent from your computer, are not allowed to pass more than 30 hops to reach the destination. Each request should receive a reply, and here the passed hops are counted. With IPv4 still TTL (Time to Life) can be used for this purpose, with IPv6 this is called Hops Limit, because IPv6 does not use TTL – works from the counting principle like TTL.
With the use of IPv4 in connection with Time To Life it would be then in such a way: Computer sends with tracert now TTL1 to the closest hop. If a Hop receives TTL1, this sets TTL1 by a value down, thus on TTL0 and sends the data back to the computer. Now the computer sends TTL2 off, the closest hop, sets TTL2 again down by one value (to TTL1), this packet is passed on and at the next hop again set down by one value, to TTL0. Once there (at zero), acknowledgement/feedback is sent to the computer. The counting process at TTL ensures that sent packets are not “lying around” endlessly in the network or are sent back and forth senselessly.
So the computer starts with TTL1 and increases before each renewed sending TTL by a value, as long as the target address is reached. Of in each case last reached hop, between computer and Google.com, the message TTL exceeded is sent back to the computer. If the destination is reached, a correct echo-reply comes back, instead of TTL exceeded. This way the computer knows that the target has been reached and terminates the process. The process is also terminated if the request or data packets have passed the maximum number of hops and the destination has not yet been reached.
Why is ping not enough?
Ping is not sufficient because you can only measure the latency with it. If this is greatly increased or you are losing packets along the way, then you can use Tracert to find out some possible causes for this. As a newcomer to the field, you can quickly see, for example, that your router is not responding, that there are frequent redirections (when repeating the command) or that the route itself is very long due to many hops. If you know the route that has been set, you can also find out whether it is being adhered to or not. If not, it can then be further tested whether the “missing” IP address could be reached at all from the actual route. If you are interested in geographical details of an IP address, you can search for it on the website https://tools.keycdn.com/geo, for example. Note that the information on the website is not 100% correct.
Understand the result of ping google.com
- In my case, the IP address of Google.com is 2a00:1450:4001:80e::200e (an IPv6 address);
- four packets of data are sent to Google.com and back;
- from the ping statistics, I can see that four packets were sent and four replies to them were received. No packet was lost. A packet would be considered lost if my computer did not receive feedback on the packet sent;
- the minimum, maximum and mean value of the previously measured ping values are recorded for each time or, in the case of the mean value, calculated;
- if you are using Internet Protocol Version 4 (IPv4), then TTL (number) and bytes (number) are also output in each line.
Result: Google.com is reachable at this moment and no information is lost. Top. The average value of 21ms is good in my case. When using internet via a cable, the milliseconds should be between 20-25. The standard value for fibre optics, for example, is between 5 and 10 milliseconds. If you want to calculate the milliseconds in seconds, use the following conversion formula:
Milliseconds x 0.001 = seconds
By the way: Four echo request packets (sometimes three) are sent because it would not be correct to say that a host is not reachable if you send only one packet and do not receive a reply. A packet can be left in many places or get lost, sometimes for this reason, several are sent.
Writing a batch script
To create a batch script on Windows you need an editor. There you write a command in every single line and save it under the extension .bat. Using one line for each command is less confusing if you are just starting with batch scripts or if you are passing a script to another person.
With my batch-script, we can test how the ping to Google.com and the traceroute is without entering everything manually each time. In addition, with the execution of the script, a .txt file is created, in which all the results, of each execution are documented. Of course, with date and time of the respective execution.
You can use the following 1 to 1 with you and you should see that it works. It’s best to use a separate .bat file for each one to better see the differences in execution. My scripts are followed by brief explanations of the commands used.
Tidy Skirpt – Step by Step
@echo off echo We start the test. timeout 8 rem Example. echo Date >> results_of_ping_and_tracert_google.txt date /t date /t >> results_of_ping_and_tracert_google.txt echo Uhrzeit >> results_of_ping_and_tracert_google.txt time /t time /t >> results_of_ping_and_tracert_google.txt ping google.com ping google.com >> results_of_ping_and_tracert_google.txt tracert google.com tracert google.com >> results_of_ping_and_tracert_google.txt timeout 50 exit
@echo off && Echo Date >> Unfavorable_script.txt && date /t >> Unfavorable_script.txt && Echo Uhrzeit >> Unfavorable_script.txt && time /t >> Unfavorable_script.txt && ping google.com >> Unfavorable_script.txt && tracert google.com >> Unfavorable_script.txt
As you will see, the .txt files are all the same in the end, the only difference between the first and second script is the omission of the comments. The last example is to show you how chaotic a script can become if you don’t stick to “line by line”.
Commands in batch script- explanation
Echo: With echo you can display messages. You can also use @echo on and @echo off to enable or disable the command echo function.
REM: If you want to use comments in your batch script, which should be skipped in the execution of the batch script, you use REM. Some also use REM as a placeholder for lines. The latter is not necessary, since empty lines are skipped in a batch script anyway. Comments are not displayed during execution. If desired, you can set it with @echo on that the comment is printed in the CMD.
Pause: Pause is used so that the window does not close automatically immediately after processing one or more commands. It waits for an input from the user before the program continues.
Timeout X: This provides time to read comments while executing a .bat file, for example. You can replace the X with any number. The number you enter runs down like a countdown, only at 0 the next command is executed, unless the timeout is interrupted by a keyboard entry. With the addition /nobreak you prevent the possibility to stop the countdown with a single keystroke. /nobreak does not prevent an interruption completely, but makes it more complicated by asking for two keys at the same time and an additional “confirmation prompt”.
With the command timeout -1 /nobreak you force the user to react. By -1 no countdown starts.
@echo off: @echo off is written at the beginning of a batch script to not display the commands themselves. @echo on has the opposite effect.
date /t: The command date alone would output the date and then wait for an input from you (e.g. to change the date). With the addition /t, however, only the date is output without waiting for an input from you.
The faster alternative: Echo %Date%.
Time /t: The command time alone would output the time and then wait for an input from you (e.g. for a change of the time). With the addition /t, however, only the time is output without waiting for an input from you.
The faster alternative: Echo %Time:~0.8% (-0.8 ensures that only the first 8 digits are read).
Exit: Exit terminates the .bat file and all parents. However, the window would close anyway as soon as the last command was executed. Exit also closes all parents if the executing script is a child. Exit /b closes only the executing script and leaves parent scripts untouched.
&&: Executes the next command if the previous command has completed.
>> XYZ.txt: Creates a .txt file in the same location (e.g. a folder) where the .bat file is stored. If the file already exists, results, the contents of the file, are appended in text form. XYZ can be replaced with the desired file name. If you omit .txt, a file is still created, which only has no assignment of the data form.
If you have followed our scripts and you have read and understood the explanations of the commands, then you are well prepared for the next article. In the next article we will show you how to read and process .txt files using CMD and how to directly read and process output within CMD. You will also learn about the IF and For query.