PuTTY Auto Login Macro using AutoHotKey

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
; 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
v_userid=youruserid
v_passwd=yourpasswd
; msec to wait before sending userid
v_wait_userid=1000
; msec to wait before sending password
v_wait_passwd=500
 
;-------------------------------------------------
; 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.

1
2
3
4
5
6
7
8
9
 
;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.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • MySpace
  • Reddit
  • Technorati
  • Twitter

, , ,

My favorite listmania

PDAs Everybody Wants!!! GPS, Ultra Mobiles, Media Players! Updated!

$365.60
HP iPAQ 211 Enterprise Handheld (210 Series) - Device died within 3 months. Unit would not charge and would not power on or reset. Sent to HP under basic warranty. HP claimed that due to some discolorations on the system board near the power / ...
$420.00
Apple iPod touch 32 GB (1st Generation) OLD MODEL - I absolutely love my new iPod touch. I only wish it was 80GB since I already have over 40GB of music and pix, but hey I love it.
$499.99
Nokia N810 Portable Internet Tablet - The Nokia N810 is a great device for browsing the web, Listening to music, or sending emails. The screen is large and high quality. It even has a gps. The ipod touch can't do halve of the stuff the N8...
$260.00
Asus A626 3.5-inch PDA Windows Mobile 5.0, Wi-fi (802.11 B+g), Bluetooth 2.0 (edr), - After many years, and being a sold-on-pda man who doesn't like to have all his eggs in one basket, I needed to upgrade. This was the one I chose after much research and I do not regret it. It's an upg...
Sony VAIO VGN-UX490N/C 4.5-Inch Portable Computer (Intel Core 2 Solo U2200 Processor, 1GB RAM, 48 GB Flash Drive, Vista Business) - I have one of these Sony UX UMPCs, an HP TX1000 and a Asus eee 701 with a 901 on order from Amazon.

I love the UX as an incredibly clever, compact gadget. It gets oohs and aahs wherever I...
Samsung Q1P SSD 7" Ultra Mobile PC (Intel Pentium M ULV Processor, 1 GB RAM, 32 GB Flash Drive, Windows XP) - Most amazing high tech product I have purchased yet! With a Verizon broadband USB plug in, you are in touch from everywhere. Easily zooms screen size, which requires more scrolling, but no need for ...
OQO 1070203-US Model 02 5" Ultra Mobile PC UMPC - Sprint - I own a Model 02 OQO. It has a built in "motherboard flaw" that causes it to lock up and become totally unusable. I was informed by Customer Service at OQO that "they are aware of this problem" and ...
OQO 1070303-US Model 02 5" Ultra Mobile PC UMPC - Verizon - I spent $3,000.00 to this product and learned an expensive lesson: this product has a major technical flaw. Someday, you might push the power button and realize that it is not working! If you have imp...
OQO 1060107-US Model 02 5" Ultra Mobile PC UMPC - I own a Model 02 OQO. My device has a built in "motherboard flaw" that causes it to "lock up" and become an unusable brick. Customer service told me they are "aware of the problem" and the tech I sp...
TAB PM/1.0 7W 1.2GB 80GB GPS CAM FPRT WLS WVHP - I am very happy with the R2HV i purchased. The screen is very clear. It comes with 2 batteries, 2 stylus, mouse, carry case. The speed is ok, it could be faster. The built in GPS receiver is great wh...
Samsung Q1 7-inch Ultra Q1U-EL (Intel A110 Processor, 1 GB RAM, 40 GB Hard Drive, Vista Premium) - When I first received the Samsung Q1U-EL, I booted it up with great dismay to find it had come with Vista, which in my opinion is a terrible operating system for any computer running an 800mhz process...
Samsung Q1Ultra NP-Q1BV000 Ultra Mobile PC (Intel Pentium M Processor, 1 GB RAM, 60 GB Hard Drive, Vista Premium) - The image and information given are false. This model of the Samsung Q1 Ultra runs on Windows XP Tablet PC, AND has an entirely different case design then the one displayed (just google it before buyi...
ASUS Eee PC 8G (7-inch Screen, 800 MHz Intel Celeron Processor, 1 GB RAM, 8 GB Solid-State Disk, Linux Preloaded) Pearl White - XP installed with ease and the included XP drivers CD is a great idea, but the keyboard is unusable.

Because the keyboard is impossibly small even for the most petite girls in our office,...
Pepper Pad 3 Handheld Web Computer (Black) - I had some high hopes for this product. To say that I'm disappointed is an understatement. My main goal was to have a quick booting web device that could allow me to read TV listings and possibly cont...
$271.54
HP iPAQ 111 Classic Handheld (FA979AA#ABA) (110 Series) - Don't buy this product. Poor service, and keeps loosing the information entered. Go with Dell Axiom.
$300.00
Apple iPod touch 16 GB (1st Generation) OLD MODEL - The iTouch is a great device, as long as you understand that it has the Apple limitations.

Video, sound and control are virtually great. It's really good for reading books, surfing the we...
Latest 416MHZ Intel Xscale Processor, Latest Windows Mobile 5.0 (pocket Pc), 3.5 - Excellent PDA and extremely useful for mapping and navigating. Minor issue connecting to free WiFI networks.
$599.00
HP iPAQ rx5915 Travel Companion - The HP iPAQ 5915 is a misbegotten hybrid of multiple bad systems. The ONLY place it shines is in the navigational software functionality. All else is garbage.

HP's links to Windows and ...
ASUS Eee PC 4G (7-Inch Display, Intel Mobile Processor, 512 MB RAM, 4 GB Hard Drive, Linux Preloaded) Galaxy Black - The main problem with this pc is that the Linux OP is propriatory. In order to add a program or update the browser you have to get it from ASUS, and currently they don't provide the latest firefox br...
Asus Eee 4G 7" PC Mobile Internet Device (512 MB RAM, 4 GB Hard Drive, Webcam, Linux Preloaded) Pearl White - i bought the 4G 512m linux eeepc. Have had to send it back twice after it worked well for a while. I stopped trying to grasp linux and just want to use it for internet radio and email. Once again it j...
$480.00
Asus Eee PC 4G Surf (7" Screen, 800 MHz Intel Celeron Processor, 512 MB RAM, 4 GB Hard Drive, Linux Preloaded) Galaxy Black - Nice little netbook, but the keys are too small for my already small hands, and the touch pad is not that great. The Linux OS is OK, but I think windows would be better.
$579.00
HP iPAQ hx2795 Pocket PC (FA676B#ABA) - I purchased this PDA a few weeks ago & I just love it. It's very easy to use. I don't know what I would do without it. The old PDA I had just wasn't as easy to use as this one is. Thanks.
$399.00
Palm TX Handheld - Order was filled and sent on a timely basis. Item was as described and in excellent condition. Instructions for start up were straightforward and easy to understand. Tech support offered for lifeti...
ASUS Eee PC 2G Surf (7-Inch Display, Intel Mobile Processor, 512 MB RAM, 2 GB Hard Drive, Linux Preloaded) Lush Green - I"m a classroom teacher and I wanted to try it out in my class.
My students love it. It's just their size. The games on it are great and it has the word processing that I want my students to be ...
ASUS Eee PC 2G Surf (7-Inch Display, Intel Mobile Processor, 512 MB RAM, 2 GB Hard Drive, Linux Preloaded) Sky Blue - I"m a classroom teacher and I wanted to try it out in my class.
My students love it. It's just their size. The games on it are great and it has the word processing that I want my students to be ...
$449.88
ASUS Eee PC 2G Surf (7-Inch Display, Intel Mobile Processor, 512 MB RAM, 2 GB Hard Drive, Linux Preloaded) Galaxy Black - I"m a classroom teacher and I wanted to try it out in my class.
My students love it. It's just their size. The games on it are great and it has the word processing that I want my students to be ...
$379.99
Hewlett-Packard iPAQ 310 Bluetooth 4.3-Inch Widescreen Portable GPS Navigator - I have owned this unit for 2 year now, and I like practically every aspect of this GPS unit (all the good things that everyone else has mentioned.) I never had a problem with strange routing like some...
HP iPAQ hx2495b Pocket PC (FA674B#ABA) - I can't remember how long I have had my 2495b but it has been at least 3 years. I have a 4Gb compact flash card and a 2Gb SD card installed and I consistently get 8 to 9 hours. I have never had a prob...
$509.99
Palm LifeDrive Mobile Manager - I loved this PDA! It has a 4 gig hard drive, which is awesome, and it holds so much information. The touch screen responds well, the wifi picks up as good as my laptop, and when I set it up with the...
PalmOne Tungsten T5 Handheld - Product only forked for 6 months, then just stopped working. Not a very good product.
$349.00
Palm Tungsten E2 Handheld -

The Palm Tungsten E2 is a very efficient and reliable personal data assistant. This PDA has served me well for many years, but recently it became erratic. After much trial and error ...
$175.00
Dell Axim X51v PDA 624 MHz Processor, 256MB ROM / 64MB SDRAM, 3.7 inches 480*640 VGA TFT Color LCD, BlueTooth / WiFi 802.11b/Infrared - I hope to be able to return the one I bought for $129 last week to the seller in LA CA. It was good in its day that is true. It was impossible for me to work with. Dell has no support available at a...
$299.99
PalmOne Tungsten E Handheld - Product delivered was as advertised and works fine. We were also sent information on how to set up the device which was useful. We were also sent information where free software to expand the units f...
AIRIS T620 PDA Navigation - PDA with fully embedded GPS receiver, Wi-Fi, and Bluetooth - I am using the AIRIS T620 with an i-go and an English version of the Active-Sync on the PC side since it comes with a Spanish Active-Sync or at least the one i have it came with it. It is pretty fast ...
  1. #1 by iman at June 26th, 2008

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

  2. #2 by Jay at July 6th, 2008

    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
    http://jayrajput.blogspot.com/search/label/Putty

  3. #3 by HanaDaddy at July 6th, 2008

    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. #4 by Nix at July 12th, 2008

    This is wonderful! Great work, mate ;) Tnx for sharing!

  5. #5 by bhavin at August 5th, 2008

    Great work!!!

  6. #6 by Sonia at August 9th, 2008

    You can do a lot more than automatic login with AutoHotkey. This free and small program is what we call an automation program. With the help of hotkeys like F1, F2 etc., it types whatever sequence you want it to type like “nano /var/log/apache2/access.log”.

    Read more on my blog at http://cedeq.com/blog/automation/how-to-work-faster-with-putty

  7. #7 by steve at August 10th, 2008

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

  8. #8 by Mel at August 18th, 2008

    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.

    Mel

  9. #9 by HanaDaddy at August 18th, 2008

    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.

  10. #10 by Mel at August 18th, 2008

    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.

  11. #11 by Sampath at September 7th, 2008

    Have you found a way to send key strokes based on the prompt in putty instead of a sleep time.

  12. #12 by Sonia at September 7th, 2008

    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]

    Sonia

  13. #13 by HanaDaddy at September 7th, 2008

    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.

  14. #14 by G at November 1st, 2008

    Wow thanks! You just saved me loads of time!

  15. #15 by Grey at December 11th, 2008

    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.

    ^#!s::
    ;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
    WinActivate
    Sendinput MyPassPhraseHere{Enter}

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

    ;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
    return

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

  16. #16 by HanaDaddy at December 11th, 2008

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

  17. #17 by Narendran Babu at December 25th, 2008

    Hi,

    I really appreciate the help, it was great using it.

  18. #18 by Michael Kabugi at January 7th, 2009

    Thank you very much. Totally Genius!!

  19. #19 by Jane Taubman at February 11th, 2009

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

  20. #20 by Doug Boreham at September 29th, 2009

    Does anyone have code written for AutoHotkey that will close Putty entirely without user input?

  21. #21 by Grey at September 29th, 2009

    Closes putty, or any other process.

    Process, close, putty.exe

  22. #22 by Pete G at December 31st, 2009

    Perhaps what you are looking for is KiTTY: http://www.9bis.net/kitty/?page=Logon%20script

  23. #23 by HanaDaddy at December 31st, 2009

    Thank you, Pete! This is the long waited solution for AUTO LOGIN for PuTTY! It works perfectly!

  24. #24 by Sjoerd at January 19th, 2010

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

    Thanks!

(will not be published)
  1. No trackbacks yet.
Quick Coupons!
Abe's of Maine: Actually , if you buy a product more than $75, you can use FRIEND10 coupon code to get extra $10 off!