Expect - Examples

Examples

A simple example is a script that automates a telnet session:

# Assume $remote_server, $my_user_id, $my_password, and $my_command were read in earlier # in the script. # Open a telnet session to a remote server, and wait for a username prompt. spawn telnet $remote_server expect "username:" # Send the username, and then wait for a password prompt. send "$my_user_id\r" expect "password:" # Send the password, and then wait for a shell prompt. send "$my_password\r" expect "%" # Send the prebuilt command, and then wait for another shell prompt. send "$my_command\r" expect "%" # Capture the results of the command into a variable. This can be displayed, or written to disk. set results $expect_out(buffer) # Exit the telnet session, and wait for a special end-of-file character. send "exit\r" expect eof

Another example is a script that automates ftp:

# Set timeout parameter to a proper value. # For example, the file size is indeed big and the network speed is really one problem, # you'd better set this parameter a value. set timeout -1 # Open an ftp session to a remote server, and wait for a username prompt. spawn ftp $remote_server expect "username:" # Send the username, and then wait for a password prompt. send "$my_user_id\r" expect "password:" # Send the password, and then wait for an ftp prompt. send "$my_password\r" expect "ftp>" # Switch to binary mode, and then wait for an ftp prompt. send "bin\r" expect "ftp>" # Turn off prompting. send "prompt\r" expect "ftp>" # Get all the files send "mget *\r" expect "ftp>" # Exit the ftp session, and wait for a special end-of-file character. send "bye\r" expect eof

Below is an example that automates sftp, with password:

#!/usr/local/bin/expect -f #<---insert here your expect program location # procedure to attempt connecting; result 0 if OK, 1 otherwise proc connect {passw} { expect { "Password:" { send "$passw\r" expect { "sftp*" { return 0 } } } } # timed out return 1 } #read the input parameters set user set passw set host set location set file1 set file2 #puts "Argument data:\n"; #puts "user: $user"; #puts "passw: $passw"; #puts "host: $host"; #puts "location: $location"; #puts "file1: $file1"; #puts "file2: $file2"; #check if all were provided if { $user == "" || $passw == "" || $host == "" || $location == "" || $file1 == "" || $file2 == "" } { puts "Usage:

Read more about this topic:  Expect

Famous quotes containing the word examples:

    It is hardly to be believed how spiritual reflections when mixed with a little physics can hold people’s attention and give them a livelier idea of God than do the often ill-applied examples of his wrath.
    —G.C. (Georg Christoph)

    In the examples that I here bring in of what I have [read], heard, done or said, I have refrained from daring to alter even the smallest and most indifferent circumstances. My conscience falsifies not an iota; for my knowledge I cannot answer.
    Michel de Montaigne (1533–1592)

    Histories are more full of examples of the fidelity of dogs than of friends.
    Alexander Pope (1688–1744)