, 3 min read

Linux commands: expect and kibitz

Occasionally I teach customers and colleagues how to use some aspects of Linux, or how to use and administrate newly developed software on Linux. I regularly use either VNC or kibitz for this. While VNC is somewhat known to many people, kibitz seems to be like a hidden germ, unbeknownst to many.

With VNC two, or even more people, can see what each person is typing, on the screen, and at the same they can also type, or move the mouse, which everybody can see and witness.

kibitz is similar to that, except, there is no mouse, no graphical component. It works with simple text screens. So everything you type into your shell, the guy at the other end will also see, and vice versa, what he types, you will see as well. You can edit files together, change users together, and so on. It is particular handy if the machine in question does not have any graphical stuff, like Gnome, KDE, Xfce, IceWM, etc. kibitz works with two or more users. Unfortunately it is not always installed by default on Ubuntu or Red Hat.

kibitz is a script written in the Expect programming language, which is based on Tcl/Tk. The script size is not more than 500 lines.

expect is a command which is very useful if you want to automate something which originally was never really ment to be automated. One prominent example is ftp. The ftp command usually does not contain any macro language, or any other form for automation, disregarding lftp for the moment. With expect it is very easy to automate file transfers, checking for succesful transfer, and so.

Below you'll find an example for transfering files to a hosting company via ftp. The challenge is that the hosting company closes its connection very early. So you have to constantly log-on to transfer your files.

#!/usr/bin/expect

puts $argc
puts $argv

set timeout -1
spawn ftp ftp-host-name-goes-here
expect "220 Welcome"
expect "Name*:"
send "my-user-name\r"
expect "331 Password"
expect "Password:"
send "my-secret-password\r"
expect "230 User"
expect "ftp>"


if {$argc <= 1} {
        #puts "At least two arguments expected."
        interact
        exit 0
}
set cmd [lindex $argv 0]
if {$cmd != "put"  &&  $cmd != "get"  &&  $cmd != "del"} {
        puts "Command must be either put or get or del"
        exit 2
}

for {set i 1} {$i < $argc} {incr i} {
        set argvi [lindex $argv $i]
        send "$cmd $argvi\r"
        if {$cmd == "del"} {
                expect "250 DELE"
        } else {
                expect "226 Transfer"
        }
        expect "ftp>"
}
send "quit\r"
expect "221 Goodbye&quot;

So you can edit files on your machine, and then just call above script and transfer your file to the hosting company, without entering user name and passwort all the time, and directly telling ftp what to do, here put, get, or del.

The Expect programming language is explained in the book by its creator Don Libes: "Exploring Expect: A Tcl-based Toolkit for Automating Interactive Programs", O'Reilly & Associates.

Exploring Expect

(Amazon told me that I bought this book at 20-Nov-2003, i.e., ten years ago.)

BTW, expect and kibitz are not tied to Linux. You can find them on any Unix system, even on Windows.