Once again, using this script in one of the classrooms, I looked for materials and found that I hadn’t been thinking about expect here for a long time. This is a great alternative command line interpreter for Linux that can communicate with it instead of a live person, and I will add here just one more example of its use.

There are no pictures on this subject, and there will be no article in the article at all, so we will draw your attention to the cover of a wonderful book
')
A bit of history and offtopic
At the beginning of 2013, I returned to additional education for children, received 24 hours of weekly workload for four groups of “young programmers” and “young system administrators”, and started recruiting students at two sites: my own office and the computer science office at a gymnasium. With the help of the “young system administrators” groups, we put our office in order by deploying AltLinux 6 and a couple of alternative distributions. And in the gymnasium, a very experienced “computer class engineer” has long conducted experiments with source-based, which ended with the arrival of Calculate Linux on all machines of teachers and training classes. Respect him)
Task
After a couple of months of classes, I saw a problem. The children refused to leave classes, in any way trying to stay longer. Since Pedagogical technologies are not mine, but our circle is still technical, I killed third-party processes and turned off machines by ssh. This has increased pupils ’interest in exploring command line capabilities. However, they soon found a bug: switching off with “hands” takes too much time, and even in a small office they will have time to restart half of the cars before I complete the punitive process, and this will delay the matter. To demonstrate
who is here the main best command line programming features, I thought about automation.
Solution options
Zero thought was the use of
italc , and we even spent several weeks setting it up and experimenting. But he was too buggy and primitive, so he was safely forgotten.
The first thought was to set up access via ssh on the fingerprint. This would reduce the task to one cycle, but for the application “on the knee” this method turned out to be not quite suitable (although why not? EMNIP, an imprint for the user can also be created without root). But with the available password authorization (by the way, a great damage to security for the training classes, where the login-passwords are the same) I was interested in the option with automatic password authorization. After spending an hour searching, I found some examples for expect.
Decision through expect
Before the start
But first, it must be installed. In my class, we easily did it, but in the classroom of the gymnasium EXTREMELY, it turned out that both expect, and nmap, and some other interesting system utilities are not only installed, but also accessible to the student. This helped us a lot when the network drive that was bolted on the NFS was “dropped” once again, and we found a way to distribute tasks through a quickly found accounting computer with shared disks.
Secondly, besides expect itself, we need to use 2 types of cycles: with a precondition and with a parameter. The first is necessary for “looping” the process so that you can hang the execution of the script as a background for the whole lesson. The second is needed to iterate through the required addresses, which we will access via ssh using the actual expect.
It should also be remembered that the standard script drops out when the first error occurs, for example, the client's computer does not respond, and you should probably look for a way to handle such exceptions.
Script
We inform that for execution we need to use a nonstandard interpreter.
We set a pause to wait for a response from the client, username and password (this is more likely by inertia from the examples available on the network):
set timeout 2 set USER "u1" set PASS "1"
Script start:
while 1 { // foreach HOST {58 60 61} { // . , , spawn ssh $USER@192.168.0.$HOST // expect { "(yes/no)?*" { send "yes\r" // \r , . } } expect "word:" send "$PASS\r" expect "$*" send "killall -r teew*\r" expect "$*" send "killall firefox\r" expect "$*" send "killall chrome\r" expect "$*" send "exit\r" expect eof }
It was a simple auto-kill script for unnecessary processes for the gymnasium. In my own office, I modified the script for auto-shutdown machines:
guest@0-315-gymn2 ~ $ cat scripty
Conclusion
I hope that the mention of the wonderful expect interpreter and a couple of living examples of working with it will be useful to the community and will generate quite a few articles about its more subtle uses.
PS:
The script for autosearching active computers and creating a list of addresses for processing a little later was written by one of my students (not to do all the interesting things myself). A little later, when he deserves an invite, he will be able to write about it as applied to a more interesting task.