Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
"open(slave pty): bad file number" error

Posted by KNut (KNut), 24 February 2004
Hi all,

I have an Expect script that makes an ssh connection to a server, does a df -k on the remote server and exits. I repeat this on a number of servers.
It works fine for a while then starts giving me this error :-
open(slave pty): bad file number

At this point, even if I try a ssh outside of my Expect script I get the same error. I have to close my cygwin and start it up again to be able to continue.

I am running the expect script using Cygwin on Windows XP.

I do know that this error is most likely if I do not close the processes spawned by my expect script properly. However, I do think that my script does close (or exit) properly. I am not sure what I am missing out.
Can this be a problem with Cygwin ?

I am a newbie so any help is much appretiated !

Here's my script :


proc report_failure {msg} {
send_user "\n FAILURE: $msg \n"

proc report_failure_and_close {msg} {
send_user "\n FAILURE: $msg \n"
close; wait

proc handle_login {} {

send "myPassword\r"

while (1) {
expect {
-nocase "password:" { report_failure_and_close "Incorrect Password !"
-re "(.*)\\$ $" { break }
-re "(.*)> *$" { break }

send "df -k\r"

expect -re "(.*)\r"

expect {
-re "\\$ $" { expect *
send "exit\r"
wait }
-re "> *$" { expect *
send "exit\r"
wait }


spawn ssh -l user <IPADDRESS>

expect {
-re "(yes/no).*" { send "yes\n"
expect -nocase "password:" { handle_login }

"Please type 'yes' or 'no':"
{ send "yes\n"
expect "Password:" { handle_login }

-re "password: *" { handle_login }

-re "^ssh:" { report_failure_and_close "Connection Failed for" }

-re "Usage:" { report_failure_and_close "Usage Error !" }

eof { report_failure "!!! Unexpected Error !!!" }

timeout { report_failure_and_close "Connection time out" }


Posted by admin (Graham Ellis), 25 February 2004
I think you might not be closing your ssh session by running an exit command on the remote computer, even though you're closing the spawn process connection within your expect program.  I can't be sure of this as I think you've only posted your login code and not your logout code.

When exiting a spawned process, you have to issue the appropriate remote closedowns and use expect sequences to ensure it has completed.   Slightly offtopic but this is a particular problem if you spawn FTP processes - it's a common error to start a get process and then exit the expect program ... which kills the transfer in the middle and you get just a partial file.   I suspect you're seeing a similarly generated effect

Posted by KNut (KNut), 25 February 2004
Thanks Graham !

I do not have any logout code. I am assuming that I am logging out by issuing an 'exit'.
Is it right that by issuing an exit I am initiating the closedown from the remote program and expect will automatically issue a 'close' in response. [If I have an explicit close after the exit I get an error].
I am also using a 'wait', if I understand correctly this will make my Expect script wait for all the processes to close on the remote end.

I have only done Expect and perl for 4 or 5 days now... so please excuse my basic questions.

Posted by admin (Graham Ellis), 25 February 2004
Ah - yes.  I see the "send exit" now .... I'm out of my office away from my usual excellent connection this week is all I can say as an excuse  

I would tend to wait for the exit to complete using an expect looking for an end of file ... see my previous comment on issuing a command in FTP;   wait may do it, but I personally would do an expect to make sure it was closed down.

It's going to be one of those things to experiment with - when you use expect, you're in to multiple processes happening at the same time and the correlation and sycronisation gets interesting!


This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho