Thursday, June 12, 2014

Connect to Putty, Execute Commands, Close putty from QTP

To connect to Putty from QTP, use the below functions. Change your Putty path and Login credentials and use this file.

Note: Please add the below objects to Object repository before run the below script. 

'Start and logging into Putty
Public Function startPutty()
       

Dim SettingsFile,strHost,strLoginUser,strPassword,strPort, strPuttyPath
     strHost="10.111.12.13"
     strLoginUser="LinuxUser1"
     strPassword = "Password1"
     strPort=22
     strPuttyPath="C:\Program Files\PuTTY\putty.exe"

InfoLog "Starting Putty"

SystemUtil.Run strPuttyPath, "", "", "" 
       Wait(2)
Window("PuttyConfig").WinRadioButton("SSH").Set
       Wait(2) 

Window("PuttyConfig").WinEdit("HostName").Set strHost
       Wait(1)
Window("PuttyConfig").WinEdit("Port").Set sPort

Window("PuttyConfig").WinButton("Open").Click
      Wait(3)

Window("PuTTY").Type sLoginUser
Window("PuTTY").Type micReturn
     Wait(3) 

Window("PuTTY").Type sPassword
Window("PuTTY").Type micReturn
     Wait(2)

End Function

'Execute few commands after logging into putty
Public Function  executeCommandsPutty()   Dim sHost,sLoginUser,sPassword,sPort,sCommand
   'Call to StartPutty() Function if you execute this function first

   startPutty() 
   Window("PuTTY").Type "cd ./JEE/MyProduct/applications/Scripts"
   Window("PuTTY").Type micReturn
       wait (4)

  strParameter1="Value1"
  strCommand="./myShellScript.sh "&strParameter1&""
  Window("PuTTY").Type strCommand
  Window("PuTTY").Type micReturn

End Function


'To close the Putty
Function closePutty()
    'This Closes the Putty Window.
    If window("PuTTY").Exist(0) Then
        Window("PuTTY").Close
        Window("PuTTY").Dialog("PuTTY Exit Confirmation").WinButton("OK").Click
    End If

End Function

1 comment: