Expect – Linux tool for automating interactive programs
linux.die.net
linux.die.net
Nevethless I think it is safe to say that expect was not written using or for Linux as Linux was not released until 1991.
Those days, people are thought to not bother their brain with useless words and knowledge.
#!/bin/bash
expect -c "
spawn ssh user@10.0.0.1
expect \"password:\"
send \"12345\r\"
interact
"
`interact` hands you back control to do whatever you want, but you don't actually need to do that. You can have `expect` run every command, fill out every field.The tool you need when you're in environments you don't deserve.
https://linux.die.net/man/1/sshpass
...Operations folks today might not understand -- everything just works nowadays, which is slightly boring. And boring is good!
But there's something unique about the feeling of making things work even though they are not designed to work, and really have no right to work. Expect has been a lifesaver for some of the most critical systems at some of the biggest companies in the world.
No aspersions cast by me!
It has all the pitfalls of putting your front door key under your doormat. Except you're doing it in a world where everyone can materialize a unique key out of thin air, and you can always instantly tell your door which of those keys should or shouldn't be allowed to open it.
sshpass is a curse, even calling it a crutch would be improper flattery. It's a dangerous cheat that accomplishes nothing except impeding people from otherwise spending the 20 minutes it takes to figure out SSH keys.
Any automation around passwords is a crutch and a mistake. But sometimes it is necessary.
You don't always control the remote systems. The remote systems are not always capable of key-based auth. And sometimes the remote system is not of high concern so the "danger" is null.
sshpass makes a reasonable effort to do the best-possible thing under these less-than-ideal circumstances. The other options suck more.
My most recent use of sshpass is to collect reports from a vendor over sftp. I would have preferred to use https with BASIC auth, but in truth that has exactly the same problems as sshpass, and I have other hills to die on.
The actual productive workflow is to do your task with autoexpect, and then refine the generated output: https://linux.die.net/man/1/autoexpect
Meta shells and meta meta shells should be an assumed feature by now, not a hit or miss of terminal program feature sets, and not tied to UI programs.
;)
I thought that it was far better than the TCL Expect because I could also use other CPAN modules and have Perl's regular expressions to parse the system's output.
Expect is a great tool to have in your toolkit.
Still a great answer and latest update in March this year.
We used Perl/Tk for GUIs on that old system. It brimgs the ease of Tk GUIs to Perl:
https://metacpan.org/dist/Tk/view/pod/UserGuide.pod
>> Nowadays it falls into the category of tools that were once somewhat used, but now are obscure
To be sure, but a lot of it still works without changes!
To be fair, Tcl has Henry Spencer’s regexp[0][1], and is awesome; Postgres also base their regex on Henry’s work for Tcl[2]. Perl is certainly famous for its regex and popularizing thinking of problems in terms of regexes (for better or for worse[3]), but it’s not the only game in town.
[0] https://en.wikipedia.org/wiki/Henry_Spencer
[1] https://wiki.tcl-lang.org/page/Regular+Expressions
[2] https://github.com/postgres/postgres/blob/master/src/backend...
[3] https://stackoverflow.com/questions/1732348/regex-match-open...
Agreed.
In that old system we did not have any TCL scripts or third-party libraries--just C, Perl, and C shell (no Bash; it was Unix).
C shell scripting is painful (https://www.grymoire.com/Unix/Csh.html), so practically all scripting was Perl or occasionally AWK.
For me, I detect them by checking the exit-code on each command that I run, and also by looking for expected output in response to the commands.
Corrective action is usually a full-stage retry.
So true, and yet so common.
I used it to let aider suggest and run shell commands [1]. This is handy, so aider can offer to run the code it just wrote for you, run migrations, install dependencies, etc.
Using pexepect makes it easy for aider to run interactive shell commands and capture their output. This allows aider to fix bugs or suggest next steps based on the command’s output, error messages, etc.
1. aider makes some change 2. aider then tells itself to use pexpect to check what changes it made 3. pexpect runs, opens browser somehow captures error messages if any and feeds that back?
i am pretty sure i misunderstood something
But ya, aider can: code something for you, run it, capture the terminal output with pexpect and iterate further based on that.
Expect would still be useful for someone scripting a debugger (for example), but Tcl falls under the close to isoteric language nowadays and it takes less time to just repeat the commands in a readline capable interactive program than find just the right way to script it.
Last time I've tried to use expect was when I attempted to build a Tk frontend to the Haskell GHCi debugger (similar to the DDD application), but I got tripped up, from vague memory, on terminal ansi codes, and random lock ups I couldn't pinpoint.
We rewrote our deployment arch since then
Also, your comment would read almost identically and perhaps even better without the first three words.
Many years later I heard they were still relying on that tool!
Automating tedious tasks as a network engineer and sysadmin motivated me to learn proper software development. If gluing together TCL/tk scripts could make my life easier, imagine what larger programs would do.
The good days.
I don't understand what this means, but it sounds like an interesting piece of 'how we used to do things'?
oh. that's an OLD old tool..
O’Reilly put out a book about expect back in the old days (1995).
I used expect a few times to automate remote admin, and even to scrape terminal-based applications.
Pyexpect is in another class of software. It is a Python testing utility that provides expressive assertions for unit testing.
On the other hand, the Unix expect program, and its Python equivalent pexpect, are used for automating interactive applications.