From: nobu.nokada@... Date: 2004-04-15T14:58:49+09:00 Subject: Re: ruby pty/expect help Hi, At Thu, 15 Apr 2004 12:09:12 +0900, Ara.T.Howard wrote in [ruby-talk:97212]: > perhaps a handle on an object which can be 'closed' could be returned instead > of an array of Files, etc: > > passphrase = gen_random_passphrase > > pty = PTY.spawn 'ssh-keygen' > > pty.stdin.puts 'id_rsa.pub' > pty.stdin.puts passphrase > pty.stdin.puts passphrase > pts.close # do the right thing Yes, I've thought about same strategy for open3. > > pty = PTY.spawn 'ssh-agent' > > pty.stdin.write passphrase > > ... > > > > BTW, you don't need PTY at all to generate ssh key > > automatically. > > see above - i want to pass it to ssh-agent. thanks though, didn't know > that... ssh-agent doesn't read passphrase but prints some environment variables and runs as a daemon, like as: $ ssh-agent SSH_AUTH_SOCK=/tmp/ssh-FGmutb8164/agent.8164; export SSH_AUTH_SOCK; SSH_AGENT_PID=8165; export SSH_AGENT_PID; echo Agent pid 8165; Therefore you should set those variables instead. e.g., `ssh-agent`.scan(/^(SSH_[A-Z_]+)=([^; ]*)/, &ENV.method(:[]=)) Then, you'll need to send passphrase to ssh-add. At this time, you have 2 methods: 1) with pty. PTY.spawn("ssh-add") do |r,w,pid| r.expect %r/^Enter.*:\s+/ {w.puts passphrase} end 2) without control terminal. Since ssh-add reads passphrase from stdin when no ctty or X are available, you can: IO.popen("-", "w") do |f| if f f.puts passphrase else ENV.delete("SSH_ASKPASS") ENV.delete("DISPLAY") Process.setsid exec("ssh-add") end end or, if setsid command is available and env command has -u option: IO.popen("env -u SSH_ASKPASS -u DISPLAY setsid ssh-add", "w") do |f| f.puts passphrase end -- Nobu Nakada