Piping output of process to file asynchronously in windows - windows

I am trying to run a program using "start" and I want to have the output piped to a txt file, for example, I want to have the output of a simple http python server to be logged to a text file:
START cmd /c python -m SimpleHTTPServer 8000^>engineLog.txt
The above will print it to the log file but the problem with that is, it only prints it when the process finishes. Are there any way that I can store it to the log as it is running? Also is it possible to have it log in the txt file as well as having it print out in the console?

Give it a try:
START /b "Your title" "cmd /c python -m SimpleHTTPServer 8000 > engineLog.log | type engineLog.log"
Alternatively, you can get tee port for Windows.

Related

How to redirect Windows cmd output to a text file?

I am trying to monitor the cmd output of a certain executable .exe and use it for another process running at the same time.
The problem is that all cmd redirecting functions ( '>','>>','<' and '|') would only redirect the output after a successful return of the last command.
What I wanted to do is to generate some kind of streaming log of my cmd.
You can run in your process in background by using
start /b something.exe > somefile.txt
Windows 20H2: Redirection works fine when logged in as true administrator but will NOT work when logged in as a created administrative user. I have used it for years to redirect the output of a multi target backup system using Hobo copy to put the console output in a log file. I have never been able to get it to work successfully in Windows 10 ver 19 or 20 on a created administrative user.
You can prefix the command with "cmd /c" to start a new command prompt, and redirect the output of the command prompt:
cmd /c YOUR CODE > TextFileName.txt
Note : Use ONLY single greater than (>)
Since the output of cmd is going to TextFileName.txt.
Whereas this misses the error output, so you are not able to see : Request to UnKnown timed-out for each failed address.

open an application and run a command in that application with a Batch script

Basically I am trying to open an application (mingwenv.cmd) and run a command in it
/c/Users/jab/src/veins-4.4/sumo-launchd.py -vv -c /c/Users/jab/src/sumo-0.25.0/bin/sumo-gui.exe
via Batch scripting, analogous to opening, say, python.exe in a new command prompt and running
python -m pip install xlutils
on the command line interface in the new command prompt.
I've spent several hours trying to do so, to no avail.
Below are my documented attempts:
sthcmd C:\Users\jab\src\SUMO_OMNeT_connection.txt | C:\Users\jab\src\omnetpp-5.0\mingwenv.cmd
cmd /k & sthcmd C:\Users\jab\src\SUMO_OMNeT_connection.txt | C:\Users\jab\src\omnetpp-5.0\mingwenv.cmd
C:\Users\jab\src\omnetpp-5.0\mingwenv.cmd | C:\Users\jab\src\SUMO_OMNeT_connection.txt
C:\Users\jab\src\SUMO_OMNeT_connection.txt|C:\Users\jab\src\omnetpp-5.0\mingwenv.cmd
C:\Users\jab\src\omnetpp-5.0\mingwenv.cmd < C:\Users\jab\src\SUMO_OMNeT_connection.txt
type C:\Users\jab\src\SUMO_OMNeT_connection.txt | C:\Users\jab\src\omnetpp-5.0\mingwenv.cmd
type C:\Users\jab\src\omnetpp-5.0\mingwenv.cmd | C:\Users\jab\src\SUMO_OMNeT_connection.txt
echo /c/Users/jab/src/veins-4.4/sumo-launchd.py -vv -c /c/Users/jab/src/sumo-0.25.0/bin/sumo-gui.exe | C:\Users\jab\src\omnetpp-5.0\mingenv.cmd
echo "/c/Users/jab/src/veins-4.4/sumo-launchd.py -vv -c /c/Users/jab/src/sumo-0.25.0/bin/sumo-gui.exe" | C:\Users\jab\src\omnetpp-5.0\mingenv.cmd
start C:\Users\jab\src\omnetpp-5.0\mingwenv.cmd /k /wait "/c/Users/jab/src/veins-4.4/sumo-launchd.py -vv -c /c/Users/jab/src/sumo-0.25.0/bin/sumo-gui.exe"
C:\Users\jab\src\omnetpp-5.0\mingwenv.cmd "/c/Users/jab/src/veins-4.4/sumo-launchd.py -vv -c /c/Users/jab/src/sumo-0.25.0/bin/sumo-gui.exe"
preceding the code block but only the last one worked and the highlighting was vastly off) perhaps someone knows how to do that, if it's possible. In the meantime the code block can be copied into Notepad++ and highlighted there. Anyways, here are the results and references per attempt(s):
crashes (nothing happens) (https://www.codeproject.com/Questions/747981/send-command-to-another-program-with-batch-file)
basically just prints the command to the cmd prompt, seems to expect other commands
opens cmd, txt file, and mingwenv.cmd prompt (3 windows) separately
opens cmd, txt file, and mingwenv.cmd prompt (3 windows) separately
just opens mingwenv.cmd; all else crashes (How to make a batch file that send application an input char)
just opens mingwenv.cmd and cmd prompt only (not txt file) (How do I send commands to an EXE running through the command line with batch?)
just opens txt and cmd prompt only (not mingwenv.cmd)
does nothing (crashes) (How to make a batch file that send application an input char)
does nothing (crashes)
opens mingwenv.cmd and cmd prmpt stays open, that's it though (Bat file to run a .exe at the command prompt)
opens mingwenv.cmd, cmd prompt crashes (BAT file: Open new cmd window and enter code in there)

How to combine batch file and expect script in cygwin?

i have an application on windows that requires user name and password to start, but i don't want user to enter the user/password, so i created a script to interact with CMD interface to enter the password, using cygwin on windows 8.1, but i can't find expect command on the Cygwin, and i would like to ask also if my script has any issue.
below is the script
#!/usr/bin/expect
sh C:\Users\Osama.Barakat\Desktop\batchfile.bat
expect "Enter the password for administrator:"
send "Pa$$w0rd"
interact
I identified two issues in your question:
How to perform batch files in cygwin/bash?
How to get expect installed/running in cygwin/bash?
1. How to perform batch files in cygwin/bash?
In your sample code, I found
---8<---
sh C:\Users\Osama.Barakat\Desktop\batchfile.bat
--->8---
IMHO, this is a mistake. I believe that bash will try to execute anything as script regardless of the script file extension. But if batchfile.bat is really a batch file then it should be called with the correct interpreter (i.e. cmd.exe) instead.
I tried it to see if there is any issue in the cygwin bash but it worked. My sample batch file test-expect.bat:
#echo off
rem output
echo Hello %USERNAME%
rem input
set /p INPUT= Enter input:
rem output
echo Input: %INPUT%
...tested in bash on cygwin:
$ cmd /c test-expect.bat
Hello Scheff
Enter input: Hello cmd
Input: Hello cmd
$
OK, done.
2. How to get expect installed/running in cygwin/bash?
To install expect on cygwin I googled a little bit and found Installation of Cygwin, expect and Ssh. Then, I started the cygwin-setup setup-x86.exe and tried something else:
On the "Select Packages" page, I set View to Full and Search to expect. I got a list of four entries only where the first was: "expect: Tool for automating interactive applications" (Category: Tcl). I choosed this for installation and had to confirm another package as dependency.
After installation, I tried this in an interactive bash:
$ which expect
/usr/bin/expect
$
OK - looks quite good.
Combining CMD.EXE and expect
To put all together, I wrote another test script test-expect.exp:
#!/usr/bin/expect
spawn cmd /c test-expect.bat
expect "Enter input: "
send "Hello expect\r"
interact
Tested in bash on cygwin:
$ ./test-expect.exp
spawn cmd /c test-expect.bat
Hello Scheff
Enter input: Hello expect
Input: Hello expect
$
Well, it seems to work.

Writing pings to files batch

Im looking for the best way to write two instances of cmd ping commands to 2 seperate text files using a batch file.
Here is what I have but its not writing to the file im wondering why.
start cmd /k ping 8.8.8.8 -t >> c:\troubleshoot_connection_google.txt
start cmd /k ping 192.x.x.x -t >> c:\troubleshoot_connection_gateway.txt
Thanks for the help.
The problem with your commands, appart from the point that the root folder of the drive is usually not writteable, is when the redirection to the output file is done.
As you have it written, what is being redirected to the file is the output of the start command, but if you need to redirect the output of the ping command, the redirection must be part of the started command
start "" "cmd /c ping 8.8.8.8 -t >> c:\troubleshoot_connection_google.txt"

How to open a .txt file after using netstat command with Batch

I am trying to open a simple log.txt file (in this example comandos.txt) after running a netstat command like so:
# echo off
echo. >> C:\comandos.txt
netstat -b -o 1 >> C:\comandos.txt
start C:\comandos.txt
After netstat Prompt Windows won't close and comandos.txt won't open.
Any clues on how to solve this?
# echo off
echo. >> C:\comandos.txt
netstat -b -o >> C:\comandos.txt
start C:\comandos.txt
The above snippet works fine. Note that you were specifying the interval in netstat command which redisplays the statistics again and again. Also, because you have echo turned off and display redirected to the file, the empty prompt window showing up for a long time would send wrong signals. Show some message like Collecting information... or similar.
Also, given that the command needs to resolve addresses and depends on the number of processes with network connection, it may take some time for netstat to complete which would be system dependent.

Resources