PuTTY is a free and open source terminal emulator ,serial console and network file transfer.In general it is used to run the UNIX/Perl/Python and other commands. In this write up, we will see the automation approach for running the commands on PuTTY using our UI automation tool QTP/ UFT.

To understand this approach clearly ,lets take the scenario with below steps:

	Navigate to a webpage.
	Trigger Perl scrip on PuTTY and get the unique key(say Account ID)
	Insert the Account ID in the text box.

Now from automation point of view, we can easily automate the first step as object can be easily recognized. But when we try to identify PuTTY, it only recognize the object of the configuration screen and not the objects of the screens which opens after a PuTTY session on a host.

So the main challenge for us is how we perform the operation on the screen.This is not only the challenge that we have here ,the second challenge is how we wait between two inputs that needs to be given in the session probably as input or commands.To explain it in more efficient way ,let me list the steps that needs to be done for running the script after you have open a PuTTY session.

    Enter the username.
    Enter the password when prompted.
    Run the script.

Now in the above step, when we enter the username, PuTTY takes some time for prompting for password. So this needs to be handled in our script. For handling this issue,we can follow the below two approaches.

   Add a hard-coded wait in our script.
   Wait till the desired text does not come on the screen.

The first approach will lead to increase in the execution time due to hard coded delay even after the operation is finished while in second approach as soon as the text come on screen ,it will automatically move the cursor to next step without any delay.So second approach looks good here.

In second approach we talked about to wait till the desired text does not come on screen.Now the question is how we will come to know about the desired step.Don’t worry !!!.We have the solution for it to.Follow the below step on configuration screen and we will get the desired step.

Click on Logging tab under Session(Session---->Logging)
Select "All Session Output" option under Session Logging.
Click on" Browse" Button under  file name section.
Set a text file path into the Session log file name popup and click on Save button.
Select "Always overwrite it" option.
Click on Session again.
Enter the host-name under  "Host Name" session.
Enter the host-name under "Saved Session"
Click on Save button.

After following these steps you can see your session under saved session list-box.Now you only  need to select the session and click on load session button.

See also  Micro Focus/HPE Tools that work well with UFT

Please note : This part can be done manually if you are using one host-name in  your script else you can automate it .It’s your choice!!!.

Once these setting has been made ,now we move how we write into the PuTTY when objects are not getting identified.Here we will be using Sendkeys method.It is similar to that we type in PuTTY.Only we need to have active focus on PuTTY before we send the inputs using Sendkey method.

Windows("PuTTY").Activate

Set wshell=CreateObject("WScript.Shell")

wshell.Sendkeys "Automated-360"

delay 100

wshell.Sendkeys "{Enter}"

delay 100

Now once we entered the Username  input(“Automated-360” here),now we need to wait till the text(“Password”) appears.Below code snippet can be used for it .

Set objFSO=CreateObject("Scripting.FileSystemObject")

'For reading the log file

Set objFile=objFSO.OpenTextFile(Path of  log file,1,true)

'Intializing a counter

iCnt=0

'Loop for waiting till the desired text come

while instr(1,objFile.ReadAll,"Password")=0 and iCnt<10

wait 1

iCnt=iCnt+1

wend

In this fashion we can read the file and wait till the desired text does not appear.Only one thing needs to be taken care ,that the wait text should be unique in nature.

Here is complete code snippet for reference

'Code for saving the hostname along with the log file path.

'List of Putty Command to be executed

strPuttyCommand="Automated-360,*****,C:\Execute.pl"

'List of Wait Text

strPuttyWaitText="Password,1$,2$"

'Split the Putty Command

arrPuttyCommand=Split(strPuttyCommand,",")

'Split the Wait Text

arrPuttyWaitText=Split(strPuttyWaitText,",")

Set objFSO=CreateObject("Scripting.FileSystemObject")

for i = 0 to ubound(arrPuttyCommand)

Windows("PuTTY").Activate

Set wshell=CreateObject("WScript.Shell")

wshell.Sendkeys arrPuttyCommand(i)

delay 100

wshell.Sendkeys "{Enter}"

delay 100

'For reading the log file

Set objFile=objFSO.OpenTextFile(Path of  log file,1,true)

'Intializing a counter

iCnt=0

'Loop for waiting till the desired text come

while instr(1,objFile.ReadAll,arrPuttyWaitText(i))=0 and iCnt<10

wait 1

iCnt=iCnt+1

wend

Next

Hope this article will help your to achieve  PuTTY automation using QTP.

Happy Learning!!!

 

1 COMMENT

  1. Hi,

    I have automated putty script using UFT and run it in a remote machine. There are 2 parts to my script :
    Part 1: automate putty
    Part 2: check few URLs
    When the remote machine is locked or minimized, UFT runs and the URL checking works fine but putty does not work.
    The issue I am facing is that UFT launches putty but it is not able to type anything in the window or click on anything. I investiaged a little and found same behavior on command prompt. Nothing gets typed when the screen is not active.

    I need to schedule this task, so getting a work around for this issue is must. Any help is greatly appreciated.

    Thanks,
    Swaroop

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.