1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_CHAR As Long = &H102
Private Sub PostText(hWnd As Long, Text As String)
Dim i As Integer, ret As Long
For i = 1 To Len(Text)
ret = PostMessage(hWnd, WM_CHAR, Asc(Mid(Text, i, 1)), 0&)
Next
End Sub
Private Function RunPutty(ServerName As String, UserName As String, PassWord As String) As Long
'Permet de lancer Putty avec une connexion ssh
'Retourne l'HWND de putty pour un "PostMessage" ulterieur
Dim strPutTY As String
strPutTY = "C:\AdressePutty\PUTTY.EXE"
Dim aCommande As String
aCommande = "-ssh -pw " & PassWord & " " & UserName & "@" & ServerName
Call Shell(strPutTY & " " & aCommande)
Call TimeControler(5)
RunPutty = FindWindow("PuTTY", vbNullString)
End Function
Private Sub TimeControler(Optional iTime As Single = 1)
Dim lTime As Single
lTime = Timer
Do
DoEvents
Loop While Not Abs(Timer - lTime) > iTime
End Sub
'********************
Public Sub Run_PuttyServices(ServerName As String, UserName As String, PassWord As String, CurDir As String, File_sh As String, OutPutName As String)
'Exemple d'utilisation
Dim hWnd As Long
hWnd = RunPutty(ServerName, UserName, PassWord)
Dim aCmd As String
aCmd = "cd " & CurDir & " && " & File_sh & " >> " & OutPutName & " && rm " & OutPutName & " && exit"
Call PostText(hWnd, aCmd & vbCrLf)
Call TimeControler
End Sub |