Logging into putty by reading the log file.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
CarPiotr
Posts: 40
Joined: 11 Dec 2019, 08:21

Logging into putty by reading the log file.

Post by CarPiotr » 02 Mar 2022, 15:43

Hello !
I have changed the way of logging in to putty. Previously, it was done as standard, so when starting putty I added the session parameter and password. Now, before the password appears, I have to press "Enter" twice. In autohotkey I write simple scripts so I made it simple but the computer is sometimes loaded and not always the script will execute correctly. I found a script on the forum that executes commands based on reading the log file, but it does not want to work, although the indicated text appears in the logs. What is wrong ? In putty, I have set the log to be overwritten when the next window is started.

Code: Select all

logfile = %A_ScriptDir%\putty.log

run,C:\Program Files\PuTTY\putty.exe -load session


loop 
	{
		Loop, read, %logfile%
		last_line := A_LoopReadLine
		if last_line=AFM-User
		break
		sleep 100
	}
Send {Enter}
Send {Enter}

loop 
	{
		Loop, read, %logfile%
		last_line := A_LoopReadLine
		if last_line=password
		break
		sleep 100
	}

Send password{ENTER}
Return

User avatar
mikeyww
Posts: 27373
Joined: 09 Sep 2014, 18:38

Re: Logging into putty by reading the log file.

Post by mikeyww » 02 Mar 2022, 20:00

A simple debugging strategy would be to display the result of every conditional statement, as well as the value of every variable, when defined. With a short script like this, that could be done in just a few minutes. Doing that will likely pinpoint the problem with the script.

CarPiotr
Posts: 40
Joined: 11 Dec 2019, 08:21

Re: Logging into putty by reading the log file.

Post by CarPiotr » 08 Mar 2022, 09:16

Hello !
Thanks for the tips. I used a different solution and it works very well for me. I would like to ask about another problem I have encountered.
I want to run minimized putts and execute all commands via "ControlSend" and activate the window after the last command. Unfortunately, I can't send one command. ControlSendRaw ,,% password, ahk_pid% PID%.
"Password" is taken from the GUI window. Where is the problem ?
This is only a fragment of the code, after entering the password I send other commands via "ControlSend" which select options from the server menu

Code: Select all

run C:\Program Files\Putty\putty.exe -load Session,,Min,PID
LogPutty := "putty.log"
WinWait, ahk_pid %PID%
Sleep, 100
	Loop
	{
		FileRead, VAR, % LogPutty
		 if InStr(VAR, "AFM-Users")
			{
				ControlSend,,{Enter},ahk_pid %PID%
				ControlSend,,{Enter},ahk_pid %PID%
				Break
			}
Sleep, 200
	}
	Loop
	{
		FileRead, VAR, % LogPutty
		 if InStr(VAR, "'s password:")
			{
				WinActivate, ahk_pid %PID%
				Sleep, 100
				SendRaw, % Password                   ; Here I would like to use ControlSendRaw ,,% password, ahk_pid %PID%
				ControlSend,,{Enter},ahk_pid %PID%
				Break
			}
Sleep, 200
	}
Return

User avatar
mikeyww
Posts: 27373
Joined: 09 Sep 2014, 18:38

Re: Logging into putty by reading the log file.

Post by mikeyww » 08 Mar 2022, 09:44

It looks like Putty does not handle that well, as the About window is triggered. In any case, since you are activating the window, you would not need a ControlSend.

CarPiotr
Posts: 40
Joined: 11 Dec 2019, 08:21

Re: Logging into putty by reading the log file.

Post by CarPiotr » 08 Mar 2022, 10:39

Thanks !
I activate the window because that's the only way it accepts my password. :)

User avatar
mikeyww
Posts: 27373
Joined: 09 Sep 2014, 18:38

Re: Logging into putty by reading the log file.

Post by mikeyww » 08 Mar 2022, 12:03

The following technique worked. Window Spy will show you the control names.

Code: Select all

ControlSetText, Edit1, test, ahk_exe putty.exe

CarPiotr
Posts: 40
Joined: 11 Dec 2019, 08:21

Re: Logging into putty by reading the log file.

Post by CarPiotr » 11 Mar 2022, 06:15

It worked

Code: Select all

ControlSendRaw ,,{text}%password%, ahk_pid %PID%
Greetings !

Post Reply

Return to “Ask for Help (v1)”