Sign in to follow this  
Followers 0
TheRauchster101

Send keystrokes to minimized PuTTY session.

5 posts in this topic

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?

Share this post


Link to post
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 :(

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I would recommend plink, eliminating the GUI completely.

Edited by boththose

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

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

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

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