so i'm using a code that is activating the window via the process id. it used to work. now it does not.
it's actually a series of macros.
Macro 1: runs a code to search all 'open' process id's and saves then within a collection.
Macro 2: then open a PuTTY sessions via cmd profile for the log file.
Macro 3: runs a code once again to search all open process id's, then compares each id with the collection from macro 1 and removes them one by one til what should be left is the single process id for that particular putty session.
then it will run a macro to login with 'default' usernames and passwords, to the device it is using as 'telnet' via PuTTY, while also checking the log file to see if it's actually logged in or not, if not, it tries to login with 'personal' credentials which are also saved as variables with a userform.
if i use the command 'AppActivate ("PuTTY")' it will activate the window and leave it active for entering a username and password.
if i use the command 'AppActivate ("process ID")' it will bring that window to the front, then immediately deactivate the window.
as stated above, this code used to work, but for some reason, now it no longer works as it deactivates the window immediately, so when it tries to use 'sendkeys' for the credentials, it can't actually enter them on that window.
any ideas would be great, and i will past my code below as best as i can while protecting any personalized info.
<code>
'=========================================================================
' API Functions and Variables Declaration
'=========================================================================
Private Const INVALID_HANDLE_VALUE = -1&
Private Const TH32CS_SNAPPROCESS = &H2
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * 1000 'Increase this limit if there are more than 1000 Process
End Type
Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, PE32 As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, PE32 As PROCESSENTRY32) As Long
Private PE32 As PROCESSENTRY32
Private Proc_Name As String
Private hSnapshot As Long
Private iRet1 As Long
Private lRet As Long
'*************************************************************************
' Open PuTTY
'*************************************************************************
Sub PuTTY_Session()
Call Pre_Active_PuTTY_Sessions
End Sub
'*************************************************************************
' Check PuTTY Sessions Before Opening
'*************************************************************************
Sub Pre_Active_PuTTY_Sessions()
'Call Clear_Sheet
Set PuTTY_Col = New Collection
'Get Snapshop of All Process.
'To get details about Threads of each process, refer MSDN for additonal Parameters
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
If hSnapshot <> INVALID_HANDLE_VALUE Then
PE32.dwSize = Len(PE32)
lRet = Process32First(hSnapshot, PE32)
Do While lRet
iRet1 = InStr(1, PE32.szExeFile, VBA.Strings.Chr(0))
If iRet1 > 0 Then
Proc_Name = VBA.Strings.Left(PE32.szExeFile, iRet1 - 1)
If InStr(Proc_Name, "putty.exe") <> 0 Then
PuTTY_Col.Add PE32.th32ProcessID
End If
End If
lRet = Process32Next(hSnapshot, PE32)
Loop
CloseHandle hSnapshot
End If
Call Open_PuTTY
End Sub
'*************************************************************************
' Open PuTTY
'*************************************************************************
Sub Open_PuTTY()
Dim PT As String, PT_Log As String
Dim TaskID As Long
Select Case ActiveSheet.Name
Case "Main"
mySlam = Sheets("Main").Range("C9")
ipAdd = Sheets("Main").Range("C11")
Case "CLI"
mySlam = Sheets("CLI Info").Range("C6")
ipAdd = Application.WorksheetFunction.VLookup(Sheets("CLI Info").Range("C6"), Sheets("Data Tables").ListObjects("Dslams").Range, 3, False)
End Select
On Error Resume Next
DirFile = "C:\putty.log"
If Dir(DirFile) <> "" Then
Kill DirFile
End If
On Error GoTo 0
PT = "C:\Program Files\PuTTY\putty.exe -telnet -sessionlog " & _
"""C:\putty.log""" & " -P 23 " & ipAdd
TaskID = Shell(PT, vbNormalFocus)
Application.Wait Now + TimeValue("00:00:02")
Call Post_Active_PuTTY_Sessions
End Sub
'*************************************************************************
' Check PuTTY Sessions After Opening
'*************************************************************************
Sub Post_Active_PuTTY_Sessions()
Set PuTTY_Col2 = New Collection
'Get Snapshop of All Process.
'To get details about Threads of each process, refer MSDN for additonal Parameters
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
If PuTTY_Col.Count = 0 Then
If hSnapshot <> INVALID_HANDLE_VALUE Then
PE32.dwSize = Len(PE32)
lRet = Process32First(hSnapshot, PE32)
myRow = 2
myCol = 1
'Fetch Each Process Details one by one
Do While lRet
iRet1 = InStr(1, PE32.szExeFile, VBA.Strings.Chr(0))
If iRet1 > 0 Then
Proc_Name = VBA.Strings.Left(PE32.szExeFile, iRet1 - 1)
If InStr(Proc_Name, "putty.exe") <> 0 Then
myPuTTY = PE32.th32ProcessID
End If
End If
lRet = Process32Next(hSnapshot, PE32)
Loop
CloseHandle hSnapshot
End If
Else
If hSnapshot <> INVALID_HANDLE_VALUE Then
PE32.dwSize = Len(PE32)
lRet = Process32First(hSnapshot, PE32)
myRow = 2
myCol = 1
'Fetch Each Process Details one by one
Do While lRet
iRet1 = InStr(1, PE32.szExeFile, VBA.Strings.Chr(0))
If iRet1 > 0 Then
Proc_Name = VBA.Strings.Left(PE32.szExeFile, iRet1 - 1)
If InStr(Proc_Name, "putty.exe") <> 0 Then
PuTTY_Col2.Add PE32.th32ProcessID
End If
End If
lRet = Process32Next(hSnapshot, PE32)
Loop
CloseHandle hSnapshot
End If
Do Until PuTTY_Col2.Count = 1
For i = 1 To PuTTY_Col.Count
If PuTTY_Col(i) = PuTTY_Col2(1) Then PuTTY_Col2.Remove 1
Next i
Loop
myPuTTY = PuTTY_Col2(1)
End If
Set PuTTY_Col = Nothing
Set PuTTY_Col2 = Nothing
Call PuTTY_Login
End Sub
Sub PuTTY_Login()
AppActivate (myPuTTY)
SendKeys "ADMIN" & "{ENTER}" & "PASSWORD" & "{ENTER}"
Application.Wait Now + TimeValue("00:00:01")
Open "C:\putty.log" For Input As #1
Do Until EOF(1)
Line Input #1 , textline
'Text = Text & textline
Text = textline
Loop
Close #1
If InStr(Text, mySlam) <> 0 Then
AppActivate (myPuTTY)
SendKeys "en" & "{ENTER}" & "terminal length 80" & "{ENTER}"
Application.Wait Now + TimeValue("00:00:01")
Else
Call PuTTY_Login_Personal
End If
If ActiveSheet.Name = "Main" Then
Application.Wait Now + TimeValue("00:00:01")
AppActivate (myPuTTY)
SendKeys "menu" & "{ENTER}"
End If
Application.Wait Now + TimeValue("00:00:01")
Call KeyTest
Call KeyTest
End Sub
Sub PuTTY_Login_Personal()
Application.Wait Now + TimeValue("00:00:01")
If myUN = "" Or myPW = "" Then
AppActivate ("HSI 2.0.xlsm - Excel")
PuTTY_Info.Show
End If
If myUN = "" Or myPW = "" Then
MsgBox "Login Info input wrong.", vbCritical, "Error!"
Exit Sub
End If
Application.Wait Now + TimeValue("00:00:01")
AppActivate (myPuTTY)
SendKeys myUN & "{ENTER}" & myPW & "{ENTER}" ' & _
"terminal length 60" & "{ENTER}"
Application.Wait Now + TimeValue("00:00:03")
Open "C:\putty.log" For Input As #1
Do Until EOF(1)
Line Input #1 , textline
'Text = Text & textline
Text = textline
Loop
Close #1
Application.Wait Now + TimeValue("00:00:01")
If InStr(Text, mySlam) <> 0 Then
AppActivate (myPuTTY)
SendKeys "terminal length 80" & "{ENTER}"
Else
'If MsgBox("Is this info correct?" & vbCrLf & vbCrLf & " UN: " & _
myUN & vbCrLf & " PW: " & myPW, vbYesNo, "Your Info") = vbNo Then _
PuTTY_Info.Show
Call PuTTY_Login_Personal
Exit Sub
End If
End Sub
</code>
it's actually a series of macros.
Macro 1: runs a code to search all 'open' process id's and saves then within a collection.
Macro 2: then open a PuTTY sessions via cmd profile for the log file.
Macro 3: runs a code once again to search all open process id's, then compares each id with the collection from macro 1 and removes them one by one til what should be left is the single process id for that particular putty session.
then it will run a macro to login with 'default' usernames and passwords, to the device it is using as 'telnet' via PuTTY, while also checking the log file to see if it's actually logged in or not, if not, it tries to login with 'personal' credentials which are also saved as variables with a userform.
if i use the command 'AppActivate ("PuTTY")' it will activate the window and leave it active for entering a username and password.
if i use the command 'AppActivate ("process ID")' it will bring that window to the front, then immediately deactivate the window.
as stated above, this code used to work, but for some reason, now it no longer works as it deactivates the window immediately, so when it tries to use 'sendkeys' for the credentials, it can't actually enter them on that window.
any ideas would be great, and i will past my code below as best as i can while protecting any personalized info.
<code>
'=========================================================================
' API Functions and Variables Declaration
'=========================================================================
Private Const INVALID_HANDLE_VALUE = -1&
Private Const TH32CS_SNAPPROCESS = &H2
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * 1000 'Increase this limit if there are more than 1000 Process
End Type
Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, PE32 As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, PE32 As PROCESSENTRY32) As Long
Private PE32 As PROCESSENTRY32
Private Proc_Name As String
Private hSnapshot As Long
Private iRet1 As Long
Private lRet As Long
'*************************************************************************
' Open PuTTY
'*************************************************************************
Sub PuTTY_Session()
Call Pre_Active_PuTTY_Sessions
End Sub
'*************************************************************************
' Check PuTTY Sessions Before Opening
'*************************************************************************
Sub Pre_Active_PuTTY_Sessions()
'Call Clear_Sheet
Set PuTTY_Col = New Collection
'Get Snapshop of All Process.
'To get details about Threads of each process, refer MSDN for additonal Parameters
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
If hSnapshot <> INVALID_HANDLE_VALUE Then
PE32.dwSize = Len(PE32)
lRet = Process32First(hSnapshot, PE32)
Do While lRet
iRet1 = InStr(1, PE32.szExeFile, VBA.Strings.Chr(0))
If iRet1 > 0 Then
Proc_Name = VBA.Strings.Left(PE32.szExeFile, iRet1 - 1)
If InStr(Proc_Name, "putty.exe") <> 0 Then
PuTTY_Col.Add PE32.th32ProcessID
End If
End If
lRet = Process32Next(hSnapshot, PE32)
Loop
CloseHandle hSnapshot
End If
Call Open_PuTTY
End Sub
'*************************************************************************
' Open PuTTY
'*************************************************************************
Sub Open_PuTTY()
Dim PT As String, PT_Log As String
Dim TaskID As Long
Select Case ActiveSheet.Name
Case "Main"
mySlam = Sheets("Main").Range("C9")
ipAdd = Sheets("Main").Range("C11")
Case "CLI"
mySlam = Sheets("CLI Info").Range("C6")
ipAdd = Application.WorksheetFunction.VLookup(Sheets("CLI Info").Range("C6"), Sheets("Data Tables").ListObjects("Dslams").Range, 3, False)
End Select
On Error Resume Next
DirFile = "C:\putty.log"
If Dir(DirFile) <> "" Then
Kill DirFile
End If
On Error GoTo 0
PT = "C:\Program Files\PuTTY\putty.exe -telnet -sessionlog " & _
"""C:\putty.log""" & " -P 23 " & ipAdd
TaskID = Shell(PT, vbNormalFocus)
Application.Wait Now + TimeValue("00:00:02")
Call Post_Active_PuTTY_Sessions
End Sub
'*************************************************************************
' Check PuTTY Sessions After Opening
'*************************************************************************
Sub Post_Active_PuTTY_Sessions()
Set PuTTY_Col2 = New Collection
'Get Snapshop of All Process.
'To get details about Threads of each process, refer MSDN for additonal Parameters
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
If PuTTY_Col.Count = 0 Then
If hSnapshot <> INVALID_HANDLE_VALUE Then
PE32.dwSize = Len(PE32)
lRet = Process32First(hSnapshot, PE32)
myRow = 2
myCol = 1
'Fetch Each Process Details one by one
Do While lRet
iRet1 = InStr(1, PE32.szExeFile, VBA.Strings.Chr(0))
If iRet1 > 0 Then
Proc_Name = VBA.Strings.Left(PE32.szExeFile, iRet1 - 1)
If InStr(Proc_Name, "putty.exe") <> 0 Then
myPuTTY = PE32.th32ProcessID
End If
End If
lRet = Process32Next(hSnapshot, PE32)
Loop
CloseHandle hSnapshot
End If
Else
If hSnapshot <> INVALID_HANDLE_VALUE Then
PE32.dwSize = Len(PE32)
lRet = Process32First(hSnapshot, PE32)
myRow = 2
myCol = 1
'Fetch Each Process Details one by one
Do While lRet
iRet1 = InStr(1, PE32.szExeFile, VBA.Strings.Chr(0))
If iRet1 > 0 Then
Proc_Name = VBA.Strings.Left(PE32.szExeFile, iRet1 - 1)
If InStr(Proc_Name, "putty.exe") <> 0 Then
PuTTY_Col2.Add PE32.th32ProcessID
End If
End If
lRet = Process32Next(hSnapshot, PE32)
Loop
CloseHandle hSnapshot
End If
Do Until PuTTY_Col2.Count = 1
For i = 1 To PuTTY_Col.Count
If PuTTY_Col(i) = PuTTY_Col2(1) Then PuTTY_Col2.Remove 1
Next i
Loop
myPuTTY = PuTTY_Col2(1)
End If
Set PuTTY_Col = Nothing
Set PuTTY_Col2 = Nothing
Call PuTTY_Login
End Sub
Sub PuTTY_Login()
AppActivate (myPuTTY)
SendKeys "ADMIN" & "{ENTER}" & "PASSWORD" & "{ENTER}"
Application.Wait Now + TimeValue("00:00:01")
Open "C:\putty.log" For Input As #1
Do Until EOF(1)
Line Input #1 , textline
'Text = Text & textline
Text = textline
Loop
Close #1
If InStr(Text, mySlam) <> 0 Then
AppActivate (myPuTTY)
SendKeys "en" & "{ENTER}" & "terminal length 80" & "{ENTER}"
Application.Wait Now + TimeValue("00:00:01")
Else
Call PuTTY_Login_Personal
End If
If ActiveSheet.Name = "Main" Then
Application.Wait Now + TimeValue("00:00:01")
AppActivate (myPuTTY)
SendKeys "menu" & "{ENTER}"
End If
Application.Wait Now + TimeValue("00:00:01")
Call KeyTest
Call KeyTest
End Sub
Sub PuTTY_Login_Personal()
Application.Wait Now + TimeValue("00:00:01")
If myUN = "" Or myPW = "" Then
AppActivate ("HSI 2.0.xlsm - Excel")
PuTTY_Info.Show
End If
If myUN = "" Or myPW = "" Then
MsgBox "Login Info input wrong.", vbCritical, "Error!"
Exit Sub
End If
Application.Wait Now + TimeValue("00:00:01")
AppActivate (myPuTTY)
SendKeys myUN & "{ENTER}" & myPW & "{ENTER}" ' & _
"terminal length 60" & "{ENTER}"
Application.Wait Now + TimeValue("00:00:03")
Open "C:\putty.log" For Input As #1
Do Until EOF(1)
Line Input #1 , textline
'Text = Text & textline
Text = textline
Loop
Close #1
Application.Wait Now + TimeValue("00:00:01")
If InStr(Text, mySlam) <> 0 Then
AppActivate (myPuTTY)
SendKeys "terminal length 80" & "{ENTER}"
Else
'If MsgBox("Is this info correct?" & vbCrLf & vbCrLf & " UN: " & _
myUN & vbCrLf & " PW: " & myPW, vbYesNo, "Your Info") = vbNo Then _
PuTTY_Info.Show
Call PuTTY_Login_Personal
Exit Sub
End If
End Sub
</code>