VBA excel 2010 how to initiate ssh connection from a list of ip address and send credential and write result of connection and credential in 2 cell

legethi

New Member
Joined
Nov 6, 2014
Messages
1
Hello, everyone.

I need help on doing an automation of connectivity and credential test with Excel vba code. For this I have a list of IP address which will be in a column let's say B. In column C I would like to provide the connection test result and in column D I would like to provide the authentication test result as per below


  • I would like to automate connection via ssh based on an IP address list 1 by 1

  • initiate connection to the server and if ok, in column C write status "ok" and then send credential, if no answer, stop and go to next cell IP address.

  • If credential is ok, in Column D set the value to "ok" and then proceed to next column. If Credential are wrong set column D to "Nok" and go to next cell with new IP@

I have found on some other forum to possibility that I have test.
The first code look like this:

Code:
Sub Button1_Click()
    Dim wSheet As Worksheet
    Dim wsh As Object
    Set wsh = VBA.CreateObject("WScript.shell")
    'Dim waitOnReturn As Boolean: waitOnReturn = True
    Dim waitOnReturn As Boolean: waitOnReturn = Wait
    Dim WindowStyle As Integer: WindowStyle = 1
    Dim errorCode As Long
    Dim fname As Variant
    Dim FileFormatValue As Long
    Dim login As String
    Dim password As String
    Dim protect As String
    Dim IPAdd As String
    Dim rng As Range, cell As Range
    
    Set rng = Range("B2:B7")
    For Each cell In rng
        IPAdd = cell.Value
        errorCode = wsh.Run("plink " & IPAdd & " -P 22 -l admin -pw Password", WindowStyle, waitOnReturn)
        If errorCode = 0 Then
           MsgBox "Connection to Device Done!"
        Else
           MsgBox "Connection to VSM Sharepoint Failed"
        End If
    Next cell
    
End Sub

This code work correctly on 1 by 1 but need manual operation to close the putty or plink application before going to the next IP@


My second code that I try look as below:

Code:
Option Explicit

Public Const WM_CLOSE = &H10
Public Const INFINITE = -1&
Public Const SYNCHRONIZE = &H100000
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000



#If VBA7 Then
    Declare PtrSafe Function lbf_ShellExecute Lib "shell32" Alias "ShellExecuteA" (ByVal hwnd As Long, _
    ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _
    ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Declare PtrSafe Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
    Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Declare PtrSafe Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Declare PtrSafe Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long
    Declare PtrSafe Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

#Else
    Declare Function lbf_ShellExecute Lib "shell32" Alias "ShellExecuteA" (ByVal hwnd As Long, _
    ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _
    ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Public Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long
    Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
#End If

Function TerminateTunnel()

Dim lngHandle As Long
Dim lngResult As Long

lngHandle = OpenProcess(SYNCHRONIZE Or STANDARD_RIGHTS_REQUIRED Or &HFFF, False, GetWindowHandle("Plink.EXE"))
lngResult = TerminateProcess(lngHandle, 0)
lngResult = CloseHandle(lngHandle)

End Function
Function GetWindowHandle(strWindowName As String) As Long
Dim w As Object
Dim sQuery As String
Dim objAllProcesses As Object
Dim objProcess As Object

    Set w = GetObject("winmgmts:")
    sQuery = "SELECT * FROM win32_process"
    Set objAllProcesses = w.execquery(sQuery)

    For Each objProcess In objAllProcesses
        If objProcess.Name = strWindowName Then
            'Once you get the handle, you cannow exit to function and return the handle
            GetWindowHandle = objProcess.Handle
            'Debug.Print process.Name, process.Handle, process.Caption
            GoTo EXitThisFunction
        End If
    Next
    
EXitThisFunction:

    On Error Resume Next
    Set w = Nothing
    Set objAllProcesses = Nothing
    Set objProcess = Nothing
    
End Function


Function CreateSSHTunnelUsingPutty()

Dim strFilename As String, strCommandLine As String
Dim strCurrPath As String
Dim lngWindowHandle As Long
Dim strServerPOrt As Long
Dim strServerUser As String
Dim strServerPassword As String
Dim IPAdd As String
Dim rng As Range, cell As Range
    
strServerPOrt = 22 'Sample POrt
strServerUser = "admin" 'Sample User Name
strServerPassword = "Password"  'Sample Password

'Set rng = Range("B2:B7")
For Each cell In ActiveSheet.Range("B2:B7")
IPAdd = cell.Value
'strCurrPath = CurrentProject.Path & "\"
strFilename = "Plink.exe"
strCommandLine = IPAdd & " -P " & strServerPOrt & " -l " & strServerUser & " -pw " & strServerPassword

'The command line will look something like this
'  "Server_IP_address -P 22 -l root -pw MyPassword"

'Debug.Print strFilename
'Debug.Print strCommandLine

'Launch tunnel
Call lbf_ShellExecute(0, "open", strFilename, strCommandLine, "", 1)
'Change the last parameter from a one to a zero and the
'connection window will be hidden.

Sleep (1000)
'SetForegroundWindow (GetWindowHandle("Plink.EXE"))

'Send a 'n' (no) which means trusted certificate is not stored on users PC)
SendKeys "n + {ENTER}", True
SendKeys "{ENTER}", True
SendKeys "{ENTER}", True
SendKeys "{ENTER}", True
SendKeys "exit", True
SendKeys "{ENTER}", True
SendKeys "{ENTER}", True

'Tunnel is now created
Next cell

End Function

With this code, I can automatically send the credential, leave the application. Issue that I face with this code is that it doesn't wait end of the putty session is finished before going to the next IP@

Any help on this will be strongly appreciate. I'm fully open also for some other way to do what I'm trying to reach, not necessary to use putty or plink.
Thanks in advance
Kind Regards
Thibaut
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,214,788
Messages
6,121,577
Members
449,039
Latest member
Arbind kumar

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