Jump to content

Send keystrokes to minimized PuTTY session.


Recommended Posts

Currently I have a working script, but I'd like to add one last tweak to make it perfect so that it's usable while I work on other things.

I am trying to monitor and track the error count of a specific cable in our system. Our tracking system only takes a count every 15 minutes, and I need more specific data, so I'm creating a program that will use my remote terminal session with PuTTY to automatically send the command every 5 seconds. 

Global $WndHan, $SendCount, $SleepCount, $Paused
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "End")
HotKeySet("+!d", "PullInfo") ; Shift-Alt-d
Opt("WinTitleMatchMode", 2)
$SendCount = 0
$SleepCount = 5

While 1
    Sleep (500)
    ToolTip ("Waiting for Start-up command. Shift+Alt+D" & @LF & "Pause/Break to Pause" & @LF & "Escape to Exit" & @LF & "Created by Adam Rauch", 5, 5)
WEnd

Func PullInfo()
    WinWait ("Shawnee, OK - PuTTY", "")
    ToolTip ("Waiting for Window to Activate" & @LF & "Escape to Exit" & @LF & "Created by Adam Rauch", 5, 5)
    $WndHan = WinGetHandle("Shawnee, OK - PuTTY", "")
        If @error Then
            MsgBox(65552, "", "An error occurred when trying to retrieve the window handle of Notepad.")
            Exit
        Else
            ControlSend ($WndHan,"", "", "show int cable 5/0/14{ENTER}")
            $SendCount = $SendCount + 1
            Tooltip ("Window Handle is " & $WndHan & " - Sending command, Time #" & $SendCount & @LF & "Escape to Exit" & @LF & "Created by Adam Rauch", 5, 5)
            Call ("SleepCount")
        EndIf
EndFunc

Func TogglePause()
    $Paused = Not $Paused
    While $Paused
        Sleep(100)
        ToolTip("Script is Paused" & @LF & "Pause/Break to Resume" & @LF & "Escape to Exit" & @LF & "Created by Adam Rauch", 5, 5)
    WEnd
    ToolTip("")
EndFunc

Func End()
    Exit
EndFunc

Func SleepCount()
    If $SleepCount > 1 Then
        ToolTip ("Window Handle is " & $WndHan & " - Waiting " & $SleepCount & " seconds before sending next command." & @LF & "Have currently sent " & $SendCount & " commands." & @LF & "Escape to Exit" & @LF & "Created by Adam Rauch", 5, 5)
        $SleepCount = $SleepCount - 1
        Sleep (1000)
        Call ("SleepCount")
    ElseIf $SleepCount = 1 Then
        ToolTip ("Waiting " & $SleepCount & " seconds before sending next command." & @LF & "Have currently sent " & $SendCount & " commands." & @LF & "Escape to Exit" & @LF & "Created by Adam Rauch", 5, 5)
        Sleep (1000)
        $SleepCount = 5
        Call ("PullInfo")
    EndIf
EndFunc

This code works fine, the problem is not with that. I pull the window handle just fine, and the program attempts to send to the PuTTY window, but I think a feature of PuTTY is that it doesn't send commands without being the active window.

Anyone have prior experience with this program and know a workaround?

Link to comment
Share on other sites

I have experience with putty but i guess you have to use memory read to do that .... I have not the skilledge :(

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

I would recommend plink, eliminating the GUI completely.

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

WinActivate to make the window active

Also maybe try ControlSetText

Going the way of a cmd driven program sounds like a good solution as well, but then you would need to start over.

If I was going to use Putty and the first 2 things did not work I would run it on another computer so that the window is not in the way, keep the window off screen, or run it in a VM and keep the VM minimized. 

Link to comment
Share on other sites

Plink does not involve starting over...

http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

  • Plink (a command-line interface to the PuTTY back ends)

 

For your case something similar to:

$Connection = "plink.exe -ssh COMPUTERNAME -l USERNAME -pw PASSWORD" 
$log_dir = @ScriptDir & "\" & @MON & @MDAY & @YEAR & "\" 


Func _CableTest() 
$log = ">" & $log_dir & "\CableTestLog.log" 
$Command1 = "show int cable 5/0/14" 
runwait('cmd /c ' & $Connection & ' ' & $Command1 & $log) 
EndFunc ;_CableTest()
Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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