I've had a look at various tips (ss64.com Command Redirection) and tricks but couldn't find an answer to my question:
Question
Is it possible to pipe the command I am going to execute into the same redirected output I will be creating?
Example with netstat
Input command
C:\Users\memyselfandi> netstat -obna >C:\temp\netstat_with_programs.txtThe actual command would be: netstat -obna >C:\temp\netstat_with_programs.txt
Output (The text file netstat_with_programs.txt)
This is the actual content of the netstat_with_programs.txt file. (The command is basically documenting itself in the output file.)
netstat -obna >C:\temp\netstat_with_programs.txt
Active connections Proto Local Address Remoteaddress State PID TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 888 RpcSs [svchost.exe] TCP 0.0.0.0:2382 0.0.0.0:0 LISTENING 1396 [sqlbrowser.exe] TCP 0.0.0.0:3389 0.0.0.0:0 LISTENING 376 TermService [svchost.exe]
....Example with arp
Input command
C:\users\memyselfandi> arp -a >C:\temp\arp_output.txtThe actual command is: arp -a >C:\temp\arp_output.txt
Output (The contents of arp_output.txt)
This is the actual content of the arp_output.txt file. (The command is basically documenting itself in the output file.)
arp -a >C:\temp\arp_output.txt
Interface: 10.57.209.191 --- 0x5 Internet Address Physical Address Type 10.57.209.2 80-e0-1d-58-8a-50 dynamic 10.57.209.3 80-e0-1d-58-8b-88 dynamic 10.57.209.9 00-50-56-8d-91-fe dynamic 10.57.209.10 00-50-56-8d-91-fe dynamic 10.57.209.175 00-50-56-b5-44-16 dynamic 10.57.209.255 ff-ff-ff-ff-ff-ff static 224.0.0.22 01-00-5e-00-00-16 static 224.0.0.252 01-00-5e-00-00-fc static 230.0.0.1 01-00-5e-00-00-01 static 239.255.255.250 01-00-5e-7f-ff-fa static So basically I would be documenting the command I am executing in the output I am creating.
Based on the possible solutions provided by @barlop in the comments, I went ahead and executed both commands:
With ECHO
echo netstat -obna >C:\temp\netstat_with_programs.txt && netstat -obna >>C:\temp\netstat_with_programs.txt...this produced the following first line in the output file, which doesn't fully satisfy the requirements:
netstat -obna
....With %aaa% variable
set aaa=netstat -obna
echo (%aaa%>C:\temp\netstat_with_programs.txt) && (echo %aaa%|cmd)>>C:\temp\netstat_with_programs.txt...this produces the same output, which doesn't fully meet the requirements:
netstat -obna
... 8 2 Answers
This is impossible because the shell, which takes the command and executes it, has no idea of what the arguments do that it is passing to the command.
For example in our theoretical rot13write C:\foobar.txt, is P:\sbbone.gkg
rot13write -qevir "P" -svyr "sbbone" rkg ".gkg"That may tell rot13write to write to the drive P, the file foobar, and the extension txt. Or it may all be a joke and that path could have already been hard coded into the executable. You don't know, and neither does a shell.
So the shell can not echo to the file that a program is mysteriously writing to, because the shell doesn't know of that sufficiently; and, the program which does know how it is invoked is under no obligation to do anything with that data (like print the invocation command to the file it's outputting to). What you can do is
- Have the shell echo all its commands. Most shells support this.
- Have the program that you execute write to standard output (as it normally does) which the program will inherit from the shell that spawned it (this is how programs you call and the shell write to the same place -- the psuedo terminal)
- Redirect the shells standard output to destination file.
This will do everything you want except show the flag and output location in the command.
This looks like this in command prompt (I think)
cmd /c "netstat" > myOutput.txt | type myOutput.txtAnd it looks like this in PowerShell,
powershell -command "Set-PSDebug -Trace 1; netstat" | tee myOutput.txt If the problem is to display (or redirect to a file) the redirection symbol (>) with the ECHO command, then you just need to escape it. The escape symbol in this case would be ^:
ECHO netstat -obna ^>C:\temp\netstat_with_programs.txt >C:\temp\netstat_with_programs.txt
netstat -obna >>C:\temp\netstat_with_programs.txtThe second command in the above snippet is using >> instead of > because you want to add the output to the same file rather than to overwrite it.