CMD - Ping and traceroute from list of ips - batch-file

We have sporadic connection failures when webserver tries to connect to service on the net.
There is a problem to trace failure from PHP for many reasons.
I'm a web-programmer and not familiar with command-line scripts. Can anyone help with following cmd-script:
-there is a list of ips separated by newline in text file (ip_list.txt)
-take ip from list and ping it, if it fails on first attempt - traceroute it
-go to next ip in file

I don't really sure what you want to test, but here's a pretty useful command to test ping. Enter ping IP_ADDRESS -l 10 -n 10 directly to cmd, change the IP_ADDRESS to ip address you want. -l 10 - Ping you ip address with 10 bytes of data
-n 10 - Ping for 10 times
ping /? - For more informations
To get each ip address (line by line) in a text file, use for /f %%a in (YOUR_FILE.txt) do ( //to do ). Since I'm not sure about what you want, so that's all I can help with :)

Related

How to force Windows Comand Prompt to flush text output to a file line-by-line

I'm trying to log every ping in my Windows CE5.0 machine Command Prompt using
> ping 192.168.1.1 -t -l 60000 >> ping.txt
The file starts with a single line of output and then only flushes after pressing 'ctrl+c'.
I was wondering if there was a way to force it to print in every new line.
I do not know any direct way to do this.
But you could work around that by doing single ping requests in a (infinite) loop and write to the log file in the loop, like this:
for /L %I in () do #(timeout 1 > nul & ping 192.168.1.1 -n 1 -l 60000 | find "TTL=" >> "ping.txt")
The timeout 1 command establishes a one-second delay in every loop iteration in order to avoid heavy CPU load, > nul suppresses its console output.
The find command is used to filter for lines containing TTL from positive replies (like Reply from 192.168.1.1: bytes=60000 time<1ms TTL=128). If you want, you can change that to findstr /B /C:"Reply from " /C:"Request " /C:"Ping request ", for example, to capture positive replies as well as negative ones like Request timed out. or Ping request could not find host ..., or you can remove it completely (also the |) to write the whole ping response to the file, including header and footer.

Working with ping localhost in Batch

We use ping localhost -n 2 >nul to delay its following executions.
We can change 2 to the number of seconds needed.
How can I control this in a much broader way? I tried using 1.5 instead of 2 and it didn't work.
Is there any code by which we can change the unit of time?
EDIT: Instead of ping localhost -n 2 >nul. I'm using TIMEOUT 1 >nul.
The command timeout is the best choice for waiting a specific time in a batch file which is designed for execution on Windows 7 and later versions of Windows. It supports breaking the timeout by the user with any key except /NOBREAK is specified as parameter. And it shows a nice message with a seconds countdown for the user informing also the user how to break the timeout. But it supports only timeout values in seconds, not in milliseconds.
The command sleep could be also used on Windows XP and later versions of Windows when having access to Windows 2003 resource kit and this small executable is copied to all computers running the batch file. But this executable is deprecated because of being replaced by TIMEOUT and by default not installed on any Windows computer.
But a good choice for all Windows is using the command ping for pinging the loopback adapter or a not reachable IP address with using appropriate values of the options -n and -w for the delay.
The IP address of the loopback adapter of local machine is 127.0.0.1, see Wikipedia articles about Reserved IP addresses. localhost is just an RFC defined alias for 127.0.0.1 defined on Windows XP and former Windows versions in file %SystemRoot%\system32\drivers\etc\hosts and is defined built-in on Windows Vista and later Windows versions.
The first ping of 127.0.0.1 is always immediately successful. Therefore using command PING with -n 1 as option gives just a delay of approximately a millisecond in total.
For that reason using PING as delay on pinging 127.0.0.1requires a value greater 1 for option -n ... number of echo requests to send. After a successful request PING waits about 1 second before making the next request.
So for a delay of 5 seconds the following command line is necessary with 6 echo requests:
%SystemRoot%\System32\ping.exe 127.0.0.1 -n 6 >nul
Note 1: Windows is not a real-time operating system and for that reason the time is not 100% accurate, but should be good enough for a batch file.
The option -w defines in milliseconds how long ping (Microsoft documentation) waits for an echo on the request. It does not define the time between two successful requests. Therefore this option can't be used to fine tune the delay on pinging the IP address 127.0.0.1 as this request is successful in less than 1 millisecond and value of option -w does not matter.
So for a delay in milliseconds instead of seconds it is necessary to ping an IP address which is definitely or at least most likely not reachable and which is not routed via networks because of being a private network address according to RFC 6761.
An example is:
%SystemRoot%\System32\ping.exe 168.192.255.253 -n 1 -w 1500
The IPv4 address range from 168.192.0.0 to 192.168.255.255 is for private networks. The highest address 192.168.255.255 in this network is the broadcast address and is not used for devices. It is common to configure a router with local area network broadcast address minus 1 which means 192.168.255.254 could be assigned to a router in case of current computer is part of this private network. And other devices in a LAN get assigned usually the IPv4 addresses from lowest address plus 1 upwards. Therefore for IPv4 network 168.192.0.0/16 the IP address 168.192.255.253 is most likely not assigned to any device which would respond on the echo request of PING.
Well, the milliseconds delay is not very accurate. But is it really important on execution of a batch file to wait exactly 1500 ms?
Note 2: This approach does not work if the computer on which the batch file is running is currently not connected to any network. Without any network connection each echo request is always immediately terminated and PING outputs for each echo request the error message:
PING: transmit failed. General failure.
The general failure is no network (connection) present at all and therefore only echo requests to local loopback adapter work.
Unfourtanelty, #thx1138v2's solution only delays 0.04 seconds on my machine. Therefore, I've modified his solution to make it more accurate.
ping 1.1.1.1 -n 1 -w 1500 >nul
1500 stands for 1500 milliseconds, which is 1.5 seconds.
ping is inaccurate when pinging a small amount of time, see this table:
Milliseconds In Code | Actual Waited Time
1500 | 1.24 seconds - 1240 milliseconds
1600 | 1.34 seconds - 1360 milliseconds
1700 | 1.52 seconds - 1520 milliseconds
As you can see, 1700 ms's wait time is much precise than 1500 ms, so you may need to consider some extra milliseconds.
Note: ping only supports delay more than 99 milliseconds
-n is the (n)umber of times to ping, not the amount of time to wait. You can't ping 1.5 times.
-w is the time to (w)ait on each ping in milliseconds. To pause 1.5 seconds would be
ping -n 3 -w 500
If there is a web site set up on the machine running the batch file the ping will find it as localhost and the timeout will not apply. The timeout only applies to failed requests. It is better to ping 0.0.0.1 for a delay.
ping -n 3 -w 500 0.0.0.1

insert command into start cmd

I am trying to insert the content of a text file into a cmd
console, by using:
start cmd.exe < c:\text.txt
I also tried:
start cmd.exe | c:\text.txt
However, the both open the cmd shell but nothing gets passed.
My point being in the end that I have a scheduler jenkins, and I pass the content of the text file inside the console when opening it with the start command. So I am not simply trying to print to the cmd console; I could just use echo for that different case.
Something like this?
from CMD.exe itself,
type C:\Text.txt
If it is a batch file, then
type C:\Text.txt
pause
or to just see the content, use more
more C:\Text.txt
If you want to actually run commands from the file instead of trying to insert the commands from the text file into the cmd console, you should rather build it as a batch file which is an executable. you do this by renaming the file to either .bat or .cmd
you then insert you commands into the file and execute it by either double clicking the file or running it from a scheduler etc. Here is an example of a batch or cmd file:
echo Please wait while I execute.
tp merge $/ServerFolderA $/ServerFolderB
So just a few explanations on your initial commands. When you ran:
start cmd.exe | c:\text.txt
you actually told the system to run multiple executables from a single command. The pipe commands is like separator to specify each command. so this:
ping 127.0.0.1 | nslookup www.google.com | cmd.exe | c:\text.txt
will actually do all of those commands in sequence, first it will ping, the do nslookup, open cmd.exe then open c:\text.txt
Here you were on the right track, but my guess is you had a single line in the file and not a new line.
start cmd.exe < Text.txt
That will use the Text.txt file as an answer file so if I edit it and insert the following:
echo This is an answer file
ping 127.0.0.1
ping 10.132.4.99
echo Completed all commands
and then run start cmd.exe < Text.txt it will execute everything in sequence. This difference here is it reads the file line by line and displays each commands it runs. so your output will be something like this:
C:\>echo this is an answer file
this is an answer file
ping 127.0.0.1
Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Ping statistics for 127.0.0.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
ping 10.132.4.99
Pinging 10.132.4.99 with 32 bytes of data:
Reply from 10.132.4.99: bytes=32 time=3ms TTL=254
Ping statistics for 10.132.4.99:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 3ms, Maximum = 8ms, Average = 4ms
As you can see this works perfectly, but it displays each command you are running except, it does not run the very last command which is echo Completed all commands. So to run all commands you always have to add a new line after your last commands. When however you rename it to .cmd it will just run the commands without displaying the commands to run and runs each line until the end. The other issue with the answer file is it reads line by line, so having 3 new lines with no text in the answer file will result in something like this
C:\>
C:\>
C:\>
So having just this in the answer file:
ping 127.0.0.1
will not work as it is a single line without the enter section.
but by just adding a new line after it will make it work.
I hope all this makes some sense.

PuTTY command line automate serial commands from file

I am trying to connect to a serial port and send a series of commands from a file.
Firstly I have mananged to connect via the following:
PuTTY.exe -serial COM3 -sercfg 57600,8,n,1,N
E.g. I have a file called commands.txt with a series of serial commands I wished to be sent.
I tried the following however it failed to work:
PuTTY.exe -serial COM3 -sercfg 57600,8,n,1,N -m commands.txt
Any help is greatly appreciated.
Try like this :
for /f "delims=" %%a in ('type commands.txt') do PuTTY.exe -serial COM3 -sercfg 57600,8,n,1,N -m %%a
Another solution which I have used to regularly send commands to a device uses a combination of PuTTY and Autohotkey.
For the initial setup, configure a PuTTY session and save it. In my case I named is Oasis.
The following Autohotkey function can send a command to the already open PuTTY session. If PuTTY is not open it will start the saved session. oasis_putty_name() is the name of the PuTTY window once it's open, it will depend on the COM port selected. location_putty() is the location of the PuTTY executable. Both of these can be hard coded but I wanted to keep the variables separate from the functions.
; Oasis Check --------------------------------------------------
oasis_check(){
putty_name := oasis_putty_name()
; Start PuTTY if it's not already running
IfWinNotExist, %putty_name%
{
putty := location_putty()
Run %putty% -load Oasis
Sleep,1000
}
; Format Time Stamp
FormatTime, TimeString,,yyyy-MM-dd HH-mm-ss
; Record Oasis Values
ControlSend, , %TimeString%{ENTER}, %putty_name%
Sleep, 2000
ControlSend, , all?{ENTER}, %putty_name%
}
The frequency of execution can be controlled with another Autohotkey script or, as in my case, with the Windows Task Manager.

Save ping results to text file with Computer name

I'd like to use a variation of the following batch script to save the results of a ping test to a folder on the network with the computer name appended to the file name or somewhere in the results.
ping www.google.com -n 1000 > pinglog.txt
type pinglog.txt
I need to perform some diagnostics on all computers of a network to determine whether a connection stability issue is router related, internet related or localised to just one of the computers.
The batch file will be stored on the network at \\192.168.1.254\ICT\Scripts, and I would like the log files saved to that location with the computer name added so that I can determine which PC the results belong to (eg. pinglog-reception.txt).
Can this be done? If so, how?
If you want to do this for different servers, then:
server=www.google.com
ping %server% -n 1000 > \\192.168.1.254\ICT\Scripts\%server%.txt
type \\192.168.1.254\ICT\Scripts\%server%.txt
If you want to do this for different querying computers on your own network, then see the other answer. You will need to use the network name for the file:
ping www.google.com -n 1000 > \\192.168.1.254\ICT\Scripts\pinglog-%COMPUTERNAME%.txt
Use the %COMPUTERNAME% environment variable:
ping www.google.com -n 1000 > pinglog-%COMPUTERNAME%.txt
pathping
would seem to be better suited to this than ping.

Resources