Putty Automation - 1 doubleclick to open, login, resize, reposition & enter commands in multiple sessions!

This is not really the kind of automation expected in the forum when someone says automation, but this is definately automation and definately saves me considerable time on the kinds of things I am monitoring while I work on HA -

I got R.E.A.L.L.Y. sick of manually opening multiple putty windows, each time entering my userid and password, resizing and moving each window, entering the commands I wanted, then opening another one, rinse and repeat, rinse and repeat… so consider this to make life easier - it also lets you name each putty window - here is how I did it.

One doubleclick for me to get these four windows!

Note, this is for windows and I use it to open four putty sessions tailing the syslog on my RPI running HA Supervised but it is very flexible, you can open/launch whatever you want - so the instructions below can result in one doubleclick to launch these and enter the tail commands I am currently using

  1. Download and install “AutoHotKey” - this website also has excellent help documentation - from AutoHotkey Downloads

  2. Open a putty session as you usually do to get a command line window. Note the exact title of the window - copy and paste it somewhere (you will need this for step 6 later). Once you have done that, you can just close the Putty window, it’s no longer needed.

  3. In the putty dialog box (such as mine shown below for example), save one separate configuration you want for each size window with preferred fonts, etc. (required because the AutoHotKey command for resizing a window does not work in the case of Putty sesions). Note I have named the three sizes I use in my four windows shown above (first window is Config1, 2nd & 3rd windows are Config2, and the 4th window is Config3) for this one click example - so the only difference between these putty configurations are the window sizes (but you can of course make them different fonts or whatever).

image

  1. In the same directory where you unzipped the AutoHotKey executable, create a command file with notepad - I called mine “Setup_RPI_Tails.cmd” and it only has one line:
autohotkey Setup_RPI_Tails.ahk
  1. Put two shortcuts on your desktop - one to the .cmd file (and one to the .ahk file you create in #6 below) - then you only have to doubleclick on the .cmd file shortcut launch all your putty sessions (and you can just doubleclick on the .ahk file to make edits to what it does (the first time you click on the .ahk shortcut, windows might ask you what you want to do with the file, I just told it to open it with notepad)).

  2. Then, again in notepad, I created the file with the below contents and saved it in the same directory, named “Setup_RPI_Tails.ahk”. If the below is not easy to read, just paste it into Notepad to take a look (you may as well do that anyway to save it for yourself and edit it as needed to use it!). Note the ‘SetMeUp’ function written at the bottom of the piece of code (you have to scroll down to see it) is just called repeatedly. To make this yours, just enter the IP of the linux host, your username and password, and then customize what commands you want entered into each window when it appears. Also - note where it says this in the ‘SetMeUp’ function at the bottom:

WinWaitActive XXXXXXXXXX

Remember step 2? Replace the XXXXXXXXXX in that ‘SetMeUp’ function below (again, you need to scroll down to see it) with the exact text you copied and pasted from step 2 (don’t put any quotes around it). All this does is, it tells AutoHotKey to wait until the new putty window with that title finally finishes being created - before proceeding.

; https://www.autohotkey.com

#SingleInstance

; Launch PuTTY sessions

; Set reused params
IpAddr := "###.###.###.###"
UserId := "####"
Pwd := "#######"

; Window #1
; These two variables are just for readability (instead of putting the string into the custom function call directly)
CommandLine1 := "sudo cat /usr/share/hassio/share/syslog | grep 'yolink mqtt client disconnected!'"
CommandLine2 := "sudo tail -f /usr/share/hassio/share/syslog | grep 'yolink mqtt client disconnected!'"
SetMeUp(IPAddr, "Window 1", UserId, Pwd, 0, 10, "Config1", CommandLine1, CommandLine2)

; Window #2
CommandLine1 := "sudo cat /usr/share/hassio/share/syslog | grep 'ERROR\|error'"
CommandLine2 := "sudo tail -f /usr/share/hassio/share/syslog | grep 'ERROR\|error'"
SetMeUp(IPAddr, "Window 2", UserId, Pwd, 0, 155, "Config2", CommandLine1, CommandLine2)

; Window #3
CommandLine1 := "sudo cat /usr/share/hassio/share/syslog | grep '.automation.' | grep -v 'IFTTT'"
CommandLine2 := "sudo tail -f /usr/share/hassio/share/syslog | grep '.automation.' | grep -v 'IFTTT'"
SetMeUp(IPAddr, "Window 3", UserId, Pwd, 0, 385, "Config2", CommandLine1, CommandLine2)

; Window #4
SetMeUp(IPAddr, "Window 4", UserId, Pwd, 0, 600, "Config3", "sudo tail -f /usr/share/hassio/share/syslog", "")

Exit

SetMeUp(IPAddr, WindowName, UserId, Pwd, X, Y, PuttyConfigFile, CommandLine1, CommandLine2)
{
  Run "C:\Program Files\PuTTY\putty.exe" -load %PuttyConfigFile% -ssh %UserId%@%IPAddr% -pw %Pwd% -t
  WinWaitActive XXXXXXXXXX
  SetTitleMatchMode, 2
  WinSetTitle %WindowName%
  WinMove %X%,%Y%
  SetKeyDelay 0, 10
  ControlSend, , %CommandLine1%{Enter}, ahk_class PuTTY
  ControlSend, , %CommandLine2%{Enter}, ahk_class PuTTY
}
  1. Remember step 5 with the shortcuts? Once you have the above working the way you want, make a couple other files for other purposes - for example to launch an additional quick putty session already logged in whenever you need it, etc… :slight_smile:

This community has helped me alot, so I feel as though I should give back when I can. (Now it takes me 10 times as long to close all the windows as it does to open them!) Enjoy!

1 Like

Excellent, thank you.