How can I pipe "Y" into netstat.exe to satisfy N/Y demand

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

How can I pipe "Y" into netstat.exe to satisfy N/Y demand

#1 Post by alan_b » 23 May 2010 15:51

This code causes netstat (standard Windows executable) to record outgoing network activity.
When Ctrl'C is entered then activity.txt is closed ready for viewing.
I want netstat to instantly close down - I do not want it running anymore
Unfortunately it will not go away without being given either Y or N

Code: Select all

ECHO XLOWTEST IS RUNNING
netstat -b 5 > activity.txt
ECHO XLOWTEST IS ENDING
PAUSE


When I close with Ctrl'C the response is
^CTerminate batch job (Y/N)?
to which I tried ugh, and then Z, then A then the valid N
After which netstat decided to close down and allow my script to say it was ending.

This is what the screen looked like

Code: Select all

C:\Utils\Sys_Test>ECHO XLOWTEST IS RUNNING
XLOWTEST IS RUNNING

C:\Utils\Sys_Test>netstat -b 5  1>activity.txt
^CTerminate batch job (Y/N)? ugh
Terminate batch job (Y/N)? Z
Terminate batch job (Y/N)? A
Terminate batch job (Y/N)? N

C:\Utils\Sys_Test>ECHO XLOWTEST IS ENDING
XLOWTEST IS ENDING

C:\Utils\Sys_Test>PAUSE
Press any key to continue . . .


I would like to give Netstat a valid Y or N by piping that character from the script,
so the ONLY keyboard action needed is the Ctrl'C

The DOS command TIME will show the current time
and then offer the opportunity to change the time
and it requires a keyboard entry before it goes away.

This is a clever trick that simulates keyboard entry :-
ECHO. | MORE | TIME

I was hoping for a similar trick with Netstat.

Help

Regards
Alan

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How can I pipe "Y" into netstat.exe to satisfy N/Y deman

#2 Post by aGerman » 23 May 2010 17:20

Alan

I'm virtually certain there is no trick to pipe y/n after ctrl c.
But you could run netstat in a second (minimized ?!) window. Using TASKKILL it should be easy to close it.
Give that a try:-

Code: Select all

@echo off

ECHO XLOWTEST IS RUNNING

start "NetstatLog" /min cmd /c netstat -b 5 ^>activity.txt
pause>nul|set /p "=Press any key to quit netstat logging ... "
taskkill /im netstat.exe /f >nul

ECHO XLOWTEST IS ENDING
PAUSE


Regards
aGerman

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How can I pipe "Y" into netstat.exe to satisfy N/Y deman

#3 Post by alan_b » 25 May 2010 12:37

I have spent the last two days fighting the Internet.
For a week a small percentage of high uptime web-sites have been missing and not responding to pings.
Two days ago the Internet disintegrated, and had difficulty logging in to Gmail, and when I was in and clicked on Calendar that was down.
70% of all sites were down 70% of the time.
There were no 404 error messages - the DNS just failed to resolve names into IP addresses.
I vaguely remembered forthcoming DNS doom, and eventually found
"DNSSEC unlikely to break Internet on May 5" - an article on
http://blogs.techrepublic.com.com/itdojo/?p=1713

Utter desperation after all else failed,
I abandoned my Router's default of using DNS servers chosen by my ISP,
and tried my son's preference -
immediately every-one was on-line again. ! ! !

Hence my unavoidable delay in getting back.

Thanks, but I had no success.
But you gave me an idea after struggling.

This one line does the job

Code: Select all

start "USE Ctrl'C to terminate NETSTAT Logging and close this Window" cmd /c netstat -b 5 ^>activity.txt

This script simply launches a new CHILD instance of CMD.EXE to support NetStat, and terminates.
I avoid the /min option, so the CHILD has focus and is able to receive the Ctrl'C from the keyboard and immediately and cleanly close down
I do not know of a way to put a "USE Ctrl'C to terminate ..." message WITHIN the CMD Window,
but consider the Title bar to be almost as good a place to put it.

The following is my summary of failure and what went wrong should any-one be interested.
It was only as I previewed and went to SUBMIT that I realised Ctrl'C might get into netstat if it was not minimized - then single line success.

Regards and Thanks
Alan

test/debug script NETSTOP.BAT

Code: Select all

@echo off

ECHO XLOWTEST IS RUNNING

start "NetstatLog" cmd /c netstat -b 5 ^>activity.txt
pause>nul|set /p "=Press any key to quit netstat logging ... "
echo %TIME%
goto :EOF
pause
echo %TIME%
ECHO now wait until a list of tsks is presented
tasklist
echo %TIME%
echo o.k. to proceed and see if "tskill netstat.*" will work
pause
echo %TIME%
tskill netstat.*
echo %TIME%

ECHO XLOWTEST IS ENDING
PAUSE


When I double click this Windows launches a "MOTHER" instance of CMD.EXE to run it,
and the script then starts "CHILD" instance of CMD.EXE to execute netstat.
MOTHER then issues time stamps and requests "wait until a list of tasks is presented",
then it lists the task that are running.
After another time stamp and continue it follows with
17:42:47.50
and then it FAILS to kill netstat.* with the error message
"Could not find process: netstat.*"
17:42:48.64
It took 1.14 seconds trying, and failing to kill the process.

FACT 1
MOTHER never lost focus, i.e. CHILD and grandchild netstat.exe never saw any Ctrl'C that could have stopped the logging of activity by netstat.exe

FACT 2
MOTHER can use Tasklist and see her CHILD cmd.exe, and also see the grandchild netstat.exe,
but MOTHER is unable to kill her grandchild whilst it is still logging -
good granny - but why pretend it could not find it when it has already listed it ! !

It looked as if netstat.exe logging at 5 second intervals caused a sort of "file in use" objection by Windows to the killing of that task, until

FACT 3
UNCLE CMD.EXE, separately launched, and no "family blood ties" to netstat, has no problem killing netstat.exe whilst it is still logging.

If I remove the option "/min" the the CHILD would gain focus, and Ctrl'C would stop the logging

Below is what appeared on the NETSTOP.BAT window up to the final PAUSE and shutdow,
but with 2 dozen tasks omitted for the sake of brevity - something I am not noted for ! !

XLOWTEST IS RUNNING
Press any key to quit netstat logging ... 17:42:19.57
Press any key to continue . . .
17:42:35.88
now wait until a list of tasks is presented

Image Name PID Session Name Session# Mem Usage
========================= ====== ================ ======== ============
System Idle Process 0 Console 0 16 K

cmd.exe 3076 Console 0 1,124 K
svchost.exe 3220 Console 0 4,816 K
taskmgr.exe 2664 Console 0 2,160 K
wmiprvse.exe 2976 Console 0 6,488 K
cmd.exe 732 Console 0 2,520 K
cmd.exe 2072 Console 0 3,428 K
netstat.exe 1840 Console 0 4,204 K
tasklist.exe 3276 Console 0 4,988 K
17:42:38.24
o.k. to proceed and see if "tskill netstat.*" will work
Press any key to continue . . .
17:42:47.50
Could not find process: netstat.*
17:42:48.64
XLOWTEST IS ENDING
Press any key to continue . . .

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How can I pipe "Y" into netstat.exe to satisfy N/Y deman

#4 Post by aGerman » 25 May 2010 14:10

Alan,

thanks for your response and detailed explanation. It was verry interresting for me.

Well, to be honest, I tried my batch file only without redirection to activity.txt and that worked fine. I have no idea why it does not work if you want to redirect the stdOut.

Referring to tskill. I used the taskkill command and this killed the netstat.exe process. I remember that tskill was used without any file extension or wildcard.

Code: Select all

tskill netstat


But, how ever. Your solution is much easier and works great.

Regards
aGerman

CitizenRon
Posts: 1
Joined: 25 May 2010 14:46

Re: How can I pipe "Y" into netstat.exe to satisfy N/Y deman

#5 Post by CitizenRon » 25 May 2010 17:05

Glad to see you got your problem solved. Here's some info on why your original idea wouldn't work.
alan_b wrote:When I close with Ctrl'C the response is
^CTerminate batch job (Y/N)?
to which I tried ugh, and then Z, then A then the valid N
After which netstat decided to close down and allow my script to say it was ending.
Technically, NETSTAT closed down the instant you hit Ctrl+C. When you use "Ctrl+C" or "Ctrl+Break" in a command window, it is not treated as keyboard input but a signal that is sent to the command window and everything attached to the command window. When you used Ctrl+C in your command window, it sent the interrupt signal to NETSTAT.EXE as well as the CMD.EXE console that launched NETSTAT which is why your console prompted you to terminate the batch job but still continued the rest of the batch file when you passed it an "N." NETSTAT was stopped with the Ctrl+C and CMD.EXE was interrupted with the same Ctrl+C but you allowed it to continue by selecting "N."

alan_b wrote:I would like to give Netstat a valid Y or N by piping that character from the script,
so the ONLY keyboard action needed is the Ctrl'C
Technically, you were not sending the "Y" or "N" to NETSTAT but instead to CMD.EXE which was running the batch file. The only thing you could do from within the script is what you guys figured out. Kill the NETSTAT process with TASKKILL since it would never self-terminate without outside input when using an interval.

alan_b wrote:The DOS command TIME will show the current time
and then offer the opportunity to change the time
and it requires a keyboard entry before it goes away.

This is a clever trick that simulates keyboard entry :-
ECHO. | MORE | TIME

I was hoping for a similar trick with Netstat.
The reason that works is that you are redirecting keyboard input to the TIME command. SIGINT and SIGBREAK (Ctrl+C and Ctrl+Break) aren't interpreted as keyboard input so cannot be redirected, only simulated from outside the console, such as with the SendKeys method of the WScript.Shell. At least as far as I know.

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How can I pipe "Y" into netstat.exe to satisfy N/Y deman

#6 Post by alan_b » 26 May 2010 02:29

Thank you for the additional info.
Knowledge of WHY I fail to achieve a goal gives me a feeling that I have not lost total control ! !

The next time I fail to get the desired result when using "Ctrl+C" or "Ctrl+Break",
I hope to remember this info and recognise I need a different mechanism.

I am happy that I came across this site.
Last year it became (and remains) my favourite bookmarked site when DOS fails to do what I tell it ! !

NB When I posted my script that used tskill instead of taskkill,
I intended to note (but forgot as I typed) that I use XP Home edition, hence tskill was all I had.

Regards
Alan

Post Reply