CliSAK class - 2020/11/29 - a122

Share the finished AutoHotkey v2 Scripts and libraries you made here. Please put the current version of AutoHotkey v2 you used in Square Brackets at the start of the topic title.
User avatar
TheArkive
Posts: 403
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

CliSAK class - 2020/11/29 - a122

31 Jul 2020, 06:17

Cli - Swiss Army Knife v4 (CliSAK)


AHK v1 version/discussion moved to this thread.

Usage:
  • For one-time data collection: var := CliData(inCommand)
  • For streaming collection: obj := cli.new(inCommand, options := "", environment := "cmd", params := "/Q")
The "wget example":
Image

Please see example script for detailed examples, and the main class script for detailed docs.

Features:
  • New wrapper function CliData(inCommand) for easy inline one-time data collection from a program.
  • Easy collection of streaming output with callback functions, can handle multiple CLI sessions simultaneously
  • Creates a hidden console window so that CTRL+C and CTRL+Break can still be passed to halt execution with hotkey, button, etc.
  • Interactive CLI mode in the background, write to StdIn directly.
  • Capture StdErr separately, if desired.
  • Pass dynamically created multi-line batch commands executed in succession.
  • Define a callback for when a command returns and the prompt is displayed again.
  • Define a "Quit String" to automatically halt execution, clean up processes and handles, and trigger a callback.
  • Capture console animations like incrementing percent, or progress bar.
The following CLI environment prompts (shells) can be detected:
  • Windows batch
  • netsh
  • Android ADB
  • SSH
If you would like support for a new shell prompt, post a msg with details so I can research and add the new environment.

=======================================================================
UPDATES:
=======================================================================

:arrow: Download on GitHub

Huge thanks to the following users:
@segalion , @Sweeet, @sean , @maraksan_user, @SKAN , @HotKeyIt , @maz_1, @lexikos , @TheGood, @just me, @joedf
Details of their contributions are in the comments of the CLI class library. They have done a LOT. Without their contributions, I would not have gained the knowledge or the means to make this.

I'm happy to hear any commets/critiques to make this library more useful, fast, and memory-efficient.
Last edited by TheArkive on 01 Dec 2020, 11:30, edited 12 times in total.
User avatar
joedf
Posts: 7833
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: CliSAK class - 2020/07/31 - a119

06 Aug 2020, 16:52

Neat-o! :+1:
User avatar
TheArkive
Posts: 403
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Re: CliSAK class - 2020/07/31 - a119

07 Aug 2020, 13:30

@joedf
thanks for the encouragement man :) ... if you do any in-depth testing and find any issues please let me know
burque505
Posts: 1415
Joined: 22 Jan 2017, 19:37

Re: CliSAK class - 2020/07/31 - a119

07 Aug 2020, 13:46

:D Better and better!
The examples are really helpful.
Regards,
burque505
User avatar
TheArkive
Posts: 403
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Re: CliSAK class - 2020/07/31 - a119

07 Aug 2020, 13:55

Thanks @burque505
Fortunately not much as changed with all the alpha updates (that i know of) ... i haven't managed to test much more than the examples lately. I have some bigger projects that will put this library to the test.
User avatar
TheArkive
Posts: 403
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Re: CliSAK class - 2020/09/28 - a122

28 Sep 2020, 07:43

Update:2020/09/28
  • Improved consistency of operation across modes, optimized code
  • Added QuitStringCallback() for user defined quit string.
  • Fixed handling of ADB environment. Prompt consistently shown after command execution.
  • Finished documentation comments (top of the script).
  • Added ConsoleSend() method for proper usage of StdIn using mode "m" (thanks @lexikos!)
  • Added GetLastLine(str) method for usage in callbacks to filter output.
User avatar
TheArkive
Posts: 403
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Re: CliSAK class - 2020/11/29 - a122

29 Nov 2020, 07:45

Update:2020/11/29
  • Fixed potential security vulernability using mode "m" (WriteConsoleInput) - now StdIn is secure, and it can only communicate with the script, much more difficult to hijack.
  • Removed .ConsoleSend() method, because this contributed to a less secure background CLI session.
  • Added parameters to separate the environment from the commands, when calling cli.New().
  • Pure PowerShell now works with CliSAK.
    - Known issue: multi-line commands not currently possible.
    - Once you enter multi-line command mode you can't get out of it unless you terminate the session.
    - Multi-line commands might be possible in mode "m" but still with some limitations.
  • Default environment is CMD window with /Q for ECHO OFF.
  • Removed most of the modes - now this cli class is "streaming only".
  • Updated CliData() wrapper function for one-time easy "run and collect". This is the equivalent of the old "w" (wait) mode.
  • Now using a callback only requires that the callback function exists.
  • Improved timing and execution of PromptCallback() - StdErr and StdOut easily line up within this callback function.
  • Added CliObj.ready property to indicate when the environment is finished loading and the first prompt is displayed.
    - Best used in PromptCallback().
  • Added CliObj.BatchProgress property so the user can check which command in a multi-line set of commands is finished executing.
    - Best used in PromptCallback().
  • Mode "m" now allows StdErr to be a separate stream (using mode "x").
    - This can cause unwanted side effects with some programs, ie. the program may appear to hang.
  • Updated and simplified examples.
  • Added example GIF of wget Example #8.
CappuccinoCake
Posts: 3
Joined: 01 Dec 2020, 02:01

Re: CliSAK class - 2020/11/29 - a122

01 Dec 2020, 02:13

I have a question, i have to made a telnet session to a device, how i can get it to work with your class? If i insert "telnet" into the environment param i get the command line input field as a multi line input so i cannot send the commands to the console. And it also does not load the telnet cleint. I installed the telnet via windows features. It would be nice if somebody could help me out.

Kind reguards
User avatar
TheArkive
Posts: 403
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Re: CliSAK class - 2020/11/29 - a122

01 Dec 2020, 05:49

@CappuccinoCake
please post your script so I can see what you are working with. How you use the PromptCallback() and StdOutCallback() functions is critical to making this work.

I'll also do some testing on my end.
User avatar
TheArkive
Posts: 403
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Re: CliSAK class - 2020/11/29 - a122

01 Dec 2020, 06:20

@CappuccinoCake
I have seen something like this before with ssh.exe on Windows 10, but you actually get an error message. I'm afraid I'll have to do more testing.

I tried using mode "m", and i saw that telnet does load, but immediately exits. There are strange side effects with some command line programs when redirecting stdout/stdin away from the console.

What kind of automation are you trying to accomplish? Maybe there is another way?

I have done more testing with putty/plink. That works quite well with CliSAK.

Please let me know if you find a success method. I may need to modify some settings when using CreateProcess DllCall().

I might actually add a mode for redirecting to file, so that the same interface (this script) can be used. I've tried file redirect with ssh.exe on Win10 and it does work, so that may be the solution for telnet (I've been trying to avoid using a file as much as possible).
User avatar
TheArkive
Posts: 403
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Re: CliSAK class - 2020/11/29 - a122

01 Dec 2020, 11:13

This post on another forum is related to exactly what we are experiencing: http://www.delphigroups.info/3/1/116676.html

Either I need to use a specific security descriptor or this isn't meant to work without a console. In which case, redirecting to a file would probably be the only option, but then your harddrive is constantly churning as the script keeps checking the file... definitely not ideal.

Or you can check out my socket script class, and write a telnet script. Main problem though, it's just sockets, no encryption.

EDIT:

Or you can use a modified version of mode "m", which i removed because it is a bit unsecure and could be hijacked by another application. The AHK v1 version currently uses this other method to execute mode "m". Basically it a typical hidden console that you can scrape, and send keystrokes to. But still not ideal because for decent performance you need to shrink the console size, which also reduces your ability to scroll back.

I think for the time being, I'm just going to take off telnet as a capability since I have found a few posts already of people being unable to use telnet with CreateProcess. I had added it as a capability only because i added the "prompt recognition" so it could be used with CliSAK. Never thought it would actually fail though.

Sorry to say, I think telnet and CliSAK won't be getting along any time soon.

EDIT2:

@CappuccinoCake
I might dabble with making my own telnet script using sockets. If i get one working I'll post it in the AHK v2 Scripts forum.
CappuccinoCake
Posts: 3
Joined: 01 Dec 2020, 02:01

Re: CliSAK class - 2020/11/29 - a122

04 Dec 2020, 07:29

Sorry, i was not respomnding here because I had to do other stuff. I changed the line where you are sending multiple command to the normal telnet command only and the console isn't showing anithing...

Code: Select all

If (IsObject(c))
		c.close(), c:=""
	oGui["CmdOutput"].Value := ""
	
	batch := "ECHO. & telnet`r`n"
	
	; Mode "r" removes prompt and command from data, so only output is visible.
	c := cli.New(batch,"mode:|ID:Console_Streaming") ; Only using StdOutCallback()
I am not sure if this is the correct way.

Kind reguards.
User avatar
TheArkive
Posts: 403
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Re: CliSAK class - 2020/11/29 - a122

04 Dec 2020, 13:59

@CappuccinoCake
Please read my previous post more carefully, as well as the comments in the main class script, specifically the option showWindow. Normally the console screen will be blank, because StdOut / StdIn / StdErr are redirected.

I'm afraid I mentioned Telnet as a possibility prematurely (I apologize for this). Other scripters / programmers have run into issues trying to run telnet and redirecting std handles. It would be more effective to rewrite Telnet in AHK using the socket class. Again, please read my last post. All is explained there.

EDIT:

I'm am planning to attempt to write a Telnet script that will recreate the protocol of the Telnet program, but I can't give a proposed ETA at this time.
CappuccinoCake
Posts: 3
Joined: 01 Dec 2020, 02:01

Re: CliSAK class - 2020/11/29 - a122

10 Dec 2020, 04:35

I tried it now with the socket class, with this it worked fine. Thank you for the help.
User avatar
TheArkive
Posts: 403
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Re: CliSAK class - 2020/11/29 - a122

10 Dec 2020, 04:41

Glad you got it working :)

Return to “AutoHotkey v2 Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 2 guests