Xref: utzoo comp.unix.admin:508 comp.unix.questions:26868 comp.unix.shell:886 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!think.com!yale!cmcl2!kramden.acf.nyu.edu!brnstnd From: brnstnd@kramden.acf.nyu.edu (Dan Bernstein) Newsgroups: comp.unix.admin,comp.unix.questions,comp.unix.shell Subject: Re: telnet in a shell script Summary: example of how to translate expect script into sh/pty script Message-ID: <7006:Nov1408:13:3290@kramden.acf.nyu.edu> Date: 14 Nov 90 08:13:32 GMT References: <3886@male.EBay.Sun.COM> <8026@muffin.cme.nist.gov> Organization: IR Lines: 52 In article <8026@muffin.cme.nist.gov> libes@cme.nist.gov (Don Libes) writes: > In article <3886@male.EBay.Sun.COM> briantr@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&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