SFTP Connection with UFT/VBS

6+
Share

SFTP Connection with UFT/VBS


During automation tasks sometimes, we need to Upload and Download files from and to server using SFTP or FTP. This blog will help you to perform such automation tasks.


Pre-requisite

We will be demonstrating the automation process to interact with server over SFTP and FTP using WinSCP software. So, following software/files are pre-requisite for this task.

  1. WinSCP Software to view and connect server.
  2. WinSCP .NET assembly

Registering WinSCP for COM

We need to register WinSCP .NET Assembly for COM prior to use. Follow the following steps to register WinSCP for COM:

  1. Download and Extract the WinSCP .NET assembly from “https://winscp.net/download/WinSCP-5.13.1-Automation.zip”.
  2. Copy the file extracted in step 1 to following location.
    C:\Windows\Microsoft.NET\Framework\<version>\”If you are using 64-bit OS, then copy the same files to the following location too.
    C:\Windows\Microsoft.NET\Framework64\<version>\Replace “<version>” with latest .NET framework on your system.So Actual path should like:

    C:\Windows\Microsoft.NET\Framework\v4.0.30319
    C:\Windows\Microsoft.NET\Framework64\v4.0.30319

Once above files have been copied to given location, then follow the following steps to register binary with COM.

  1. If you are using 64-bit operating system then you should register the assembly for Framework64 to use it for 64-bit Applications.
  2. Following command should be used in command prompt (with Admin Rights) to register assembly for COM:
    For 32-Bit Application:

     %WINDIR%\Microsoft.NET\Framework\<version>\RegAsm.exe WinSCPnet.dll /codebase /tlb:WinSCPnet32.tlb

    For 64-Bit Application:

     %WINDIR%\Microsoft.NET\Framework64\<version>\RegAsm.exe WinSCPnet.dll /codebase /tlb:WinSCPnet64.tlb

    Replace “<version>” with latest .NET framework on your system.

    So Actual Command should like:

     %WINDIR%\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe WinSCPnet.dll /codebase /tlb:WinSCPnet32.tlb

    For 64 Bit Application:

     %WINDIR%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe WinSCPnet.dll /codebase /tlb:WinSCPnet64.tlb


Upload and Download Server Files

For Server connectivity, you need some information about the server.

  1. HostName
  2. UserName
  3. Password
  4. PortNumber
  5. SshHostKeyFingerprint

Information for first Four items can be obtained from the admin and “SshHostKeyFingerprint”, you can get it from your system.

Open WinSCP software installed on your system and connect to your Server.

Once connected successfully, go to “Session->Server/Protocol Information”, and note “Server host Key fragments”.

 

Run Following code in UFT or VBS file to upload/download files from Server using sFTP/FTP

' Setup session options for WinSCP
Dim objSOptions
Set objSOptions = CreateObject("WinSCP.SessionOptions")
With objSOptions
 .Protocol = Protocol_Sftp
 .HostName = "test.rebex.net"
 .UserName = "demo"
 .Password = "password"
 .PortNumber = 22
 .SshHostKeyFingerprint = "ssh-ed25519 256 d7Te2DHmvBNSWJNBWik2KbDTjmWtYHe2bvXTMM9lVg4="
End With
 
Dim ObjSession
Set ObjSession = CreateObject("WinSCP.Session")
 
' Connect to the Session 
ObjSession.Open objSOptions
 
Dim directoryInfo
Set directoryInfo = ObjSession.ListDirectory("/pub/example/")
 
Dim file

' Get the file information
For Each file In directoryInfo.Files
 MsgBox "File Name: " &file.Name 
Next

'******************** 
' Upload Files
'********************

ObjSession.PutFiles("SourceLocalPath","DestinationServerPath")

'******************** 
' Download Files
'********************
 
 ObjSession.GetFiles("SourceRemotePath","DestinationLocalPath")
 
' Disconnect, clean up
ObjSession.Dispose

Related Posts

Post A Comment

Your email address will not be published. Required fields are marked *