#!/usr/bin/expect # Set timeout for expect commands set timeout 30 # Open the log file log_file results.txt proc expectit {pattern} { expect { $pattern { return } timeout { puts "Error: Expected response '$pattern' not received." exit 1 } eof { puts "Error: Connection closed unexpectedly." exit 1 } } } # Start the SSH session # spawn ssh -p 4004 user@howardabrams.com # Handle the password prompt # expectit "password:" # send "\r" ; # Send an empty password # # or spawn telnet localhost 4000 # Handle the prompt after logging in expectit "After you've logged in Enter" send "connect guest beourguest\n" # That should be enough of the birdie ... expectit "perches on your shoulder" send "shoo\n" # Send the time command and handle the response expectit "Have fun, and I'll leave you here" send "time\n" expect { "in the evening" { set phase "evening" } "in the afternoon" { set phase "afternoon" } "in the morning" { set phase "morning" } # What about "at night"? timeout { puts "Error: Expected time response not received." exit 1 } } send "look\n" # Expect different responses based on the phase variable switch $phase { "evening" { expect "darkening twilight" } "afternoon" { expect "lazy afternoon" } "morning" { expect "awakening dawn" } default { puts "Error: Unexpected phase value '$phase'." exit 1 } } send "look trees\n" expectit "You feel dwarfed by colossal trees" send "jump\n" expectit "You jump in the puddle!" send "get stick\n" expect { "Could not find 'stick'." { # Print warning in red # send_user "\033[31mNo stick, check the stick script.\033[0m\n" send_user "No stick, check the stick script.\n" } "You pick up a stick." { # If stick is found, throw it send "throw stick\n" expect "it comes back to you" send "drop stick\n" expect "You drop a stick." } timeout { puts "Error: Expected response for 'get stick' not received." exit 1 } } send "read book\n" # expect -re "You open the leather-bound book, and begin to read\[.\n\r\]*For repayment of a boon, The Summer Queen" expectit "You open the leather-bound book, and begin to read" expectit "For repayment of a boon, The Summer Queen" expectit "revious" # quit reading the book: send "q\n" send "look\n" expect { -re "Exits:.*" { # Store the matched line in a variable set matched_line $expect_out(buffer) # Check if the matched line contains "boulder" if {[string match boulder $matched_line]} { puts "Error: The line contains 'boulder'." exit 1 } else { puts "The line is valid and does not contain 'boulder'." } } timeout { puts "Error: Expected line starting with 'Exits:' not received." exit 1 } } send "look boulder\n" expectit "You notice a foot hold" send "look\n" expect -re "Exits:.*climb.*boulder" expect { -re "You notice.*" { # Store the matched line in a variable set matched_line $expect_out(buffer) # Check if the matched line contains "boulder" if {[string match vines $matched_line]} { puts "Error: The line contains 'vines'." exit 1 } else { puts "The line is valid and does not contain 'vines'." } } timeout { puts "Error: Expected line starting with 'Exits:' not received." exit 1 } } send "look vines\n" expectit "Strong vines twisted to look like rope" send "look\n" expect -re "You notice.*vines" send "climb boulder\n" expectit "You grab hold of some" # And once on top... expectit "Still, a nice view." send "sit moss" expectit "You sit on the patch of moss." # We can't get this stuff: send "get moss" expectit "The moss is a bryophyte" # Close the log file log_file sleep 1 send "quit\n" # Handle the connection closed message without logging it expect { "Connection closed by foreign host" { # Ignore this response exp_continue } eof { # End the session return } }