Say youre writing a long Windows CMD script, something like an audit script thatll take a good 20-30 minutes to complete.
Now say the whole script is being redirected to a report file - as scripts get more complicated, I'm finding that almost everything Iwrite ends up doing this. Something like below (just to pick a random SEC579 example):
audit-esx.cmd servername userid password reportfile.html
If all goes well, you see *nothing* on your screen for the next 20+ minutes (unless youve got a good port of tee available) but if it gets stuck, it's going to be 20+ minutes, or likely longer, before you realize that your script is borked
What to do? What to do? - - Use STDERR !
As the script goes through, insert an echo for each test (or meaningful phase) in your script to STDERR:
echo Audit Check SomeMeaningfulName 2
or, if youve parameterized your script enough:
echo Check %CHK% 2
2 means send this to STDERR.
So, instead of a blank screen as the audit runs, the screen will be a show you useful info on it's progress:
C:\sans\sec579\auditaudit-vms esx01.sec579.com root Passw0rd esx01-audit-vms.html
Audit Check VMX01
Audit Check VMX02
Audit Check VMX10
Audit Check VMX11
Audit Check VMX12
Audit Check VMX20
Audit Check VMX21
Audit Check VMX22
and so on, until it's done
Another neat trick will allow you to echo to a file ANDto STDERR in windows. The example below will take the output of somecommand, echo it to STDERR(which you'll see on the screen), and also echo it to the file outputfile.txt
somecommand 2 outputfile.txt
In linux, I'd normally do this using tee as mentioned, mostly because I'm lazy. The problem in this case with using tee is that it goes to STDOUT, rather than to STDERR, so if you're using it in combination with other redirection, you may not get what you expect:
somecommand |tee outputfile.txt
To fix this, you might string your command serially with cat, but that means that you won't see the command output on STDERRuntil the command is completely finished, rather than in (more or less)real time.
somecommand cat outputfile.txt 2
To see everything at the same time, I'll still use tee, but we'll also use a temp file descriptor (3) and dump the STDOUToutput of tee to STDERR, as shown below
(somecommand | tee outfile.txt) 32
Ihope this was useful - if you've got a neat take on using STDERR, or STDIN or STDOUTfor that matter, in Windows (or *nix)scripts, by all means pass them along in our comment form !
===============
Rob VandenBrink
Metafore (c) SANS Internet Storm Center. http://isc.sans.edu Creative Commons Attribution-Noncommercial 3.0 United States License.
Now say the whole script is being redirected to a report file - as scripts get more complicated, I'm finding that almost everything Iwrite ends up doing this. Something like below (just to pick a random SEC579 example):
audit-esx.cmd servername userid password reportfile.html
If all goes well, you see *nothing* on your screen for the next 20+ minutes (unless youve got a good port of tee available) but if it gets stuck, it's going to be 20+ minutes, or likely longer, before you realize that your script is borked
What to do? What to do? - - Use STDERR !
As the script goes through, insert an echo for each test (or meaningful phase) in your script to STDERR:
echo Audit Check SomeMeaningfulName 2
or, if youve parameterized your script enough:
echo Check %CHK% 2
2 means send this to STDERR.
So, instead of a blank screen as the audit runs, the screen will be a show you useful info on it's progress:
C:\sans\sec579\auditaudit-vms esx01.sec579.com root Passw0rd esx01-audit-vms.html
Audit Check VMX01
Audit Check VMX02
Audit Check VMX10
Audit Check VMX11
Audit Check VMX12
Audit Check VMX20
Audit Check VMX21
Audit Check VMX22
and so on, until it's done
Another neat trick will allow you to echo to a file ANDto STDERR in windows. The example below will take the output of somecommand, echo it to STDERR(which you'll see on the screen), and also echo it to the file outputfile.txt
somecommand 2 outputfile.txt
In linux, I'd normally do this using tee as mentioned, mostly because I'm lazy. The problem in this case with using tee is that it goes to STDOUT, rather than to STDERR, so if you're using it in combination with other redirection, you may not get what you expect:
somecommand |tee outputfile.txt
To fix this, you might string your command serially with cat, but that means that you won't see the command output on STDERRuntil the command is completely finished, rather than in (more or less)real time.
somecommand cat outputfile.txt 2
To see everything at the same time, I'll still use tee, but we'll also use a temp file descriptor (3) and dump the STDOUToutput of tee to STDERR, as shown below
(somecommand | tee outfile.txt) 32
Ihope this was useful - if you've got a neat take on using STDERR, or STDIN or STDOUTfor that matter, in Windows (or *nix)scripts, by all means pass them along in our comment form !
===============
Rob VandenBrink
Metafore (c) SANS Internet Storm Center. http://isc.sans.edu Creative Commons Attribution-Noncommercial 3.0 United States License.