Techno Logica


Thursday, December 4, 2008

Automate Windows Command Prompt : Telnet Login using QTP

The Below Function lets you Login to any Telnet Server  and then Exit. You can Enter the Commands that you would like to run, enter in between the login and exit code. There is a lot of care to be taken for syncronization. You will see a lot of Loops and Call Waits, Which is required for the syncronization. 
Note: You will Need to Add the Telnet Window in your Object Repository.This object needs to use the regular expression for the property ‘regexpwndtitile’ so that the telnet window object can be applied to any telnet instance


'Fucntion Call 
Func_Telnet "127.0.0.1","root","chnagemenow”,”Linux” 

'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
' Name                 :           Func_Telnet
'Purpose               :           Telnet Login 
‘Pre-Condition    :           Telnet service should be up & running in the Server 
'Parameters        :           sServer - Server Name/IP eg. '192.128.1.27'
'                                                               sLoginID  - Server Telnet Login ID eg. 'root'
'                                                               sLoginPwd - Server Telnet Login Password eg. password'
'                                                               sOSType -  Server OS Type : Windows / Linux / AIX  eg. "Linux"
'Return Value      : Nil
'Usage                   : Func_Telnet sServer, sLoginID, sLoginPwd, sOSType
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Function Func_Telnet(sServer, sLoginID, sLoginPwd, sOSType)
            Dim i, Str
                        
            'Open Telnet Window using the Server Address
            SystemUtil.Run "telnet",sServer,"C:\",""

            If   Window("TelnetWindow").Exist Then
                        Reporter.ReportEvent micPass, "Verify Telnet Window is Displayed", "Telnet Window is Displayed Successfully"
            Else
                        Reporter.ReportEvent micFail, "Verify Telnet Window is Displayed", "Telnet Window is NOT Displayed"
                        Exit Function 
            End If
            
            'Activate Telnet Window
            Window("TelnetWindow").Activate

            Call Wait(10)
            
            'Error Handling -  Incase the Server does  not have the telnet service running or Incase the Server reponse is slow
             Str =  Window("TelnetWindow").GetVisibleText
             i = 0     ' This is a counter to Check the Number of Iteration of the Loop
             
            Do While (Instr(Str, "Connecting to") > 0)
                        If i <>
                                    msgbox Str
                                    Call Wait(60) ' Wait for connecting to server
                                    If Window("TelnetWindow").Exist(1) Then
                                                Str =  Window("TelnetWindow").GetVisibleText
                                    Else
                                                Reporter.ReportEvent micFail, "Verify the Telnet window is Connected", "Telnet Window was not able to connect to the Server"
                                                Exit Function
                                    End If
                        Else
                           Reporter.ReportEvent micFail, "Verify the Telnet window is Connected", "Telnet Window was not able to connect to the Server"
                                    Exit Function
                        End If
                        i = i + 1
            Loop

            
            '#Check if the Server OS is Windows           - The Below Code is Specific to Windows System
                        If Lcase(sOSType)="windows"  Then
                                                'Check if - " You are about to send your password information to a remote computer in Internet zone. This might not be safe. Do you want to send anyway(y/n): "- Message is Displayed
                                                Str=""
                                                i=0                    ' This is a counter to Check the Number of Iteration of the Loop
                                                 Do While (Instr(Str,"Do you want to send anyway(y/n):") = 0)
                                                                        If i<10>
                                                                                    'Call Wait As it take some time for the window to display data
                                                                                    Call Wait(2)
                                                                                    Str =  Window("TelnetWindow").GetVisibleText
                                                                        Else
                                                                                    Exit Do 'Exit the Loop if the Counter reaches the Max Count
                                                                        End If
                                                                        i = i+1 'Increment The Counter
                                                Loop
                                                If  (Instr(Str,"Do you want to send anyway(y/n):") = 0) Then
                                                            Reporter.ReportEvent micPass, "Verify Warning Message is Displayed", "Warning Message is Displayed : 'You are about to send your password information to a remote computer in Internet zone. This might not be safe. Do you want to send anyway(y/n): '"
                                                            'Type 'n' & Press Enter
                                                            Window("TelnetWindow").Type "n" & micReturn
                                                Else
                                                            Reporter.ReportEvent micWarning, "Verify Warning Message is Displayed", "Warning Message is NOT Displayed"
                                                End If
                        End If
                        
            '# Below Code is Common to Both Windows / Linux / AIX

            '!!!!!!!!!!!!!!! Login to Server !!!!!!!!!!!!!!!!!!!!!!

            'Check If the 'login:' field is displayed        
            Str=""
            While (Instr(Str,"login:") = 0)
                        Call Wait(2)
                        Str =  Window("TelnetWindow").GetVisibleText
            Wend
            'Enter the Login ID & Press Enter
            Window("TelnetWindow").Type sLoginID & micReturn
            
            'Check If the Password: field is displayed
            Str=""
            While (Instr(Str,"Password:") = 0)
                        Call Wait(2)
                        Str =  Window("TelnetWindow").GetVisibleText
            Wend
            'Enter the Login Password & Press Enter
            Window("TelnetWindow").Type sLoginPwd & micReturn
            
            '#Check if the Server OS is Windows - This Check is done because if the OS Type is Windows we get '>' symbol in the prompt WhereAs in Linux/AIX  we get '#' symbol 
            If Lcase(sOSType)="windows"  Then
                        'Check for the > Prompt - Windows
                        Str=""
                        i=0
                        Do While (Instr(Str,">") = 0)
                                    If i<6>
                                                Call Wait(2)
                                                Str =  Window("TelnetWindow").GetVisibleText
                                    Else
               Exit Do
                                    End If
                                    i=i+1
                        Loop
                        'Verify login was Successful
                If Instr(Str,"Login incorrect") > 0 Then
                                    Reporter.ReportEvent micFail, "Verify login was Successful", "Login was not Successful."
                                    Exit Function
                        Else
                                    Reporter.ReportEvent micPass, "Verify login was Successful", "Login was Successful."
                        End If
            Else
                        'Check for the # Prompt  - Linux / AIX
                        Str=""
                        i=0
                        Do While (Instr(Str,"#") = 0)
                                    If i<6>
                                                Call Wait(2)
                                                Str =  Window("TelnetWindow").GetVisibleText
                                    Else
                                                Exit Do
                                    End If
                                    i=i+1
                        Loop
                        'Verify login was Successful
                         If Instr(Str,"Login incorrect") > 0 Then
                                    Reporter.ReportEvent micFail, "Verify login was Successful", "Login was not Successful."
                                    Exit function
                        Else
                                    Reporter.ReportEvent micPass, "Verify login was Successful", "Login was Successful."
                        End If
            End If

‘!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Exit Telnet Window !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            'Enter exit  & press Enter
            Window("TelnetWindow").Type "exit" & micReturn

            'Check if Telnet window is Closed; Else Wait for 5 Seconds.
            Call Wait (5)
            If Window("TelnetWindow").Exist(3) Then
                        Window("TelnetWindow").Close
                        Call Wait (5)
            End If
            
    'Clear Data
            Set i = Nothing
            Set Str = Nothing
End Function

'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

3 comments:

Happy Himanshu Gupta said...

This is simply fantastic Swapnadeep...it worked for me...

Great work man !! Thanx

Unknown said...

Hi,

Can u pls lemme know if i can execute Unix jobs through this QTP script?

Also may i know if there is a way to execute the sql queries like update,insert,select from qtp.Db is Oracle.If yes,pls get me a sample code.

Thanks

Unknown said...

Hi,

Can u pls lemme know if i can execute Unix jobs through this QTP script?

Also may i know if there is a way to execute the sql queries like update,insert,select from qtp.Db is Oracle.If yes,pls get me a sample code.

Thanks