Return value for PTY.spawn

Hi,
I am a new bee to Ruby… facing problem with PTY.spawn command to
execute ldapsearch query…
I have 1.8.4 version of ruby
here is the code i m trying to execute …

require ‘pty’
require ‘expect’
cmd = “ldapsearch -LLL -b “” -s one -H ldaps://Ip_address -x -D
cn=admin,o=org “o=org” CN description mail fullName
generationQualifier givenName -W”

begin
PTY.spawn(cmd) do |r, w, p |
r.expect %r/Password.*:confused: do
w.puts “password\r\n”
end

            while ((str = r.gets) != nil)
                    puts str
            end
            end

    rescue PTY::ChildExited => e
         #  raise unless e.is_a?(PTY::ChildExited)
           # pid = e.pid
            val = e.status
            print "return value ... #{val}\n"
            rescue Errno::EIO
    end

Here i want to know the exit status of ldapsearch command. Above
code works fine if we give all information correct…
When i tried printing wrong password onto the screen, i was
expecting that block with “PTY::ChildExited” will be executed. For my
surprise it didn’t happen…
Here is the output with wrong password…

ldap_bind: Invalid credentials (49)
additional info: NDS error: failed authentication (-669)

(I expected that it will print “return value…some_value”)

Can anybody help me in how to get the exit status of the command i
executed…
One more point i have noticed is that when i executed command “ls
file_does_not_exist” (Replace cmd with the ls cmd)… Here is what
happening…

VIRTUAL1:/home/LATEST # ruby 3.rb
return value … 512
VIRTUAL1:/home/LATEST # ruby 3.rb
ls: dasf: No such file or directory
VIRTUAL1:/home/LATEST # ruby 3.rb
return value … 512
VIRTUAL1:/home/LATEST # ruby 3.rb
return value … 512
VIRTUAL1:/home/LATEST # ruby 3.rb
ls: dasf: No such file or directory
VIRTUAL1:/home/LATEST # ruby 3.rb
return value … 512
VIRTUAL1:/home/LATEST # ruby 3.rb
return value … 512
VIRTUAL1:/home/LATEST # ruby 3.rb
ls: dasf: No such file or directory

We can notice that sometimes it is printing the message from
ChildExited block and sometimes it is printing actual message…
Here it seems to me we dont have any control over this… Is there any
way to make our ruby program to always execute ChildExited block… ?

Thanks in advance… :slight_smile:
sayeed.