Expect ====== Automating repetitive task is a norm in the life of a system administrator.To do so, they write scripts in different programming languages. The default choice for most Linux admins is to use the bash shell. Unlike Tcl, Python, Ruby and Perl, Bash is not a full fledged programming language. It sure does provide a reasonable amount of programming features to get many jobs done, such as conditional statements, a case construct , and other lot of built in features to make decisions. Since it is not a full fledged language, you have to use other utilities to accomplish more complex tasks. Conceptually a script is series of commands provided by different programs arranged in a file that, once executed, achieves an end result. Some programs are interactive in nature, such as changing a user password in Linux. This requires the end user to type in the new password twice to verify, because the input is not echoed. Having to set up and configure a number of user accounts on a new system manually takes time, and may need to be automated. Tasks of this interactive nature, where commands expect input which cannot be provided by a "here" script, can be automated using expect. Expect is based on Tcl (Tool Command Language), developed by John Osterhout starting in early 1989. On Raspberry Pi OS, install it using the usual 3-step, as root, or using sudo: apt update -y apt upgrade -y apt install -y expect Expect extends Tcl by adding commands to spin off other processes, send data to them, which these receive on their standard input, and receives their output. This mechanism has been around for a long time, in form of chat scripts, used, for example, by UUCP, to configure a modem, dial a number, and, depending on status messages received (such as "NO DIALTONE", "BUSY", "Login:", "Password:") error out, retry, or provide a user name and password. One get easily get by with just five expect commands: - spawn starts a child process, such as: spawn ssh some.where - expect consumes output from the child process until a specific character sequence is found: expect "assword:" - send sends a string to the child process: send "my password\r" - close shuts down the connection to the child process - wait retrieves the child process's status - log_user turns display of communication between expect and its controlled process on (log_user 1) and off (log_user 0). The default timeout to wait for a command's response is 10 seconds. To change that, set the "timeout" variable to the amount of seconds to wait, or to -1 (negative one) to turn off timeouts. The expect keyword might be invoked to invoke just one, or one of many possible strings, provided in a format similar to Tcl's switch statement: expect { this { ... do something if we got "this" } that { ... do this if we got "that" } timeout { ... we get here if there's a timeout } eof { ... should the client hang up on us, we wind up here } } To wait for more of the named sequences in an expect block, invoke "exp_continue", which behaves similar to a for- or while-block's "continue" statement. There is no exp_break. While one could write an expect script from scratch, it might be easier to have one written using a recording of a sample session. To create a recording, invoke "autoexpect" to start a shell whose input and put are recorded to script.exp, then edit script.exp to remove code that might be session-specific, such as hostnames. https://www.tcl.tk/man/expect5.31/expect.1.html https://www.slashroot.in/expect-command-tutorial-linux-example-usage https://likegeeks.com/expect-command/ https://wiki.tcl-lang.org/page/Expect+Tutorials https://www.oreilly.com/library/view/exploring-expect/9781565920903/ch01.html https://person.hst.aau.dk/magnus/EDB/expect_tutorialnotes3.pdf https://docs.oracle.com/cd/E35328_01/E35336/html/vmcli-script.html