Task to be Automated
Below shows an interactive session that psm — an Oracle Application Container Cloud Service command line tool—prompted a user for needed authentication and authorization information before he/she can sign in to an Oracle Cloud service and work on a specific identity domain.
Username: weblogic1
Password:
Retype Password:
Identity domain: myIdDomain
Region [us]: http://anycloudserver.example.com:7103
Output format [json]:
In this article, we will demonstrate how to utilize Expect to automate the above task , but not focus on the correctness of the information needed by psm.
Expect is an extension to the Tcl scripting language that "talks" to other interactive programs according to a script.[1] Following the script, Expect knows what can be expected from a program and what the correct response should be.
It can be used to automate control of interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others including psm . Expect uses pseudo terminals (Unix) or emulates a console (Windows), starts the target program, and then communicates with it, just as a human would, via the terminal or console interface. Finally, Tk, another Tcl extension, can be used to provide a GUI.
To automate the said task, we need to write an Expect script (i.e., psmSetup.exp) as shown below:
psmSetup.exp
#!/usr/bin/expect -f
#exp_internal 1
set argDomain [lindex $argv 0]
spawn psm setup
expect "Username: "
send "weblogic\r"
expect "Password: "
send "welcome1\r"
expect "Retype Password: "
send "welcome1\r"
expect "Identity domain: "
send "$argDomain\r"
expect "Region \\\[us\\\]: "
send "http://anycloudserver.example.com:7103\r"
expect "Output format \\\[json\\\]: "
send "\r"
expect "\r"
spawn psm accs apps
expect "\r"
Expect scripts can have any file name suffix you like, though they generally have an .exp extension. Read [5] for more details.
Writing an Expect script the first time, it is easy to be completely lost and not getting the result you expect. In this case, un-comment the following line in psmSetup.exp:
Setting "exp_internal 1" at the beginning of an Expect script is similar to -d flag (When using Expectk, this option is specified as -diag.), which enables some diagnostic output. This primarily reports internal activity of commands such as expect and interact. In addition, the strace command is useful for tracing statements, and the trace command is useful for tracing variable assignments.
Say, if you write a Korn shell script as below:
To reference the Expect variable (i.e, argDomain), you use prefix "$" as below:
In the above psm dialog, it depicts the interaction between a sender (i.e., end user) and a receiver (i.e. psm) as:
In the below dialog,
which includes some special characters "[" and "]". To escape special characters in Expect, you can use backslash. However, to protect backslash from being substituted, you actually need to use "\\\" in front of both "[" and "]":[9-12]
Below shows an interactive session that psm — an Oracle Application Container Cloud Service command line tool—prompted a user for needed authentication and authorization information before he/she can sign in to an Oracle Cloud service and work on a specific identity domain.
$ psm setup |
Expect
Expect is an extension to the Tcl scripting language that "talks" to other interactive programs according to a script.[1] Following the script, Expect knows what can be expected from a program and what the correct response should be.
It can be used to automate control of interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others including psm . Expect uses pseudo terminals (Unix) or emulates a console (Windows), starts the target program, and then communicates with it, just as a human would, via the terminal or console interface. Finally, Tk, another Tcl extension, can be used to provide a GUI.
Expect Script
To automate the said task, we need to write an Expect script (i.e., psmSetup.exp) as shown below:
psmSetup.exp
How to Debug Expect Scripts?
Writing an Expect script the first time, it is easy to be completely lost and not getting the result you expect. In this case, un-comment the following line in psmSetup.exp:
#exp_internal 1
How to Pass Variables from Shell Script to Expect Script?
Say, if you write a Korn shell script as below:
#!/bin/ksh ... ./psmSetup.exp $domain
and would like to pass $domain from shell script to psmSetup.exp script, you can add the following line to the Expect script:
set argDomain [lindex $argv 0]
To reference the Expect variable (i.e, argDomain), you use prefix "$" as below:
expect "Identity domain: " send "$argDomain\r"
In the above psm dialog, it depicts the interaction between a sender (i.e., end user) and a receiver (i.e. psm) as:
"Identity domain: " is the prompt you "expect" from the psm; therefore, you enter that expected response (i.e., "$argDomain\r") by using "send".Read [7] for more explanation.
How to Escape Special Characters?
In the below dialog,
Region [us]: http://anycloudserver.example.com:7103
psm will prompt the user for a response:
Region [us]:
expect "Region \\\[us\\\]: " send "http://anycloudserver.example.com:7103\r"
References
- Expect User Command
- Tcl
- How to pass variables from shell script to expect script?
- How to write a script that accepts input from a file or from stdin?
- Using Expect Scripts
- Debugging Expect Programs
- Using Expect Scripts to Automate Tasks
- How to escape unusual/uniq characters from expect scripts?
- Passing '\' In Username To Expect
- Problem in expect script with password involving trailing backslash
- How to send escape characters through Expect
- How to escape unusual/uniq characters from expect scripts?
- Oracle Application Container Cloud Service
- Introduction to the Oracle VM Command Line Interface (CLI)
- All Cloud-related articles on Xml and More
- Tcl Commands (Tcl 8.4)
- New Regular Expression Features in Tcl 8.1
- Understanding Login Authentication
1 comment:
It is very use full content, thank you so much
Python Training
Python Online Training
Post a Comment