Sweet. It works. Thanks Jordan On Dec 1, 2:18 am, MonkeeSage <MonkeeS... / gmail.com> wrote: > On Dec 1, 2:52 am, MonkeeSage <MonkeeS... / gmail.com> wrote: > > > > > On Dec 1, 2:41 am, MonkeeSage <MonkeeS... / gmail.com> wrote: > > > > On Nov 30, 7:56 pm, Trey <treyb... / gmail.com> wrote: > > > > > I'm in need of running a system command as another user than the user > > > > that is executing the ruby script. Is this possible? Can I execute > > > > su (and somehow supply the correct password), then run the command? > > > > Here is a naive version using the built-in pty extension (*nix only, > > > but since you mention su, I assume that's not a problem)... > > > > require "pty" > > > require "expect" > > > > cmd = "sleep 1; sudo -u someuser ls 2>&1" > > > PTY.spawn(cmd) { | stdin, stdout, pid | > > > stdin.expect(/Password:|/) { | result, pass | > > > stdout.write("secret_password\n") if pass > > > } > > > puts stdin.read > > > > } > > > > Regardsm > > > Jordan > > > Oops... > > > > stdout.write("secret_password\n") if pass > > > stdout.write("secret_password\n") if pass == "Password:" > > Good grief. Third time's a charm (maybe)... > > require "pty" > require "expect" > > PTY.spawn("sleep 1; sudo -u root ls 2>&1") { | stdin, stdout, pid | > begin > stdin.expect("Password:") { > stdout.write("secret_password\n") > puts stdin.read.lstrip > } > rescue Errno::EIO > # don't care > end > > } > > Sorry about that! Was confusing live code with the version I meant to > post (twice!). I was trying to do fancy stuff and detect when the > password was catched already by sudo, but never got that working. > > Regards, > Jordan