Page 1 of 2
Stealthily get cmd output without temp files or clipboard
Posted: 10 Feb 2015, 08:48
by AmazinglyDumb
I need to run a cmd command and get it's output.
1. This needs to be done without writing the output to a file and then reading that file.
2. This needs to be done without writing using clipboard.
3. This needs to be done stealthily (in a meaning that no windows should appear while the script works out).
I've successfully managed to meet requirements 1. and 2., but don't know how to meet requirement 3.
Code: Select all
objShell := ComObjCreate("WScript.Shell")
; objShell.Visible := false ; Returns an error.
; objExec.Visible := false ; Doesn't affect anything.
objExec := objShell.Exec(ComSpec " /U /C ping google.com -n 1")
While, !objExec.StdOut.AtEndOfStream
result := objExec.StdOut.ReadAll()
MsgBox, % result
Another question is about codepage:
I'm using a localized windows OS and output sometimes contains non-latin chars. In that case msgbox shows them in the wrong charset (but in CMD I see non-latin chars normally). How to fix that?
Re: Stealthily get cmd output without temp files or clipboar
Posted: 10 Feb 2015, 11:19
by TLM
Unfortunately there is no hide feature in the Exec() method, only the Run() method.
The downside of using Run() is it saves stdout to a temp file ( which sucks ).
The only other trick that I've seen, is to hide the command window once it's launched,
( It briefly flashes on screen before being hidden ).
Re: Stealthily get cmd output without temp files or clipboar
Posted: 10 Feb 2015, 14:43
by wolf_II
AmazinglyDumb wrote:but in CMD I see non-latin chars normally
I have got german Win7. Ping outputs some "Umlaute". This works for me instead of MsgBox:
Code: Select all
Gui, Font, s14, Terminal
Gui, Add, Text, w800 h300, %result%
Gui, Show
Return
GuiClose:
ExitApp
I hope that helps you a bit,
Greetings
Re: Stealthily get cmd output without temp files or clipboar
Posted: 10 Feb 2015, 14:59
by garry
thank you Wolf_II for font terminal
found this
Code: Select all
;-------- http://www.autohotkey.com/board/topic/3489-cmdret-return-output-from-console-progs-dll-version/page-12 ---
DetectHiddenWindows,On
Run,%ComSpec% /k,,Hide UseErrorLevel,pid
if not ErrorLevel
{
while !WinExist("ahk_pid" pid)
Sleep,10
DllCall("AttachConsole","UInt",pid)
}
CMD=ping -n 10 8.8.8.8 ;- ping 10-times
objShell:=ComObjCreate("WScript.Shell")
objExec:=objShell.Exec(CMD)
Gui,1:-border
Gui,font,s14,Terminal
Gui,Add,Text,W480 H800 vText gButtonCancel,Testing internet connection`r`nPinging Google DNS: 10 times
Gui,Show,w580 H850
while,!objExec.StdOut.AtEndOfStream
{
GuiControlGet,Text
strStdOut:=objExec.StdOut.readline()
GuiControl,,Text,%Text%`r`n%strStdOut%
}
GuiControlGet,Text
GuiControl,,Text,%Text%`r`n`r`n%A_Tab%%A_Tab%%A_Space%%A_Space%Click me to Close..
Return
Buttoncancel:
exitapp
esc::exitapp
Re: Stealthily get cmd output without temp files or clipboar
Posted: 10 Feb 2015, 16:26
by lexikos
FYI, ping is not a cmd command; it's a standalone program. If you're actually using ping and not just as an example, I would suggest
SimplePing() or similar instead. No need for a console window or a separate process.
Also, the old StdoutToVar() function can run commands and return their output without showing a console window.
Re: Stealthily get cmd output without temp files or clipboar
Posted: 10 Feb 2015, 16:49
by TLM
lexikos wrote:Also, the old StdoutToVar() function can run commands and return their output without showing a console window.
I was going to suggest that one too but was worried about compatibility.
The last version I tried didn't work outside of ahk vanilla
Re: Stealthily get cmd output without temp files or clipboar
Posted: 10 Feb 2015, 18:37
by lexikos
That's why I didn't provide a link. Someone posted an updated version which is lacking stdin support. I have another version which supports stdin, but it isn't available for download.
At any rate, StdoutToVar() shows that it can be done without showing a window.
Re: Stealthily get cmd output without temp files or clipboar
Posted: 10 Feb 2015, 19:59
by Coco
StdOutToVar() or to hide console window:
Code: Select all
DetectHiddenWindows On
Run %ComSpec%,, Hide, pid
WinWait ahk_pid %pid%
DllCall("AttachConsole", "UInt", pid)
WshShell := ComObjCreate("Wscript.Shell")
exec := WshShell.Exec("ping www.google.com")
output := exec.StdOut.ReadAll()
MsgBox %output%
DllCall("FreeConsole")
Process Close, %pid%
return
Re: Stealthily get cmd output without temp files or clipboar
Posted: 12 Feb 2015, 00:42
by Guest
wolf_II wrote:AmazinglyDumb wrote:but in CMD I see non-latin chars normally
I have got german Win7. Ping outputs some "Umlaute". This works for me instead of MsgBox:
Thx for that trick, but I need that data not just shown in human-readable charset, but actually
saved so. Msgbox is used as an example. I need the result to be in a var so I could parse it.
Re: Stealthily get cmd output without temp files or clipboar
Posted: 12 Feb 2015, 02:01
by AmazinglyDumb
Previous message was written by me, so
the charset problem is still actual for me.
lexikos wrote:FYI, ping is not a cmd command; it's a standalone program. If you're actually using ping and not just as an example, I would suggest
SimplePing() or similar instead. No need for a console window or a separate process.
Ping is just an example. I need to run different cmd commands/programs.
lexikos wrote:Also, the old StdoutToVar() function can run commands and return their output without showing a console window.
That's why I didn't provide a link. Someone posted an updated version which is lacking stdin support. I have another version which supports stdin, but it isn't available for download.
A bit offtopic, but I'm curious what stdIn
Coco wrote:StdOutToVar() or to hide console window:
Code: Select all
DetectHiddenWindows On
Run %ComSpec%,, Hide, pid
WinWait ahk_pid %pid%
DllCall("AttachConsole", "UInt", pid)
WshShell := ComObjCreate("Wscript.Shell")
exec := WshShell.Exec("ping www.google.com")
output := exec.StdOut.ReadAll()
MsgBox %output%
DllCall("FreeConsole")
Process Close, %pid%
return
Coco, thanks a lot! That's completely awesome!
garry,
thanks for the idea to use button instead of msgbox as output
Re: Stealthily get cmd output without temp files or clipboar
Posted: 12 Feb 2015, 02:13
by AmazinglyDumb
AmazinglyDumb wrote:A bit offtopic, but I'm curious what stdIn
Sorry, accidentally posted the previous message before finishing it.
I know almost nothing about StdIn and you,
lexikos, have said that there's no support of it yet (in a script compatible with latest ahk_l).
What benefits would bring it's support? Judging by wikipedia one can redirect stdin and thus the only benefit I see is that I could write a script where user could interact with console through that script (and to do that I'd need redirecting stdin to the ahk script) like he used to interact with cmd.exe (with all the hotkeys like tab for auto-finishing dir/file names). Am I right?
Re: Stealthily get cmd output without temp files or clipboar
Posted: 12 Feb 2015, 02:53
by lexikos
Stdin is the reverse of stdout. It's like this:
Code: Select all
script ---stdin--> program
script <--stdout-- program
Stdout gives you the program's output, and stdin is how you would give the program input (other than via the command line). For example,
RunWaitMany() uses stdin to give commands to cmd.exe and
ExecScript() uses it to run AutoHotkey code without writing to a file.
Re: Stealthily get cmd output without temp files or clipboar
Posted: 12 Feb 2015, 04:13
by AmazinglyDumb
lexikos, thanks for that clarification.
So the only problem I have now is the output in wrong charset.
Could anyone, please, help me with that?
Re: Stealthily get cmd output without temp files or clipboar
Posted: 12 Feb 2015, 05:30
by wolf_II
AmazinglyDumb wrote:So the only problem I have now is the output in wrong charset.
Could anyone, please, help me with that?
Please, give an example.
Re: Stealthily get cmd output without temp files or clipboar
Posted: 12 Feb 2015, 05:35
by AmazinglyDumb
wolf_II wrote:Please, give an example.
The example is already given in this thread by Coco: any 'ping' results into something like that
Code: Select all
---------------------------
interact with cmd.ahk
---------------------------
ЋЎ¬Ґ Ї ЄҐв ¬Ё б google.com [173.194.32.133] б 32 Ў ©в ¬Ё ¤ ле:
ЋвўҐв ®в 173.194.32.133: зЁб«® Ў ©в=32 ўаҐ¬п=3¬б TTL=51
‘в вЁбвЁЄ Ping ¤«п 173.194.32.133:
Џ ЄҐв®ў: ®вЇа ў«Ґ® = 1, Ї®«г祮 = 1, Ї®вҐап® = 0
(0% Ї®вҐам)
ЏаЁЎ«Ё§ЁвҐ«м®Ґ ўаҐ¬п ЇаЁҐ¬ -ЇҐаҐ¤ зЁ ў ¬б:
ЊЁЁ¬ «м®Ґ = 3¬бҐЄ, Њ ЄбЁ¬ «м®Ґ = 3 ¬бҐЄ, ‘।ҐҐ = 3 ¬бҐЄ
---------------------------
ОК
---------------------------
If your OS locale is not english.
Re: Stealthily get cmd output without temp files or clipboar Topic is solved
Posted: 12 Feb 2015, 05:38
by AmazinglyDumb
p.s.: the above gibberish message should look like this:
Code: Select all
Обмен пакетами с google.com [173.194.32.133] с 32 байтами данных:
Ответ от 173.194.32.133: число байт=32 время=3мс TTL=51
Статистика Ping для 173.194.32.133:
Пакетов: отправлено = 1, получено = 1, потеряно = 0
(0% потерь)
Приблизительное время приема-передачи в мс:
Минимальное = 2мсек, Максимальное = 2 мсек, Среднее = 2 мсек
Re: Stealthily get cmd output without temp files or clipboar
Posted: 12 Feb 2015, 06:23
by wolf_II
Sorry if I am missing the point. But that gibberish output is still from MsgBox, right?
My Suggestion is: Don't use MsgBox, use Gui with font "Terminal"
Does that still not work?
Sorry if I am missing the point.
Re: Stealthily get cmd output without temp files or clipboar
Posted: 12 Feb 2015, 06:26
by AmazinglyDumb
wolf_II wrote:Sorry if I am missing the point. But that gibberish output is still from MsgBox, right?
Both yes. I could apply terminal font to the msgbox, but the variable that's used in msgbox will still contain text in gibberish and thus remains unable to be parsed.
Re: Stealthily get cmd output without temp files or clipboar
Posted: 12 Feb 2015, 06:37
by wolf_II
I could apply terminal font to the msgbox
No, you can't, That's the point here. Unless I still miss something...
Try the example from coco without MsgBox:
Code: Select all
DetectHiddenWindows On
Run %ComSpec%,, Hide, pid
WinWait ahk_pid %pid%
DllCall("AttachConsole", "UInt", pid)
WshShell := ComObjCreate("Wscript.Shell")
exec := WshShell.Exec("ping www.google.com")
output := exec.StdOut.ReadAll()
;---------------------------------- new lines from here
Gui, Font, s14, Terminal
Gui, Add, Text, w800 h300, %output%
Gui, Show
return
GuiClose:
GuiEscape:
;---------------------------------- to here
DllCall("FreeConsole")
Process Close, %pid%
ExitApp
Re: Stealthily get cmd output without temp files or clipboar
Posted: 12 Feb 2015, 08:13
by AmazinglyDumb
wolf_II wrote:I could apply terminal font to the msgbox
No, you can't, That's the point here. Unless I still miss something...
You are right, I didn't mean msgbox, but meant gui.
wolf_II wrote:Try the example from coco without MsgBox:
yeah, it works. But now try it with msgbox and try to make it human-readable.