# IO.popen and ssh

**URL:** <https://www.ruby-forum.com/t/io-popen-and-ssh/114350>\
**Category:** Ruby\
**Created:** [September 11, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/io-popen-and-ssh/114350 "2007-09-11T17:44:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![plambert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@plambert](https://www.ruby-forum.com/u/plambert)\
**Post date:** [September 11, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/io-popen-and-ssh/114350/1 "2007-09-11T17:44:36Z")

</div>

I’ve been having some trouble using IO.popen to control an ssh process  
(and also mysqldump) on Linux. My understanding is that if I do:  
p = IO.popen(‘ssh mymachine’)  
then the input and output of the ssh subprocess should be accessible via  
p. However, when I try that in an irb shell, the ssh process  
immediately takes over the standard input and output of irb, and I get  
an “Enter password:” prompt. I have to hit control-c to get back to the  
irb prompt.

Is there a workaround for this? Or should I be doing this a different  
way? Thanks,  
–Paul

---

<div class="post-metadata">

**Author:** ![plambert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@plambert](https://www.ruby-forum.com/u/plambert)\
**Post date:** [September 11, 2007, 5:59pm UTC](https://www.ruby-forum.com/t/io-popen-and-ssh/114350/2 "2007-09-11T17:59:30Z")

</div>

Paul L. wrote:

> Is there a workaround for this? Or should I be doing this a different  
> way?

Why not use Net::SSH to connect directly from ruby?  
[http://net-ssh.rubyforge.org/chapter-2.html#s1](http://net-ssh.rubyforge.org/chapter-2.html#s1)

---

<div class="post-metadata">

**Author:** ![plambert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@plambert](https://www.ruby-forum.com/u/plambert)\
**Post date:** [September 11, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/io-popen-and-ssh/114350/3 "2007-09-11T18:11:27Z")

</div>

That looks much better-- thanks!

Edwin Van leeuwen wrote:

> Why not use Net::SSH to connect directly from ruby?  
> [http://net-ssh.rubyforge.org/chapter-2.html#s1](http://net-ssh.rubyforge.org/chapter-2.html#s1)

---

<div class="post-metadata">

**Author:** ![plambert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@plambert](https://www.ruby-forum.com/u/plambert)\
**Post date:** [September 11, 2007, 8:38pm UTC](https://www.ruby-forum.com/t/io-popen-and-ssh/114350/4 "2007-09-11T20:38:34Z")

</div>

I’ve had varied experience, but to answer the original question about  
password prompt… Why not setup key-based auth between the machines in  
question? Doing so has always worked for me.

t

---

<div class="post-metadata">

**Author:** ![plambert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@plambert](https://www.ruby-forum.com/u/plambert)\
**Post date:** [September 11, 2007, 9:34pm UTC](https://www.ruby-forum.com/t/io-popen-and-ssh/114350/5 "2007-09-11T21:34:42Z")

</div>

On Sep 11, 2007, at 9:44 AM, Paul L. wrote:

> Is there a workaround for this? Or should I be doing this a different  
> way? Thanks,

ssh requires a pty to read the password. note that this is NOT the  
same as STDIN, which is what you get with IO.popen. you need to use  
the built-in pty lib to control ssh that way and it’s still fraught  
with peril. it’s much, much, much, much better to setup ssh keys for  
password-less access (no passwords in scripts), and to spawn a simple  
command, such as /bin/sh, on the remote end to read pre-generated  
commands (a script).

regards.

a @ [http://drawohara.com/](http://drawohara.com/)

---

<div class="post-metadata">

**Author:** ![plambert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@plambert](https://www.ruby-forum.com/u/plambert)\
**Post date:** [September 11, 2007, 8:32pm UTC](https://www.ruby-forum.com/t/io-popen-and-ssh/114350/6 "2007-09-11T20:32:33Z")

</div>

Quoth Edwin Van leeuwen on Tuesday 11 September 2007 08:59:39 am:

> Paul L. wrote:
> 
> > Is there a workaround for this? Or should I be doing this a different  
> > way?
> 
> Why not use Net::SSH to connect directly from ruby?  
> [http://net-ssh.rubyforge.org/chapter-2.html#s1](http://net-ssh.rubyforge.org/chapter-2.html#s1)

Because at least in my experience, it doesn’t work? 😃
