?
Solved

expect script pauses at finish

Posted on 2009-04-02
9
Medium Priority
?
1,257 Views
Last Modified: 2013-12-16
My script pauses when completing.  I have done several google searches but I don't see anything which would indicate this is trivial or easy to fix.  Do I need a newer/older version of expect?  My current version is 5.43.0.

The error message causing the one second pause is ``write() failed to write anything - will sleep(1) and retry...''.  The thing it is trying to do is ``tty_set: raw = 0, echo = 1''.

My script is pasted below except the password and IP address.  Instead of the real IP I used a fake 4.2.2.2.  I also used a fake password of 12345.

If you run the script with ``expect -d myscript.exp'' then you can see the messaage when you logout of the remote server.  ``-d'' turns on debugging.

Please let me know if I can clarify my question any better.  Thanks in advance.
#!/usr/bin/expect -f
set timeout -1
spawn ssh -p 22 -l root 4.2.2.2
match_max 100000
expect -re "assword"
send -- "12345\r"
interact
exit

Open in new window

0
Comment
Question by:neospire_noc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 4

Expert Comment

by:robertfwoods
ID: 24055695
Greetings.

Look here for a possible solution:

http://www.wellho.net/forum/The-Tcl-programming-language/Exiting-from-interact-Expect.html

Seems you must tell interact how to quit in advance.

here is another good discussion:

http://objectmix.com/tcl/246041-expect-how-detect-if-process-closed-interact-command.html

0
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 24058359
I've noticed that behaviour on issuing exit in a script that doesn't use interact. Out of curiousity I tinkered with it for a bit, but couldn't make it go away. The second or so delay didn't bother me a lot, so I didn't pursue it further. Actually I tried quite hard to get rid of it but couldn't - would be interested if anybody has a solution but my take is that you have to live with it.
0
 

Author Comment

by:neospire_noc
ID: 24060465
Hi thanks for the comments, but those links don't seem to help with my problem.  I tried to use the ``interact +++ return'' as well as ``interact -o eof return'' but neither seemed to work.  The one second pause at the end is still happenning.

Looking further, I can see it's a part of the expect library.  I would ask the expect mailing list but I don't see an expect mailing list on their web site.  On their FAQ they say they don't have a mailing list anyway.

I guess I can try to look in the sources for this string and make it not sleep if this happens and just exit.  Who knows maybe it will work.
strings /usr/lib/libexpect5.43.so | grep "write() failed to write anything "
write() failed to write anything - will sleep(1) and retry...

Open in new window

0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 35

Expert Comment

by:Duncan Roe
ID: 24064925
Yes that might work - at least on Linux platform. The message comes from ExpOutputProc in exp_chan.c (see Code Snippet).
The sleep is done after write() returns a written byte count of 0. The expect code treats this as a transient condition - hence the sleep and return of EAGAIN - which may cause the caller to re-try the write, by which time the condition has cleared.
Whether or not it still happens on Sun platforms, it's now happening under Linux.
I take issue with the comment "this is not a documented return value". Actually it *is* documented in the man page:

If count is zero and  the file descriptor refers to a regular file, 0 will be returned without causing any other effect

Is that happening?
Will post more if I find time to investigate
   219 /*
   220  *----------------------------------------------------------------------
   221  *
   222  * ExpOutputProc--
   223  *
   224  *^IThis procedure is invoked from the generic IO level to write
   225  *^Ioutput to an exp channel.
   226  *
   227  * Results:
   228  *^IThe number of bytes written is returned or -1 on error. An
   229  *^Ioutput argument^Icontains a POSIX error code if an error occurred,
   230  *^Ior zero.
   231  *
   232  * Side effects:
   233  *^IWrites output on the output device of the channel.
   234  *
   235  *----------------------------------------------------------------------
   236  */
   237
   238 static int
   239 ExpOutputProc(instanceData, buf, toWrite, errorCodePtr)
   240     ClientData instanceData;^I^I/* Exp state. */
   241     char *buf;^I^I^I^I/* The data buffer. */
   242     int toWrite;^I^I^I/* How many bytes to write? */
   243     int *errorCodePtr;^I^I^I/* Where to store error code. */
   244 {
   245     ExpState *esPtr = (ExpState *) instanceData;
   246     int written = 0;
   247
   248     *errorCodePtr = 0;
   249
   250     if (toWrite < 0) Tcl_Panic("ExpOutputProc: called with negative char count");
   251
   252     written = write(esPtr->fdout, buf, (size_t) toWrite);
   253     if (written == 0) {
   254       /* This shouldn't happen but I'm told that it does
   255        * nonetheless (at least on SunOS 4.1.3).  Since this is
   256        * not a documented return value, the most reasonable
   257        * thing is to complain here and retry in the hopes that
   258        * it is some transient condition.  */
   259       sleep(1);
   260       expDiagLogU("write() failed to write anything - will sleep(1) and retry...\n");
   261       *errorCodePtr = EAGAIN;
   262       return -1;
   263     } else if (written < 0) {
   264       *errorCodePtr = errno;
   265       return -1;
   266     }
   267     return written;
   268 }

Open in new window

0
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 24065111
I wouldn't waste time trying to fix expect 5.43. There is a newer one - 5.44.1.11 (on my Slackware 12.2 DVD image).
I diffed the sources and found inter alia:

diff -r -w expect-5.43/exp_chan.c expect-5.44.1.11/exp_chan.c
[lots of stuff]
250a266,268
>     if (toWrite ==0) {
>         return 0;
>     }

That looks quite likely to fix our problem. I'll try an upgrade as soon as I get time (have to go now)
0
 
LVL 35

Accepted Solution

by:
Duncan Roe earned 2000 total points
ID: 24069871
Upgrade complete. expect-5.44.1.11 *does* fix the wait problem. You probably also need to upgrade to tcl-8.5.5 and tk-8.5.5 - I did that anyway.
0
 

Author Comment

by:neospire_noc
ID: 24077273
Hi Duncan_roe, I had similar results, it works in the new version of expect with slackware 12.2 but not the old version of expect with slackware 12.1.

I tried to compile from sources but no luck (even with the tcl8.5.5 sources available).  Were you able to upgrade both the tcl and the expect packages ``in place'' on the slackware 12.1 system to the slackware 12.2 versions of those packages?  I will give that a try later today when I have a chance.

Thanks for all your help.
0
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 24082108
Yes I did that. Actually I did rather more - upgraded glibc (Slackware upgradepkg can do that on a running system - really cool:). Because I was upgrading from SW10.1 I had a bit more to do - kernel was too old so had to config up my current version (2.6.25.4) for the 32-bit system, also had to move /lib/tls/ out of the way because no dynamic executables would run after ldconfig. Had to do something like
cd /lib;./ld-2.3.so /usr/bin/mv tls tls.old
so move it out the way first if you still have it. I'd expect to be able to build tcl/tk/expect after upgrading glibc and gcc to the versions shipped with SW12.2 - nowadays I always try to use the Slackware build script e.g. expect.SlackBuild having copied the entire expect dir off the ISO to /tmp. I need to do that to build 64-bit Slackware packages - these don't seem to be shipping yet.
Upgrading is a snip - got this hint from some doco somewhere in the DVD:

cd /mnt/slkdvd/slackware/tcl
upgradepkg *.tgz
0
 

Author Comment

by:neospire_noc
ID: 24097042
Thanks for all  your help.
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question