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

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.

Related

Plink output log coded name?

First of all, sorry if the question doesn't make sense, I'm not the best at English. My question is, I'm trying to save Plink session into a log, that part is now done:
Plink.exe -v -pw [PW] -batch -m C:\BATS\comandosx3.txt root#[IP] > C:\BATS\Logs\outputdegradado.log 2>&1
Now my question is, can I code the output.log name to be stored with date and time so everytime the program starts doesn't write over the last session? for example output2009.log
I didnt know it was related to the batch file, i thought it was plink specific code. Watching for batch specific code, i end up with a single file that registry the date and time at the start of the program.
#echo off
set logfile=C:\BATS\Degradado\Logs\Seguimiento.log
echo starting Plink.exe at %date% %time% >> %logfile%
Plink.exe -v -pw [PW] -batch -m C:\BATS\Degradado\comandosx3.txt root#[IP] oas >> %logfile% 2>&1
Thanks #martin-prikryl for the suggestion

Piping output of process to file asynchronously in 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.

Netcat pipe , redirect out put of cmd to other telented machine

I have got a windows machine and a netcat, basically what i did is, I got into the command line and ran : nc -vv -l -p 1234 | cmd so every connection that comes in every thing that the user writes it pipes it into cmd, thing is the other telneted machine cannot see the output of cmd for some reason, does anybody know an answer to that? , maybe i need to make the cmd output as a stdin. idk
Your question isn't quite clear to me.
Do you want to send the output of a command to a new instance?
ping localhost | cmd
(Note: this will give you syntax errors - just to show the prinicple)
Or do you want to send the command itself to a new instance?
echo ping localhost | cmd

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"

Windows batch script to print error message if port in use

I'm trying to write a batch script that errors if port 1099 is already in use.
Unfortunately I have to write it in a DOS batch script (I cannot install anything).
I know that I can print the PID of the process hogging port 1099 manually:
netstat -aon | findstr ":1099"
But I want to be able to run that command in a batch script and exit the script with an error message if that command has any output.
I suppose at a push I could redirect the output to a temporary file and test the size of it but that seems really hacky...
netstat -an | FINDSTR ":1099" | FINDSTR LISTENING && ECHO Port is in use && EXIT 1
You can use && in a batch script to run a command only if the previous command was successful (based on its exit code/ERRORLEVEL). This allows you to display a message and exit only if the string you are looking for is found in the output of netstat.
Also, you want to explicitly look for LISTENING ports.
FINDSTR supports regular expressions so you can also do the following to shorten the command line:
netstat -an | findstr /RC:":1099 .*LISTENING" && ECHO Port is in use && EXIT 1

Resources