Using a child process to keep a SSH pseudo-tty-less connection open in Ruby
I want to create a Ruby
script which opens a SSH pseudo-tty-less connection, and keeps it open.
I also want to send it the password with help of e.g. expect
.
I have tried this: (I have removed servername, password and username)
#!/usr/bin/env ruby
require "pty"
require "expect"
username = 'USERNAME'
server = 'SERVERNAME'
password = 'PASSWORD'
r_f, w_f, pid = PTY.spawn("ssh -T -l #{username} #{server}")
w_f.sync = true
r_f.expect(/.*asswor.*/, 600) do |output|
w_f.puts password
puts "Sending password"
end
# If we are logged on, we get a message with "Hello, username"
r_f.expect(/.*ello.*/) do
开发者_开发问答 puts "You are now logged on."
end
However, this seems to close the connection after it has received confirmation that it is connected
Your process must stay alive. If your process dies, the child processes are killed and the pty is destroyed. You can make your process sleep forever:
sleep
精彩评论