我使用em-ssh gem(它是EventMachine的net-ssh适配器)在多个远程主机上执行一些shell命令。命令在第一台主机上成功执行,但在其他主机上失败,它会抱怨“EventMachine :: Ssh :: ConnectionTerminated” 和“EventMachine :: Ssh :: NegotiationTimeout”。 似乎这些主机正在竞争可能是端口的东西?有人可以解释这个吗?
def em_ssh_connection(ssh_host,ssh_user,ssh_password,&cb)
EM.run do
EM::Ssh.start("#{ssh_host}","#{ssh_user}",:password => "#{ssh_password}") do|connection|
connection.errback do |err|
@logger.info "#{err} (#{err.class})"
EM.stop
end
connection.callback do |ssh|
@logger.info "succcessfully connected with #{ssh_host},#{ssh_user},#{ssh_password}"
yield ssh
ssh.close
EM.stop
end
end
end
end
def search_servers(ssh_host,ssh_user,ssh_password)
em_ssh_connection(ssh_host,ssh_user,ssh_password) {|ssh|
command = 'sed -n \'/.*server s1 [0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/=\' '+"#{@haproxy_conf}"
myarray = em_ssh_exec(ssh,command)
if myarray[2] == 0
if myarray[0] != ""
@logger.info "all tcp servers(lines): #{myarray[0]}"
lines = myarray[0].split("\n")
if lines.length >0
start_line = lines[0].to_i
stop_line = lines[lines.length-1].to_i
@logger.info "start_line:#{start_line},stop_line:#{stop_line}"
delete_tcp_upstream(ssh,start_line,stop_line)
else
end
else
@logger.info "the stream field in proxy file is empty"
end
else
@logger.info "search all servers command failed:#{myarray[1]}"
end
}
end
def clear_haproxy(&cb)
search_servers(@ssh_host1,@ssh_user1,@ssh_password1)
search_servers(@ssh_host2,@ssh_user2,@ssh_password2)
cb.call
end