If you’re getting into automation, Bash scripting is usually the way to go. However, there are a couple of limitations, and one of them is logging into another device like a Raspberry Pi and running a script automatically. To help in those situations, we’re going to automate delivering an SSH payload with an “expect” script.
Bash scripting is more like duct-taping things together, so it isn’t able to do everything, including predicting and then reacting to certain variables. So we’re going to use an “expect” script to log in to our Raspberry Pi and automatically shut it down. We could also adapt the script to pass pretty much any payload we want to the Pi.
It’s a pretty awesome use of an expect script, and there are lots of different things you can do with it, so know that our guide is just scratching the surface of how you can take an expect script and use it to actually do things that otherwise would require a lot more user interaction.
To follow along, you’ll need a Linux computer running something like Kali or Ubuntu — even a Mac will work. Also, you’ll need to have the arp-scan and expect tools installed. On Kali, you can do that with apt install arp-scan expect, and you’ll be set.
Step 1Create the Expect Script
Expect scripts are usually used in conjunction with Bash scripts to automate certain things like scanning a network or delivering a payload. Because the two work so well together, it’s possible to automate all sorts of interesting things. Here, we’re going to be knocking any Raspberry Pis on the network using default credentials off the network.
Expect is a unique scripting language that emulates keystrokes by responding to expected responses from a local or remote system. Think of Expect as an automated, virtual you.
The expect.exp script we’re showing off is incorporated into our trigger.sh script, so we need to create it first. If you want, start a new directory (mkdir) and nano into expect.exp to start the script draft. Then, copy and paste the script seen below into it, hit Control-X to exit, Y to add it to buffer, and Enter to save the file.