Jump to content

Putty logfile opening troubleshooting


Recommended Posts

The trouble I'm having is quite simple, yet it boggles me why it is occurring. I cannot open a log file generated by Putty.exe while the putty session is in progress using either fileread($sPuttyLogFileLocation & "\Putty.log") or fileopen($sPuttyLogFileLocation & "\Putty.log", 0). I understand that it is not desirable to open files in write mode that have streams already opened with other processes, but it shouldn't stop from opening a file in read only mode!

The process to recreate the problem is as follows:

1.) Run putty in Console, Telnet, or SSH mode. Set logging to "printable output" and direct log file to $sPuttyLogFileLocation & "\Putty.log"

2.) With the putty session open try to use fileopen($sPuttyLogFileLocation & "\Putty.log", 0) or fileread($sPuttyLogFileLocation & "\Putty.log") methods

3.) Fileread() will return empty and fileopen() will set @error to -1

Here is some snippets of code to recreate the problem:

$sPuttyLogFileLocation = @HomeDrive
$sIpAddress = "10.10.10.10"

startPuttyTelnet()

MsgBox(0,"", FileRead($sPuttyLogFileLocation & "\Putty.log"))
$file = FileOpen($sPuttyLogFileLocation & "\Putty.log", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open the file.")
    Exit
EndIf
FileClose($file)

Func startPuttyTelnet()
    Run("C:\Program Files\PuTTY\putty.exe")
    WinWait("PuTTY Configuration")
    If Not WinActive("PuTTY Configuration") Then WinActivate("PuTTY Configuration")
    WinWaitActive("PuTTY Configuration")
    Send($sIpAddress) ;port 23 automatically selected
    Send("!t") ;select Telnet
    Send("+{TAB 3}")
    Sleep(250)
    Send("{DOWN}") ;enter logging options window
    ControlSend("PuTTY Configuration", "Log &file name:", "Edit1", "{DEL 10}")
    ControlSetText("PuTTY Configuration", "Log &file name:", "Edit1", $sPuttyLogFileLocation & "\Putty.log")
    If Not WinActive("PuTTY Configuration") Then WinActivate("PuTTY Configuration")
    WinWaitActive("PuTTY Configuration")
    ;select printable output
    Send("!p")
    If Not WinActive("PuTTY Configuration") Then WinActivate("PuTTY Configuration")
    WinWaitActive("PuTTY Configuration")
    ControlClick("PuTTY Configuration", "&Open", "Button1")
    If FileExists($sPuttyLogFileLocation & "\Putty.log") Then
        ;handle logfile already exists window if presented
        If WinWait("PuTTY Log to File", "The session log file", 4) Then
            If Not WinActive("PuTTY Log to File") Then WinActivate("PuTTY Log to File")
            WinWaitActive("PuTTY Log to File", "The session log file")
            ControlClick("PuTTY Log to File", "&Yes", 6)
        EndIf
    EndIf
EndFunc

I'm using Putty 0.60 and Autoit 3.3.4.0. I've spent several hours troubleshooting this issue. The script has been used before in the past using the method that currently does not function. Putty on any of my machines tested with has not changed versions. Autoit has been updated multiple times on these machines however leading me to believe this is a bug. Also, the log file can be opened with Notepad while the putty session is in progress. When the file is opened with Notepad the contents are readable as expected.

A workaround can be done, but I prefer to handle the file directly rather than copy it to another file or read it with notepad to parse.

This would be a far slower, yet functional workaround:

$sPuttyLogFileLocation = @HomeDrive
$sTempPuttyFileLocation = @TempDir
$sIpAddress = "10.10.10.10"

startPuttyTelnet()
FileCopy($sTempPuttyFileLocation & "\Putty.log", $sPuttyLogFileLocation & "\Putty.log", 1)
MsgBox(0,"", FileRead($sPuttyLogFileLocation & "\Putty.log"))
$file = FileOpen($sPuttyLogFileLocation & "\Putty.log", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open the file.")
    Exit
EndIf
FileClose($file)

Func startPuttyTelnet()
    Run("C:\Program Files\PuTTY\putty.exe")
    WinWait("PuTTY Configuration")
    If Not WinActive("PuTTY Configuration") Then WinActivate("PuTTY Configuration")
    WinWaitActive("PuTTY Configuration")
    Send($sIpAddress) ;port 23 automatically selected
    Send("!t") ;select Telnet
    Send("+{TAB 3}")
    Sleep(250)
    Send("{DOWN}") ;enter logging options window
    ControlSend("PuTTY Configuration", "Log &file name:", "Edit1", "{DEL 10}")
    ControlSetText("PuTTY Configuration", "Log &file name:", "Edit1", $sTempPuttyFileLocation & "\Putty.log")
    If Not WinActive("PuTTY Configuration") Then WinActivate("PuTTY Configuration")
    WinWaitActive("PuTTY Configuration")
    ;select printable output
    Send("!p")
    If Not WinActive("PuTTY Configuration") Then WinActivate("PuTTY Configuration")
    WinWaitActive("PuTTY Configuration")
    ControlClick("PuTTY Configuration", "&Open", "Button1")
    If FileExists($sPuttyLogFileLocation & "\Putty.log") Then
        ;handle logfile already exists window if presented
        If WinWait("PuTTY Log to File", "The session log file", 4) Then
            If Not WinActive("PuTTY Log to File") Then WinActivate("PuTTY Log to File")
            WinWaitActive("PuTTY Log to File", "The session log file")
            ControlClick("PuTTY Log to File", "&Yes", 6)
        EndIf
    EndIf
EndFunc
Edited by Sn3akyP3t3
Link to comment
Share on other sites

  • 3 weeks later...
  • 2 weeks later...

Well... I experienced the same as you did. It's impossible to open a Putty Log file whith simpler autoit means, while Windows notepad has no problem doing it. Actually I had a lot to do with PuTTY lately and I overcame some problems. I hope there's something within you can adapt.

First: I wrote a function helping me opening PuTTY and waiting for user and password entry.

It also handles ssh keys by just approving to accept them. It's in PUTTY.AU3

It also makes PuTTY logging

Second: I needed the PuTTY output during a session, free from terminal-codes (while accessing Unix-Server)

As you, I experienced the bug that you can't read with simple autoit means.

But some of the autoit community appendes a CC+ like interface for file handling. I used this

in concurrentread.au3 (it includes putty.au3). I hope you can use some of it It's in concurrentread.au3

Simply change some IP adresses, usernames etc... try it

concurrentread.au3

putty.au3

out-aged

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...