A small collection of maybe helpful examples of how to use expect with Bash. This is a very useful tool, especially when writing a better script is just too much work, but the funky syntax gets me every time. I have various expect examples scattered throughout this site, so this is more of an attempt at organizing them.

Generate SSH keys

Push SSH public key to node001-100

Execute an arbitrary command on remote server via passwordless SSH

In this example, the uptime command will be executed on all nodes, but the “cat /etc/issue” command will only be executed on node030. Of course, this is not the right way of doing something like that – just an example.

Another example similar to the previous. In this case, we’re reading the list of hosts from a file and, after login, execute sudo su - and count the number of entries in the /etc/shadow file:


Leave A Reply

Please enter your comment!
Please enter your name here