PuTTY Auto Login Macro using AutoHotKey

PuTTY Auto Login Macro using AutoHotKey
2 votes, 5.00 avg. rating (95% score)

PuTTY is the best SSH client and it’s free. But it lacks of this little function that I really want – Automatic login.

So I have searched many Google pages and found this PuTTY Connection Manager which I explained details in my previous post. It is a very good software.

But somehow I wanted the auto login function without the extra programs. I wanted it to be done directly with PuTTY, not within the frame of PuTTY Connection Manager.

And I found this software called AutoHotKey (http://www.autohotkey.com/) which can automate keyboard and mouse actions. But I gave up to use it at that time because I was too lazy to read all the instructions and to come up with a script for the auto login.

But yesterday, I decided to take a look again and this time I successfully created the Auto Login script! To tell you the truth, it was not easy but it is worth it. I am proud of myself πŸ™‚

So here is the script.

; You need to define the full path where the putty.exe exists . 'putty_profile' should be existing profile name.
v_program=%A_ProgramFiles%\PuTTY\putty.exe -load putty_profile
; msec to wait before sending userid
; msec to wait before sending password
; To make modifier keys to work properly (such as Shift)
SetKeyDelay, 0, 10
run, %v_program% , , , newpid
;you need to wait a little for the window to initialize
;sleep, 100
winwait, ahk_pid %newpid%
;wait for the login prompt to appear
sleep, %v_wait_userid%
ControlSend, , %v_userid%{enter}, ahk_pid %newpid%
sleep, %v_wait_passwd%
ControlSend, , %v_passwd%{enter}, ahk_pid %newpid%
;create unique title so you won't be lost 
WinGetTitle, v_title, ahk_pid %newpid%
v_newtitle=%v_title% - %newpid%
WinSetTitle, ahk_pid %newpid%,,%v_newtitle%

If you want to try this yourself, you will need to change some information.

  1. Install AutoHotKey software.
  2. Create any file name with extension .ahk. For example ‘ssh.ahk’ would be fine.
  3. Copy & paste above code into the file. But need to change the values.
    v_program: PuTTY location with the -load option and connection profile name
    v_userid: userid
    v_passwd: passwd
    v_wait_userid : msec to wait before sending userid
    v_wait_passwd : msec to wait before sending passwd
  4. Save it and double click on the file to execute it. And watch the PuTTY logging in the server automagically!

In fact, you can easily add additional commands at the end of the script to execute further commands.

Below is an example.

;maybe you want to wait some seconds while initial welcome screen appears. Let's say 2 seconds.
sleep, 2000 
;cd upload
ControlSend,,cd upload{enter} , ahk_pid %newpid%
;You can send the folder list to your email account.
ControlSend,,ls|mail -s "Daily File Listing for Upload" your@emailaccount.com{enter}, ahk_pid %newpid%

Note that there is a little issue that the password is saved as plain text. But I can live with it for now.

Here is my script in action.

Come visit again for more information on PuTTY.

24 thoughts on “PuTTY Auto Login Macro using AutoHotKey

  1. Thank you very much – it worked as mentioned and will definitely prove useful in my daily operations.

  2. putty has a option for password, I have been using that for a while because the machines which I use are formatted frequently. Since they are formatted frequently key sharing is painful. But if you want to send the password in cleartext which might be the case where you are accessing server available on your local network(not on the internet).

    putty host1 -l root -pw

    same information can be found at

  3. Jay, I really appreciate your comment. I was really happy that there is such option existed. I have tested it also worked with session profiles. So below example works perfectly.

    putty -load %profile_name% -l %userid% -pw %password%

    But according to the official Putty Manual, the -pw option doesn’t work with telnet.

    Note that the -pw option only works when you are using the SSH protocol. Due to fundamental limitations of Telnet and Rlogin, these protocols do not support automated password authentication.

  4. I remember being interested in autohotkey once a long while ago, but never got around to learning it. Then I found your site when searching for a way to autologin to putty, and finally started using autohotkey, and now I use it for tons of everyday tasks, and I can’t imagine life without it. Just wanted to say thanks. πŸ™‚

  5. It is a very useful starting point and thanks for your efforts, but I have to ask a question:

    It looks like you are assuming you would receive the login prompt after a certain amount of time has passed and sending the username. Is there a way to check if you really received the prompt asking you the username and then actually send it ? In other words, can you read the putty screen and determine if the prompt has arrived ? In my particular case, some servers, due to their workload and/or network segments that they are located on, getting the prompt takes close to or sometimes exceeding 1 minute. And if I make the wait that long, I will have to wait unnecessary amount of time for logging into 80% of my machines. Thought about making two scripts but this time I have to figure out which script to use with which server, which is insane in a 100+ server environment and growing almost every week.

    Thanks and keep up the good work.


  6. Wow mel, 100 servers to maintain. That’s a big job. I myself tried to find a way to determine the login prompt, but it has been unsuccessful up to now. Once I find how to do this properly, I will let you know. Thanks.

  7. I was investigating further after I left the message above and found out that you can scrape the putty.log file (or whatever name you gave to your log file) to see if the “username” prompt has arrived but this implies that your session starts with logging enabled. I have read the putty configuration but could not find how to enable/disable logging from a command line start of putty other than using “-load session_name” switch, which requires you to have a session file created manually with the logging enabled. Not a big deal for a few servers but you can imagine creating 100+ session files is not very easy let alone remembering to create one everytime a new server gets added to the landscape.

    In retrospect, I considered sending a keystroke to the terminal to open up the change settings menu but this time the problem is, the usual alt-space is not popping up the context menu for putty as it does with other native/close-to-native windows.

    More leads for you, as you seem to be more gifted than I do in programming as well as have more time than I do.

  8. Sampath,

    The AutoHotkey MouseClickDrag function will put in the clipboard a text area in PuTTY. Then you just have to verify the clipboard to know if you can continue.

    MouseClickDrag, WhichButton, X1, Y1, X2, Y2 [, Speed, R]


  9. Sonia , That’s a good idea, but can you run the multiple process at the same time ? I mean multiple AutoHotKey will try to access the clipboard. Thank you for the information anyway.

  10. Not as elegant as your’s but here’s my auto login for Putty. My ssh access uses a passkey so no need to enter username or password into the shell. It also uses Pageant to retrieve and run the passkey. Putty, Pageant and the autohotkey script are all located on my usb stick in the same folder. The ‘pageantkey’ that the script points to is actually a short cut to pageant which also contains the location of the passkey so it automatically loads the passkey and asks for the password, ie in the shortcuts target = “U:\portablePrograms\portaputty\pageant.exe masterkey.ppk” (where masterkey is the key you want pageant to load located in the same directory as pageant.)
    I have used the autohotkey compiler to prevent the script being decomplied so my PW is reasonably protected. I have also saved my username in the putty configuration. This works great for my purposes of just connecting to my home network.

    ;Sets everything to run from the usb drive.
    SetWorkingDir, %A_WorkingDir%

    ;Run the shortcut to pageant which also loads the pass key, then enter the passkey passphrase.
    Run, pageantkey
    WinWaitActive Pageant: Enter Passphrase
    Sendinput MyPassPhraseHere{Enter}

    ;Run putty, activate it.
    Run, putty.exe
    WinWaitActive PuTTY Configuration

    ;This is to send key presses to putty to load the correct profile, depending on where in the profile list your conenction settings are determines the number of downs.
    Send {Tab}{Tab}{Tab}{Tab}{Down}{Down}{Down}{Enter}

    ;Wait for the putty session to end and then terminate pageant.
    Process, WaitClose, putty.exe
    Process, close, pageant.exe

    A problem I found was that you had to set the usb key to the correct drive letter otherwise you’d run into problems with the shortcut paths for the pageant passkey. This is an issue on systems where you don’t have admin to change your drive letter. I found a way around this was to add a couple of short batch files to the script.
    Put this line in the script straight after setting the working directory.
    Run, USBon.bat

    Create USBon.bat as so:

    @echo off
    subst u: %cd%

    where u is the drive letter you want to use. Place the .bat file in the same folder as putty & pagenat etc
    Then at the end of the script put in a USBoff.bat, also of course place this in the same folder…
    Run, USBoff.bat

    In usboff.bat put in:

    @echo off
    subst u: /D

    This relesaes the drive letter at termination of the script… As I said, not as elegant as your’s but shows how versatile autohotkey can be. I love using it! Your scripts given me a couple of ideas for modification. Thanks. πŸ™‚

  11. @Grey, Thanks for your insight and solution! For SSH connection, it is easier to implement auto login. For example, you can directly run it from command prompt to connect with access information.

    putty -load %profile_name% -l %userid% -pw %password%

    Also, there is a separate Freeware solution called PuTTY Connection Manager. It has implemented auto login function similar in a way to my AutoHotKey script (waiting few seconds before sending username and password) , but it is very nice and tabbed interface makes it easy to maintain multiple putty sessions.

  12. Thank you so much for publishing this. I was just about to write one when I found your version, works a treat.

  13. For use with obligatory special characters in your password I suggest using ControlSendRaw for that part.


Leave a Reply

Your email address will not be published. Required fields are marked *