posted by Mikhail E. Zakharov on Tue 21st Jun 2005 17:26 UTC

"Unix scripts, Page 2/4"

However, if in spite of all the attractiveness of the foregoing method and all the arguments of the expect authors (see FAQ) you have made up your mind not to use expect, then you are either too lazy or entirely poisoned by Perl. Well, in this case your salvation lies in the installation of the corresponding Perl-module (http://sourceforge.net/projects/expectperl), which is supposed to support all the functions of the original expect, On FreeBSD you can carry it out by the installation from ports:

# cd /usr/ports/lang/p5-Expect
# make install clean

Now our example with telnet-session will be like that:

#!/usr/bin/perl

use Expect;

my $exp = Expect->spawn("telnet foo.bar.com");
$exp->expect($timeout,
        [ 'ogin: $' => sub {
                            $exp->send("luser\n");
                            exp_continue; }
        ],

        [ 'assword:$' => sub {
                                $exp->send("TopSecret\n");
                                exp_continue; }
        ],
        '-re', qr'[#>:] $'
);
$exp->send("who am i\n");
$exp->send("exit\n");
$exp->soft_close();

If I an mistaken and your attachment to Perl isn't very strong you can take the opportunity of using Python as a corresponding module pexpect is written for it (http://pexpect.sourceforge.net). It's clear that Python language should be installed on the system beforehand, otherwise the FreeBSD ports will help you again:

# cd /usr/ports/lang/python
# make install clean 

And the same for the pexpect module:

# cd /usr/ports/misc/py-pexpect
# make install clean

The script of our telnet-session in Python will be like that:

#!/usr/local/bin/python

import pexpect

child = pexpect.spawn('telnet foo.bar.com');

child.expect('ogin: ');
child.sendline('luser');

child.expect('assword:');
child.sendline('TopSecret');

child.sendline('who am i');
child.sendline('exit');

child.expect(pexpect.EOF);

print child.before;

Certainly, if for some reason Python doesn't suit you either you can install, let us say, PHP language. Well, I think you realize that the searching of suitable solution can go on for a long time and may be only MS Visual Basic will be lacking in the list of results. So, I believe the time has already approached to put it all aside and come to

to the Point.

Well, now I'll tell you what really takes place when we start interactive applications from shell scripts. Though the last sentence is a bit in the stile of a conclusive speech of Hercules Poirot we are rather far from the end of narrative. In fact we are at its beginning. So let's forget everything suggested by expect and its worthy clones.

At first we'll try to make some rough model to simulate the expect-like programs. Let's use sh-scripts with all the standard UNIX tools trying to get our point and the target of this article. It's essential to note that all the experiments are valid for FreeBSD and I can't guarantee they'll give the same results on other operating systems.

To simplify out difficult task and not to fight with pipes mentioned in the first examples of the article let's make to FIFO-files: one for the standard input (in.fifo) and the other for the output (out.fifo) leaving alone the standard error-flow for now:

$ mkfifo in.fifo
$ mkfifo out.fifo

Then with the redirection its I/O to in.fifo and out.fifo let us execute unsafe telnet of which, I'm afraid, you are already sick and tired because of numerous examples on expect, perl and python:

$ telnet -K localhost > out.fifo < in.fifo

A little step aside: as all the experiments are hold on the FreeBSD box, when we attempt to connect with the FreeBSD telnet server, SRA secure login mechanisms are automatically involved to secure telnet-session:

$ telnet localhost
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Trying SRA secure login:
User (luser):			<-- server waits for login, no new-line printed by server
Table of contents
  1. "Unix scripts, Page 1/4"
  2. "Unix scripts, Page 2/4"
  3. "Unix scripts, Page 3/4"
  4. "Unix scripts, Page 4/4"
e p (0)    22 Comment(s)

Technology White Papers

See More