Putty Automation // Open putty from different drive

ganesh_6663

New Member
Joined
Jul 15, 2021
Messages
33
Office Version
  1. 2016
Platform
  1. Windows
Need to open putty from "C:/D:/E:/F:" any of the given drive , without chaging path inside macro every time.


VBA Code:
Option Explicit

Public Sub test()

Dim PuttyPID As Double
#If VBA7 Then
Dim PuttyHwnd As LongPtr
#Else
Dim PuttyHwnd As Long
#End If
Dim serverName As String, username As String, password As String
 
serverName = "xxx.yyy"
username = "xxxx"
password = "xxxx"
 
PuttyPID = Shell("C:\Program Files\PuTTY\putty.exe -telnet  " & serverName, vbNormalFocus)   'CHANGE PATH TO .EXE
 
'Get window handle of the PuTTY Telnet command window

PuttyHwnd = GetWindowHandle(CLng(PuttyPID))
 
If PuttyHwnd <> 0 Then
     
SendChars PuttyHwnd, username & vbCr
Application.Wait DateAdd("s", 1, Now)
 
SendChars PuttyHwnd, password & vbCr
Application.Wait DateAdd("s", 1, Now)
 
SendChars PuttyHwnd, "DIR" & vbCr  'DIR command
 
End If
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
What about something like this? (not tested)

VBA Code:
Public Sub test()

    Dim PuttyPID As Double
    #If VBA7 Then
        Dim PuttyHwnd As LongPtr
    #Else
        Dim PuttyHwnd As Long
    #End If
    Dim serverName As String, username As String, password As String

    Dim DriveArr As Variant, PDrive As Variant
    Dim PPath As String, CurPath As String

    DriveArr = Array("C", "D", "E", "F")
    PPath = ":\Program Files\PuTTY\putty.exe"

    With CreateObject("Scripting.FileSystemObject")
        For Each PDrive In DriveArr
            CurPath = PDrive & PPath
            If .FileExists(CurPath) Then
                Exit For
            Else
                CurPath = ""
            End If
        Next PDrive
    End With

    If CurPath <> "" Then
        serverName = "xxx.yyy"
        username = "xxxx"
        password = "xxxx"

        PuttyPID = Shell(CurPath & " -telnet  " & serverName, vbNormalFocus)    'CHANGE PATH TO .EXE
       
        'Get window handle of the PuTTY Telnet command window
       
        PuttyHwnd = GetWindowHandle(CLng(PuttyPID))
       
        If PuttyHwnd <> 0 Then
       
        SendChars PuttyHwnd, username & vbCr
        Application.Wait DateAdd("s", 1, Now)
       
        SendChars PuttyHwnd, password & vbCr
        Application.Wait DateAdd("s", 1, Now)
       
        SendChars PuttyHwnd, "DIR" & vbCr  'DIR command

        End If
    End If
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top