Sign in to follow this  
Followers 0
MachinistProgrammer

Autoit Command Line Interpreter!

4 posts in this topic

#1 ·  Posted (edited)

I don't know if it has been done before But Here is a CMD version of Autoit.

the following functions  don't work:
tcpaccept
tcpclosesocket
tcpconnect
tcplisten
tcpnametoip - however there is NSLookUp
tcprecv
tcpsend
tcpshutdown
tcpstartup
DllStructCreate
DllStructGetData
DllStructGetSize
DllStructGetSize
DllStructSetData
And anything that uses an Array
For further help go to www.autoitscript.com
the command line version of autoit dose have some extra commands such as
Repeat("command",times) - will Repeat any command
RunSelf() - open a new instance of Autoit Command line interpreter
Echo("text") - Prints text to console. however putting somthing as a string will do the same
About() - prints information about Autoit Command line interpreter
Clear() - Clears Autoit Command line interpreter
Dir(dir = $cd) - Lists a directory
Cd(dir) - sets the current directory (same as cmd''s cd)
CmdPad("comand"&$variable) - executes a CMD DOS command

 

 

source code now available

#NoTrayIcon
#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=..\..\..\..\Desktop\Autoit CMD.exe
#AutoIt3Wrapper_Res_Description=Command Line Interpreter for Autoir3
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#AutoIt3Wrapper_Res_LegalCopyright=Sycam Inc
#AutoIt3Wrapper_Res_requestedExecutionLevel=highestAvailable
#AutoIt3Wrapper_Run_AU3Check=n
#AutoIt3Wrapper_Run_Tidy=y
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
;#AutoCMD.au3#==================================
;File Name.......:AutoCMD.au3
;Creator.........:sean.campbell7
;Created with....:Created with ISN AutoIt Studio
;Version.........:0.95 BETA
;=============================================
#include <console.au3>
#include <array.au3>
#include <file.au3>
#include <string.au3>
Local $CharSet = "qwertyuiopasdfghjkzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890_"
Assign("$cd", @UserProfileDir, 2)
Cout('Welcome to the Autoit Command line interpreter' & @LF)
While 1
    _CMD()
WEnd

Func _CMD()
    Local $cmd
    Cout(">>")
    Cin($cmd)
    $exe = DoCmd($cmd)
    If $exe = '' Then
        $exe = "Null"
    ElseIf StringInStr($exe, "-ret") Then
        Return Cout(StringReplace($exe, "-ret", ''))
    EndIf
    Cout('Ret:' & $exe & @LF)
EndFunc   ;==>_CMD

Func DoCmd($cmd)
    If StringLeft($cmd, 1) = "$" Then
        $var = StringLeft($cmd, StringInStr($cmd, "=") - 1)
        $icmd = StringReplace(StringTrimLeft($cmd, StringInStr($cmd, "=")), " ", '')
        $exe = _Execute($icmd)
        Assign(StringReplace($var, ' ', ''), $exe, 2)
        Return $exe
    ElseIf $cmd = "exit" Then
        Exit
    Else
        Return _Execute($cmd)
    EndIf
EndFunc   ;==>DoCmd

Func About()
    $ret = "Name....: Autoit Console" & @LF & _
            "Creator.: Sycam Inc (sean Campbell)" & @LF & _
            "Version.: 1.1" & @LF & _
            "About...: Command line interpreter For AutoIt3-ret" & @LF
    Return $ret
EndFunc   ;==>About

Func _Execute($icmd)
    Local $cmd = $icmd
    Local $varpos[1][2]
    Local $vars[1]
    Local $str_3[1][2]
    $str_1 = _StringGetSub($cmd, '"')
    For $i = 0 To UBound($str_1) - 1
        If $str_1[$i] <> '' Then
            $rep = _StringRepeat(';', StringLen($str_1[$i]))
            $cmd = StringReplace($cmd, '"' & $str_1[$i] & '"', $rep)
            ReDim $str_3[UBound($str_3) + 1][2]
            $str_3[UBound($str_3) - 1][0] = $rep
            $str_3[UBound($str_3) - 1][1] = '"' & $str_1[$i] & '"'
        EndIf
    Next
    $str_2 = _StringGetSub($cmd, "'")
    For $i = 0 To UBound($str_2) - 1
        If $str_2[$i] <> '' Then
            $rep = _StringRepeat(';', StringLen($str_2[$i]))
            $cmd = StringReplace($cmd, "'" & $str_2[$i] & "'", $rep)
            ReDim $str_3[UBound($str_3) + 1][2]
            $str_3[UBound($str_3) - 1][0] = $rep
            $str_3[UBound($str_3) - 1][1] = "'" & $str_2[$i] & "'"
        EndIf
    Next
    For $i = 1 To _StringGetOccurances($cmd, "$")
        $chars = StringMid($cmd, StringInStr($cmd, "$", 0, $i) + 1)
        ReDim $vars[UBound($vars) + 1]
        $vars[UBound($vars) - 1] = "$"
        For $x = 1 To StringLen($chars)
            $chr = StringMid($chars, $x, 1)
            If StringInStr($CharSet, $chr) Then
                $vars[UBound($vars) - 1] &= $chr
            Else
                ExitLoop
            EndIf
        Next
    Next
    For $i = 1 To UBound($vars) - 1
        $cmd = StringReplace($cmd, $vars[$i], &"'" Eval($vars[$i]) & "'")
    Next
    Local $x = 0
    For $i = 1 To UBound($str_3) - 1
        Do
            $x += 1
            $cmd = StringReplace($cmd, $str_3[$i][0], $str_3[$i][1], 0, $x)
        Until $x >= UBound($str_3) - 1
    Next
    Return Execute($cmd)
EndFunc   ;==>_Execute

Func CmdPad($icmd)
    Local $cmd = StringReplace($icmd, "'", '"')
    Local $varpos[1][2]
    Local $vars[1]
    Local $str_3[1][2]
    $str_1 = _StringGetSub($cmd, '"')
    For $i = 0 To UBound($str_1) - 1
        If $str_1[$i] <> '' Then
            $rep = _StringRepeat(';', StringLen($str_1[$i]))
            $cmd = StringReplace($cmd, '"' & $str_1[$i] & '"', $rep)
            ReDim $str_3[UBound($str_3) + 1][2]
            $str_3[UBound($str_3) - 1][0] = $rep
            $str_3[UBound($str_3) - 1][1] = '"' & $str_1[$i] & '"'
        EndIf
    Next
    $str_2 = _StringGetSub($cmd, "'")
    For $i = 0 To UBound($str_2) - 1
        If $str_2[$i] <> '' Then
            $rep = _StringRepeat(';', StringLen($str_2[$i]))
            $cmd = StringReplace($cmd, "'" & $str_2[$i] & "'", $rep)
            ReDim $str_3[UBound($str_3) + 1][2]
            $str_3[UBound($str_3) - 1][0] = $rep
            $str_3[UBound($str_3) - 1][1] = "'" & $str_2[$i] & "'"
        EndIf
    Next
    For $i = 1 To _StringGetOccurances($cmd, "$")
        $chars = StringMid($cmd, StringInStr($cmd, "$", 0, $i) + 1)
        ReDim $vars[UBound($vars) + 1]
        $vars[UBound($vars) - 1] = "$"
        For $x = 1 To StringLen($chars)
            $chr = StringMid($chars, $x, 1)
            If StringInStr($CharSet, $chr) Then
                $vars[UBound($vars) - 1] &= $chr
            Else
                ExitLoop
            EndIf
        Next
    Next
    For $i = 1 To UBound($vars) - 1
        $cmd = StringReplace($cmd, $vars[$i], &"'" Eval($vars[$i]) & "'")
    Next
    Local $x = 0
    For $i = 1 To UBound($str_3) - 1
        Do
            $x += 1
            $cmd = StringReplace($cmd, $str_3[$i][0], $str_3[$i][1], 0, $x)
        Until $x = UBound($str_3) - 1
    Next
    system($cmd)
    Return "-ret"
EndFunc   ;==>CmdPad

Func CD($dir = '')
    If FileExists(Eval("$cd") & "\" & $dir) And StringInStr(FileGetAttrib(Eval("$cd") & "\" & $dir), "d") Then
        Assign("$cd", Eval("$cd") & "\" & $dir)
        Return Eval("$cd")
    ElseIf FileExists($dir) And StringInStr(FileGetAttrib($dir), "d") Then
        Assign("$cd", $dir)
        Return Eval("$cd")
    EndIf
    Return Eval('$cd')
EndFunc   ;==>CD

Func Dir($dir = 0, $filter = '*')
    If $dir = '0' Then
        $dir = Eval("$cd")
    EndIf

    $a = _FileListToArray($dir, $filter)
    Cout($dir & @LF)
    For $i = 1 To UBound($a) - 1
        Sleep(10)
        Cout(@TAB & "|- " & $a[$i] & @TAB & FileGetAttrib($dir & "\" & $a[$i]) & @LF)
    Next
    Return "-ret"
EndFunc   ;==>Dir

Func Repeat($Func, $times = 1)
    Local $tz
    For $i = 0 To $times - 1
        $tz &= DoCmd($Func) & @LF
    Next
    Return StringTrimRight($tz, 1) & @LF & "-ret";remove last line feed
EndFunc   ;==>Repeat

Func Echo($text)
    Cout($text & @LF)
    Return "-ret"
EndFunc   ;==>Echo


Func NSLookUp($ip)
    RunWait(@SystemDir & '\NSLookUp.exe "' & $ip & '"')
    Return ' '
EndFunc   ;==>NSLookUp

Func RunSelf()
    If @Compiled Then
        Return Run(@ScriptFullPath)
    Else
        Return Run(@AutoItExe & ' "' & @ScriptFullPath & '"')
    EndIf
EndFunc   ;==>RunSelf


Func _StringGetPos($string, $iStart, $iEnd)
    Return StringLeft(StringTrimLeft($string, $iStart), $iEnd)
EndFunc   ;==>_StringGetPos

Func _StringReplacePos($string, $iStart, $iEnd, $value)
    $st = StringMid($string, 0, $iStart)
    $en = StringMid($string, $iEnd)
    Return $st & $value & $en
EndFunc   ;==>_StringReplacePos

Func _StringGetSub($string, $iDelim = '"')
    Local $ret[1]
    Local $i = 1, $l
    Do
        $l = StringTrimLeft($string, StringInStr($string, $iDelim, 0, $i))
        $l = StringLeft($l, StringInStr($l, $iDelim) - 1)
        $i += 2
        ReDim $ret[UBound($ret) + 1]
        $ret[UBound($ret) - 1] = $l
    Until $i >= _StringGetOccurances($string, $iDelim)
    _ArrayDelete($ret, 0)
    Return $ret
EndFunc   ;==>_StringGetSub

Func _StringGetOccurances($string, $iDelim)
    $lo = StringSplit($string, $iDelim)
    Return $lo[0]
EndFunc   ;==>_StringGetOccurances

Func Help()
    $ret = 'This is the Autoit Command line interpreter it has most of the standart AutoIt3 commands.' & @LF & _
            "however the following functions  don't work:" & @LF & _
            'tcpaccept' & @LF & _
            'tcpclosesocket' & @LF & _
            'tcpconnect' & @LF & _
            'tcplisten' & @LF & _
            'tcpnametoip - however there is NSLookUp' & @LF & _
            'tcprecv' & @LF & _
            'tcpsend' & @LF & _
            'tcpshutdown' & @LF & _
            'tcpstartup' & @LF & _
            'DllStructCreate' & @LF & _
            'DllStructGetData' & @LF & _
            'DllStructGetSize' & @LF & _
            'DllStructGetSize' & @LF & _
            'DllStructSetData' & @LF & _
            'And anything that uses an Array' & @LF & _
            'For further help go to www.autoitscript.com' & @LF & _
            'the command line version of autoit dose have some extra commands such as' & @LF & _
            'Repeat("command",times) - will Repeat any command' & @LF & _
            'RunSelf() - open a new instance of Autoit Command line interpreter' & @LF & _
            'Echo("text") - Prints text to console. however putting somthing as a string will do the same' & @LF & _
            'About() - prints information about Autoit Command line interpreter' & @LF & _
            'Clear() - Clears Autoit Command line interpreter' & @LF & _
            'Dir(dir = $cd) - Lists a directory' & @LF & _
            'Cd(dir) - sets the current directory (same as cmd''s cd)' & @LF & _
            'CmdPad("comand"&$variable) - executes a CMD DOS command' & @LF & _
            'For further help go to www.autoitscript.com' & @LF & _
            "Autoit Command line interpreter is baced on Sycam Inc's _Execute function-ret" & @LF
    Return $ret
EndFunc   ;==>Help

Func Clear() ;clears termanal
    system("cls")
    Return "-ret"
EndFunc   ;==>Clear

i would add screen shots but the forums don't like embedded images from Google drive

Autoit CMD.zip

Edited by sycam0inc

All my projects live on github

Share this post


Link to post
Share on other sites



A very nice idea but I'm not sure I can test it without checking the source code,I'll be waiting, thank you.

Share this post


Link to post
Share on other sites

Added Source Code And Updated to Version 1.1

Chanellog

 

Added :

 - CD

 - Dir

 - RunSelf

 - echo

 - Clear

 - CmdPad

Removed :

 - Ping (Cmd Version)(Not Needed)


All my projects live on github

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • hcI
      By hcI
      Hello I would like to know if there is a way to return a sentence in cmd when I launch from it (because I add arguments).
      For example, diskpart.exe which help to manage the key and hdd connected, when you launch it with the parameter "/f" the app return a sentence saying that it don't recognize the parameter "/f" and it return the sentence in the cmd where i started the application, not a new one.
      That's what I want to do but I couldn't find anything that would solve my problem on internet and on AutoIt like ConsoleWrite / ConsoleWriteError (don't work).
       
      Thanks
    • Duck
      By Duck
      I'm attempting to capture the output from the command line tool PSEXEC. I'm using AutoIT to run an instance of PSEXEC against a remote PC to audit Local Admins in my environment using net.exe (C:\Windows\System32> net localgroup administrators). However the usual trick I use to capture command line output does not appear to work well with PSEXEC, as the bottom portion of the output is missing from the return. Any ideas or recommendations are greatly appreciated.  
       
      Here is what I'm working with: 
      ;This script will read from a list of hosts and report who has local admin privileges on the machine #RequireAdmin Global $fileName = @ScriptDir & '\test.txt' ;hostlist, one host per line readHostList() ;Read list of hosts Func readHostList() Local $file = FileOpen($fileName, 0) While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop ConsoleWrite($line & @CRLF) ;MsgBox(0,0,$line) getLocalAdmins($line) WEnd FileClose($file) EndFunc ;run PSEXEC to list local admins Func getLocalAdmins($remotePC) Local $testFile = @ScriptDir &'\test234.txt' FileOpen($testFile, 1) Local $psexec = 'psexec \\' & $remotePC & ' net localgroup administrators' FileWriteLine($testFile, _RunCmd($psexec) ) FileClose($testFile) EndFunc ;Used to return CLI output Func _RunCmd($sCommand) Local $nPid = Run(@Comspec & " /c" & $sCommand, @SystemDir, @SW_Hide, 8), $sRet = "" If @Error then Return "ERROR:" & @ERROR ProcessWait($nPid) While 1 $sRet &= StdoutRead($nPID) If @error Or (Not ProcessExists ($nPid)) Then ExitLoop WEnd Return $sRet EndFunc  
      ## If i manually run the command on the remote PC via PSEXEC I will get the following output: 
      PsExec v2.11 - Execute processes remotely
      Copyright (C) 2001-2014 Mark Russinovich
      Sysinternals - www.sysinternals.com
      Starting net on PCNAME... on PCNAME...
      net exited on PCNAME with error code 0.
      -------------------------------------------------------------------------------
      admin
      Administrator
      Alias name     administrators
      Domain\Domain Admins
      Comment        Administrators have complete and unrestricted access to the computer/domain
      Members
      The command completed successfully.
       
      ## The returned output from running the above script is as follows:
      PsExec v2.11 - Execute processes remotely
      Copyright (C) 2001-2014 Mark Russinovich
      Sysinternals - www.sysinternals.com
      Alias name     administrators
      Connecting to PCNAME...
      Starting PSEXESVC service on PCNAME...
      Connecting with PsExec service on PCName...
      Starting net on PCNAME..
      net exited on PCNAME with error code 0.
       
      **Note to test this script PSEXEC must be in the system dir or the path in the script changed 
      PSEXEC tool: https://docs.microsoft.com/en-us/sysinternals/downloads/psexec
    • luckyluke
      By luckyluke
      Hello,
      Im trying to read the output from CMD using Dllcall, here is my code:
      #include <WinAPI.au3> #include <array.au3> Global Const $STD_OUTPUT_HANDLE = -11 Global Const $_CONSOLE_SCREEN_BUFFER_INFO = _ "struct;int dwSizeX;" & _ "short dwSizeY;" & _ "short dwCursorPositionX;" & _ "short dwCursorPositionY;" & _ "short wAttributes;" & _ "short Left;" & _ "short Top;" & _ "short Right;" & _ "short Bottom;" & _ "short dwMaximumWindowSizeX;" & _ "short dwMaximumWindowSizeY;endstruct" $pCmd = Run( "cmd.exe" ) Sleep(1000) $hCmd = WinGetHandle("") ConsoleWrite('handle:' & $hCmd & @CRLF) $aRet = DllCall("kernel32.dll", "int", "AttachConsole", "dword", $pCmd) ;_ArrayDisplay($aRet) If $aRet[0] <> 0 Then $vHandle_data='' $vHandle='' $vHandle_data = DllStructCreate($_CONSOLE_SCREEN_BUFFER_INFO) ; Screen Buffer structure $aRet1 = DllCall("kernel32.dll", "hwnd", "GetStdHandle", "dword", $STD_OUTPUT_HANDLE) if not @error Then $vHandle = $aRet1[0] $aRet = DllCall("kernel32.dll", "int", "GetConsoleScreenBufferInfo", "hwnd", $vHandle, _ "ptr", $vHandle_data) MsgBox(0, '1',DllStructGetData($vHandle_data, 'dwSizeX') & _WinAPI_GetLastErrorMessage()) EndIf It did not work, i got the message 'The handle is invalid'. Please help?
      Thank you in advance!
    • mihaijulien
      By mihaijulien
      Hello,
      I compiled a script I made that takes a command line parameter (the version of a .msi installer) when launched. The script was compiled with the /console option. The script (.au3) works fine but the executable returns  the following error:  
      Error: array variable has incorrect number of subscripts or subscript dimension range exceeded  
    • fosil
      By fosil
      Hi everyone.

      I'm currently working a program that constantly prints out log files through "consolewrite" and the "#AutoIt3Wrapper_Change2CUI=y" wrapper.

      Part of this program requires me to run a batch script.

      My issue is the batch script launches from the same window as consolewrite. I need the batch file to be launched through a different window as currently this causes an issue with the logs (which need to be very precise) but also causes the batch file to produce some funny behavior...

      Does anyone know how I can force the file to run on a second DOS window?

      Thanks in advance!!!
       
      Edit: Im using the "run" command if that helps. I tried "shellexecute" but that seemed to not launch the batch scripts at all.