Stealthily get cmd output without temp files or clipboard

Get help with using AutoHotkey and its commands and hotkeys
AmazinglyDumb

Stealthily get cmd output without temp files or clipboard

10 Feb 2015, 08:48

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?
User avatar
TLM
Posts: 1418
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: Stealthily get cmd output without temp files or clipboar

10 Feb 2015, 11:19

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 ).
for direct help, tweet at me: https://twitter.com/CW_DTech
wolf_II
Posts: 2685
Joined: 08 Feb 2015, 20:55

Re: Stealthily get cmd output without temp files or clipboar

10 Feb 2015, 14:43

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
garry
Posts: 1869
Joined: 22 Dec 2013, 12:50

Re: Stealthily get cmd output without temp files or clipboar

10 Feb 2015, 14:59

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
lexikos
Posts: 6501
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Stealthily get cmd output without temp files or clipboar

10 Feb 2015, 16:26

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.
User avatar
TLM
Posts: 1418
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: Stealthily get cmd output without temp files or clipboar

10 Feb 2015, 16:49

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
for direct help, tweet at me: https://twitter.com/CW_DTech
lexikos
Posts: 6501
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Stealthily get cmd output without temp files or clipboar

10 Feb 2015, 18:37

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.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: Stealthily get cmd output without temp files or clipboar

10 Feb 2015, 19:59

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
Guest

Re: Stealthily get cmd output without temp files or clipboar

12 Feb 2015, 00:42

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.
AmazinglyDumb

Re: Stealthily get cmd output without temp files or clipboar

12 Feb 2015, 02:01

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 :-)
AmazinglyDumb

Re: Stealthily get cmd output without temp files or clipboar

12 Feb 2015, 02:13

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?
lexikos
Posts: 6501
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Stealthily get cmd output without temp files or clipboar

12 Feb 2015, 02:53

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.
AmazinglyDumb

Re: Stealthily get cmd output without temp files or clipboar

12 Feb 2015, 04:13

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?
wolf_II
Posts: 2685
Joined: 08 Feb 2015, 20:55

Re: Stealthily get cmd output without temp files or clipboar

12 Feb 2015, 05:30

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.
AmazinglyDumb

Re: Stealthily get cmd output without temp files or clipboar

12 Feb 2015, 05:35

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.
AmazinglyDumb

Re: Stealthily get cmd output without temp files or clipboar

12 Feb 2015, 05:38

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 мсек
wolf_II
Posts: 2685
Joined: 08 Feb 2015, 20:55

Re: Stealthily get cmd output without temp files or clipboar

12 Feb 2015, 06:23

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. :oops:
AmazinglyDumb

Re: Stealthily get cmd output without temp files or clipboar

12 Feb 2015, 06:26

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.
wolf_II
Posts: 2685
Joined: 08 Feb 2015, 20:55

Re: Stealthily get cmd output without temp files or clipboar

12 Feb 2015, 06:37

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
AmazinglyDumb

Re: Stealthily get cmd output without temp files or clipboar

12 Feb 2015, 08:13

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.

Return to “Ask For Help”

Who is online

Users browsing this forum: aseiot, Bing [Bot], ethanross1a, flyingDman, Google [Bot] and 172 guests