telnet in a shell script
Dan Bernstein
brnstnd at kramden.acf.nyu.edu
Wed Nov 14 19:13:32 AEST 1990
In article <8026 at muffin.cme.nist.gov> libes at cme.nist.gov (Don Libes) writes:
> In article <3886 at male.EBay.Sun.COM> briantr at sunnet.EBay.Sun.COM (Brian Tran) writes:
[ a telnet automation question ]
> From your shell script, call an expect script like the following:
> spawn telnet male.ebay.sun.com
> expect "*login:*"
> send "brian\r"
> expect "*Password:"
> send "igiveup\r"
> interact
A simple, exact translation of this into sh/pty code is shown below. It
works on any machine with named pipes---SunOS and Ultrix, for instance.
It's easy to modify for other machines.
One big advantage of the sh/pty version over Don's expect-based version
is that expect can't handle telnet job control. sh can't either, but at
least the user can type ``^]z'' to telnet followed by ``^Z'' to regain
his shell.
#!/bin/sh
/etc/mknod out.$$ p; exec 2>&1
( exec 4<out.$$; rm -f out.$$
<&4 waitfor 'login: '
echo 'brian'
<&4 waitfor 'Password:'
echo 'igiveup'
<&4 cat -u >&2 &
cat -u
) | pty telnet male.ebay.sun.com > out.$$
Here waitfor is a dumb program along the lines of
extern char *malloc(); main(argc,argv) int argc; char *argv[]; {
int len; char *s; int pos; char ch; int f; int p; if (!argv[1])
exit(1); len = strlen(argv[1]); if (!(s = malloc(len))) exit(2);
pos = 0; f = 0; while (read(0,&ch,1) == 1) { if (write(2,&ch,1) != 1)
exit(3); if (ch) { s[pos] = ch; pos++; if (pos == len) { f = 1;
pos = 0; } if (f && (ch == argv[1][len - 1])) { for (p = 1;
s[(pos + p) % len] == argv[1][p];p++) ; if (!argv[1][p]) exit(0);
} } } exit(4); }
Btw, Don, this is an example of what I meant by jury-rigging. Obviously
I only bothered to write waitfor once; it might be better to use a more
general tool (Perl, perhaps) to do the job, but waitfor isn't sluggish.
> The "interact" will let you use the telnet as if you had logged in
> yourself. Or you can do more send/expect statements.
Similar comments apply to the script.
---Dan
More information about the Comp.unix.questions
mailing list