Jump to content

Console in AutoIT


 Share

Recommended Posts

Here's my autoit script that runs a telnet session:

; <COMPILER>
AutoItSetOption("WinTitleMatchMode", 4)
$sleep = 500
$answer = InputBox("Set PR1", "[L]ock, [U]unlock, [V]iew PR1?", "L", "", -1, -1, 0, 0)
If @Error = 1 Then
    Exit
EndIf
Select
    Case StringLower( StringLeft ( $answer , 1 ) ) = "l"
        $flag = "D"
    Case StringLower( StringLeft ( $answer , 1 ) ) = "u"
        $flag = "U"
    Case StringLower( StringLeft ( $answer , 1 ) ) = "v"
        $flag = "V"
    Case Else
        Exit
EndSelect


Run("CMD")
WinWaitActive("classname=ConsoleWindowClass")
Sleep($sleep);Wait for initial prompt
Send("telnet 192.168.177.19{ENTER}")
Sleep($sleep);Wait for initial prompt
Send("$username{ENTER}")
Sleep($sleep + 100)
Send("$password{ENTER}")
Sleep($sleep)
Send("enq -A{ENTER}")
If $flag = "V" Then
    Exit
EndIf
Sleep($sleep)
Send("enq -P pr1 -" & $flag & "{ENTER}")
Sleep($sleep)
Send("enq -A{ENTER}")

Two things I want to change:

Easy:

Change the prompt to use a group of radio buttons.

Possible?

Have the console output inside of a "window" within the Autoit GUI rather than a separate window.

Does the current version of AutoIt have this ability?

Thanks for any help.

Not as LeanNot as MeanBut still a MarineMan may have believed missed.

Link to comment
Share on other sites

Here's my autoit script that runs a telnet session:

; <COMPILER>
AutoItSetOption("WinTitleMatchMode", 4)
$sleep = 500
$answer = InputBox("Set PR1", "[L]ock, [U]unlock, [V]iew PR1?", "L", "", -1, -1, 0, 0)
If @Error = 1 Then
    Exit
EndIf
Select
    Case StringLower( StringLeft ( $answer , 1 ) ) = "l"
        $flag = "D"
    Case StringLower( StringLeft ( $answer , 1 ) ) = "u"
        $flag = "U"
    Case StringLower( StringLeft ( $answer , 1 ) ) = "v"
        $flag = "V"
    Case Else
        Exit
EndSelect


Run("CMD")
WinWaitActive("classname=ConsoleWindowClass")
Sleep($sleep);Wait for initial prompt
Send("telnet 192.168.177.19{ENTER}")
Sleep($sleep);Wait for initial prompt
Send("$username{ENTER}")
Sleep($sleep + 100)
Send("$password{ENTER}")
Sleep($sleep)
Send("enq -A{ENTER}")
If $flag = "V" Then
    Exit
EndIf
Sleep($sleep)
Send("enq -P pr1 -" & $flag & "{ENTER}")
Sleep($sleep)
Send("enq -A{ENTER}")

Two things I want to change:

Easy:

Change the prompt to use a group of radio buttons.

Possible?

Have the console output inside of a "window" within the Autoit GUI rather than a separate window.

Does the current version of AutoIt have this ability?

Thanks for any help.

Check this out: http://www.autoitscript.com/forum/index.php?showtopic=28088

It appears that you cannot do this with Microsoft telnet.

For your radio buttons, look up the GUIStartGroup function.

Hope this helps.

Link to comment
Share on other sites

Thanks Envoi, that looks like what I want, however, I'm just getting a blank screen.

Using the script at that link, I changed the option to show the telnet window rather than hide it, but it doesn't seem to be doing anything.

I pass the IP address for telnet, but it doesn't seem to be connecting, at least nothing is appearing in the screen - nothing is being returned to StdoutRead(), so nothing to process.

Any experience in using this console telnet?

Thanks.

Edited by SergeantCorps

Not as LeanNot as MeanBut still a MarineMan may have believed missed.

Link to comment
Share on other sites

Thanks Envoi, that looks like what I want, however, I'm just getting a blank screen.

Using the script at that link, I changed the option to show the telnet window rather than hide it, but it doesn't seem to be doing anything.

I pass the IP address for telnet, but it doesn't seem to be connecting, at least nothing is appearing in the screen - nothing is being returned to StdoutRead(), so nothing to process.

Any experience in using this console telnet?

Thanks.

It has been my experience that Telnet does not allow for interaction on the STDIN and STDOUT channels. I attempted to write a VBScript that used telnet for connectivity a while back and never could make it work by sending it STDIN messages. I ended-up having to use another method to telnet out because this wouldn't work. Because Telnet is a relatively basic protocol, I would suggest looking into creating a socket and just connecting directly yourself. You can packet sniff a telnet session with a functioning client to get the appropriate key mappings for what you're looking to do. It's crude, but it has worked for me in the past. Thanks!

jawnah--Have a blessed day :)

Link to comment
Share on other sites

.. or you can look for 3rd party software "Telnet-like".

Have a Google search for Active-X or OCX and Telnet - if you're lucky you might find a free one.

I was lucky to work for a company which choose to spend $200 to buy WodTelnet. :)

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

You can write your own telnet with 3 lines of code, no includes required.

TCPStartup

TCPConnect

TCPSend

Well... that seems simple enough... except I'm not too familiar with using those commands. Will I be able to capture the tcp session into an edit box for monitoring?

If you can provide some code or point to a thread or sample that would be much appreciated.

Not as LeanNot as MeanBut still a MarineMan may have believed missed.

Link to comment
Share on other sites

  • 6 months later...

I'm attacking this little project again.

Here's the code I'm using:

; A simple custom messagebox that uses the OnEvent mode
#include <NamedPipes.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GuiConstantsEx.au3>


Opt('MustDeclareVars', 1)
Opt("GUIOnEventMode", 1)
AutoItSetOption("WinTitleMatchMode", 4)

Global $ExitID
Global $ConnectedSocket = -1
Global $iMemo

_Main()

Func _Main()
    Local $hGUI, $LockID, $UnLockID, $ViewID

    $hGUI = GUICreate("PR1", 1000, 600,  -1, -1, $WS_SIZEBOX)

    GUICtrlCreateLabel("Sent Printer Status:", 10, 10)
    $LockID = GUICtrlCreateButton("Lock", 10, 50, 80, 30)
    GUICtrlSetOnEvent($LockID, "OnLock")
    $UnLockID = GUICtrlCreateButton("UnLock", 110, 50, 80, 30)
    GUICtrlSetOnEvent($UnlockID, "OnUnLock")
    $ViewID = GUICtrlCreateButton("View Status", 210, 50,80,30)
    GUICtrlSetOnEvent($ViewID, "OnView")
    $ExitID = GUICtrlCreateButton("Exit", 310, 50, 80, 30)
    GUICtrlSetOnEvent($ExitID, "OnExit")
    $iMemo = GUICtrlCreateEdit("", 0, 100, _WinAPI_GetClientWidth($hGUI), 470, $WS_DISABLED)

    GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit")

    GUISetState()  ; display the GUI

    While 1
        Sleep(1000)
    WEnd
EndFunc   ;==>_Main

;--------------- Functions ---------------
Func OnLock()
    RunTelnet("L")
EndFunc   ;==>OnLock

Func OnUnLock()
    RunTelnet("U")
EndFunc   ;==>OnUnLock

Func OnView()
    SendCommand("View Status", False )
    RunTelnet("V")
EndFunc   ;==>OnView

Func OnExit()
    Exit
EndFunc   ;==>OnExit

Func RunTelnet($flag)
    Local $sleep = 500

    IF ConnectToTCP() = False Then
        DisconnectFromTCP()
    EndIf

    SendCommand("enq -A")
    SendCommand("exit")

    DisconnectFromTCP()
Return

EndFunc   ;==>RunTelnet

Func ConnectToTCP()
    Local Const $szIPADDRESS="#.#.#.#"
    Local Const $szUserName="username"
    Local Const $szPASSWORD="password"
    SendCommand("Connecting", False)
    TCPStartup()
    $ConnectedSocket = TCPConnect($szIPADDRESS , 23)
    If @error Then
        SendCommand("Error - TCPConnect failed with WSA error: " & @error, False)
        Return False
    EndIf
    If $ConnectedSocket = -1 Then 
        SendCommand("Error - Unable to connect.", False)
        Return False
    EndIf

    SendCommand($szUserName)
    SendCommand($szPASSWORD)
    return True

EndFunc

Func SendCommand($msg, $hide=True)
    Dim $recv, $ret
    If $hide = False Then
        GUICtrlSetData($iMemo, GUICtrlRead($iMemo) & " > " & $msg & @CRLF )
    Else
        $ret = TCPSend( $ConnectedSocket , $msg)
        If @error Then GUICtrlSetData($iMemo, GUICtrlRead($iMemo) & " > " & "ERROR SENDING" & @CRLF )
        $recv = TCPRecv( $ConnectedSocket, 6144)
        GUICtrlSetData($iMemo, GUICtrlRead($iMemo) & " > " & $recv & @CRLF )
    EndIf
EndFunc

func DisconnectFromTCP()
    SendCommand("Disconnecting", False)
    TCPCloseSocket( $ConnectedSocket )
    TCPShutdown()
endfunc

It's connecting to an AIX box (UNIX) and it's establishing a connection, ie, no errors from the TCPConnect() command, but I'm not getting any response or text back from the connection. About the only thing I do get back is garbled text: ÿþ%ÿý

Is there anything I'm missing?

I hate to have to go back to using a console window as I did in my first post.

Thanks for any insight.

Not as LeanNot as MeanBut still a MarineMan may have believed missed.

Link to comment
Share on other sites

Since this was never an interactive routine, I found an acceptable way to approach this.

All I really ever needed was a way to view the status of the telnet session.

So I did some searching on embedding the Console within my main window.

I found the code I needed here.

Here's what my finished code looks like.

#include <GUIConstants.au3>
#include <Constants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GuiConstantsEx.au3>
#include <ButtonConstants.au3>


Opt('MustDeclareVars', 1)
Opt("GUIOnEventMode", 1)
AutoItSetOption("WinTitleMatchMode", 4)

Global $ExitID
Global $hGUI
Global $hw1
Global $origParent
Global Const $class="[CLASS:ConsoleWindowClass]"

_Main()

Func _Main()
    Local $LockID, $UnLockID, $ViewID, $iMemo

    $hGUI = GUICreate("COINS PR1", 700, 900,  -1, -1, $WS_SIZEBOX)

    GUICtrlCreateLabel("Sent Printer Status:", 10, 10)
    $LockID = GUICtrlCreateButton("Lock", 10, 50, 80, 30)
    GUICtrlSetOnEvent($LockID, "OnLock")
    $UnLockID = GUICtrlCreateButton("UnLock", 210, 50, 80, 30)
    GUICtrlSetOnEvent($UnlockID, "OnUnLock")
    $ViewID = GUICtrlCreateButton("View Status", 410, 50,80,30)
    GUICtrlSetOnEvent($ViewID, "OnView")
    $ExitID = GUICtrlCreateButton("Exit", 610, 50, 80, 30, $BS_DEFPUSHBUTTON )
    GUICtrlSetOnEvent($ExitID, "OnExit")

    GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit")

    GUISetState()  ; display the GUI
    GUIRegisterMsg(0xF, "WM_PAINT")

    While 1
        Sleep(1000)
    WEnd
EndFunc   ;==>_Main

;--------------- Functions ---------------
Func OnLock()
    RunTelnet("L")
EndFunc   ;==>OnLock

Func OnUnLock()
    RunTelnet("U")
EndFunc   ;==>OnUnLock

Func OnView()
    RunTelnet("V")
EndFunc   ;==>OnView

Func OnExit()
    if $hw1 <> "" Then
        Winkill($hw1)
    EndIf
    Exit
EndFunc   ;==>OnExit

Func RunTelnet($flag)
    Local $cmdtitle, $embedWindow, $pid
    Local $sleep = 500
    Local Const $szIPADDRESS="#.#.#.#"
    Local Const $szUserName="user"
    Local Const $szPASSWORD="pass"

    if $hw1 <> "" Then
        Winkill($hw1)
    endif
    ; create a borderless window that is a child to the main window
    $embedWindow = GUICREATE("", 700, 800, 15, 90, $WS_POPUP, -1, $hGUI)
    DllCall("user32.dll", "hwnd", "SetParent", "hwnd", $embedWindow, "hwnd", $hGUI)


    ; launch the command prompt with a unique distinguishable title (without the operating system message)
    $cmdTitle = CHR(01) & CHR(02)
    $pid = run('cmd.exe /c start "' & $cmdTitle &'" /MIN cmd.exe /k',@ScriptDir,@SW_HIDE)
    WinWait($cmdTitle)

    ; get the handle of the cmd window to pass to SetParent; 
    $hw1 = WinGetHandle($cmdTitle)

    ; make the command prompt window a child to the earlier created borderless child window
    DllCall("user32.dll", "hwnd", "SetParent", "hwnd", $hw1, "hwnd", $embedWindow)
    ; resize the command prompt window so that its bolder and title bar are outside the borderless child window
    ;  giving the appearance of a borderless command prompt
    WinMove($hw1, '', -4, -30, 700, 800)
    ;WinSetState($hWndChild, '', @SW_SHOW) ; no longer need this line
    WinSetState($embedWindow, '', @SW_SHOW)
    ;WinActivate($hw1) ; Added this line to make sure the embedded command prompt gets focus

    Sleep($sleep) ;Wait for initial prompt
    ControlSend($hw1, $cmdTitle, "", "telnet " & $szIPADDRESS & "{ENTER}")
    Sleep($sleep) ;Wait for initial prompt
    ControlSend($hw1, $cmdTitle, "", $szUserName & "{ENTER}")
    Sleep($sleep + 100)
    ControlSend($hw1, $cmdTitle, "", $szPASSWORD & "{ENTER}")
    Sleep($sleep)
    ControlSend($hw1, $cmdTitle, "", "enq -A{ENTER}")
    If $flag <> "V" Then
        Sleep($sleep)
        ControlSend($hw1, $cmdTitle, "", "enq -P pr1 -" & $flag & "{ENTER}")
        Sleep($sleep)
        ControlSend($hw1, $cmdTitle, "", "enq -A{ENTER}")
    EndIf
    Sleep($sleep)
    ControlSend($hw1, $cmdTitle, "", "exit{ENTER}")
    Sleep($sleep)
    ControlSend($hw1, $cmdTitle, "", "{ENTER}")

EndFunc   ;==>RunTelnet

Func WM_PAINT($hWnd, $Msg, $wParam, $lParam)
    Sleep(100)
    DllCall("user32.dll", "int", "InvalidateRect", "hwnd", $hWnd, "ptr", 0, "int", 0)
EndFunc  ;==>WM_PAINT

This provided a much cleaner interface without the user having to worry about closing the console window. Also, using ControlSend should keep the commands limited to the actual console and not be sent to the active window.

Thanks goes to all those programmers for providing the code needed for this!

Edited by SergeantCorps

Not as LeanNot as MeanBut still a MarineMan may have believed missed.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...