Autoexpect works by recording a shell session. How to Easily Create an Expect ScriptĪn excellent way of building an Expect script is to use a shell recorder program known as Autoexpect. Note: If you are having trouble with your script, try following it yourself and running it manually, making note of server responses to ensure that the anticipated responses you have in the script are accurate and that sent commands are error-free. Once the file is saved, remember to follow the instructions given above re modifying the permissions in order to render it executable. The entire script should therefore be: #!/usr/bin/expect -f At the end of your list of automated commands anticipate the user prompt and trigger an exit to close the session. send this point you should be met with the user prompt so that should be stated as the expected information: expect "$ "Īt this point you may write out any list of commands which you would like executed, along with the anticipated responses from the remote server. Additionally, sent commands must include a hard return ( \r)at the end, within the double quotes.Īssuming a password of the next line would be. Commands which are to be sent from the local computer are precluded by the send command and, like anticipated responses, must be enclosed in double quotes. You will then have to enter your password. This method may be used for many anticipated responses from the remote machine. This line would be edited as follows: expect "*ssword: " If the scripts expects a common ‘p’ then this may cause the script to fail. Note: Some servers will return “Password” with a capital ‘P’. As we said before, Expect scripts are comprised of dialog from both machines, hence anticipated responses must be included in the script as well.Īnticipated responses must be precluded by the expect command and contained in double quotes: expect "password: " When attempting to connect to a remote server via SSH you will be prompted for a password. In this example, since we are dealing with SSH, the ssh command is used to start a SSH session with a remote server, “ ”. The second line in your script uses the spawn command, which launches an external command. This requires changing permissions on the script file, allowing it to be executable.Įnter the following at the terminal to modify permissions on the file, making it executable. In this example we’ll save it as expscript.sh. The script must be saved as a shell file (. The –f switch is used to tell Expect that it will be reading commands from the file.The shebang (#!) Is required in order to specify Expect as the program used to interpret the script.The location of Expect must be included in the script.Īssuming that the location of Expect on your system is /usr/bin/expect enter the following as the first line in your Expect script: #!/usr/bin/expect -f In order to make the Expect script an independent executable program three steps must be fulfilled. In order to be run the script must be rendered executable. This means that you must write the commands which you wish to send in the session and the responses anticipated from the remote server. You will need to write both ends of the “dialog”. This may take some time but once it’s installed Expect may be run from the terminal. If you’re using CentOS, enter the following: # yum install expect-devel If you’re using Ubuntu, enter the following string at the command line prompt to install Expect: $ apt-get install expect Use the package manager to download and install Expect along with any associated packages and/or dependencies. Setting Up ExpectĮxcept is available as an installable software package for most Linux distros. A common use of Expects scripts is to automate SSH sessions between a local computer and a remote server. In an Except script, dialog from both sending and receiving computers is scripted. The Expect script is the entire automated two-way conversation. This process is repeated until termination of the script. When anticipated responses are received, you enter another command. You type in commands to be delivered and responses to anticipate (or expect). It allows you to emulate each keystroke in a session. In this tutorial, tasks will be automated using Expect.Įxpect is an automation scripting language.
0 Comments
Leave a Reply. |