Rails和Net:SSH

时间:2016-03-11 20:31:59

标签: ruby-on-rails bash ssh

所以我试图从我的应用程序ssh到Linux服务器并运行脚本来配置调制解调器。

这是调制解调器型号。

  class Modem < ActiveRecord::Base
  require 'net/ssh'
  belongs_to :customer
  belongs_to :plan
  has_one :neighborhood, through: :customer
  # validates :mac, length: { maximum: 12 }
  before_save :remove_dashes

  enum status: [ :provisioned, :downgraded, :offline]

  def remove_dashes
    self.mac = mac.tr('-', '')
  end

  def add
     ssh("dhcp-ctrl add -t modems -g 50Mbps -m 0123456789ab")
    end

  def change
   ssh("dhcp-ctrl edit -t modems -g #{@group} -m #{self.mac}")
  end

  def remove
   ssh("dhcp-ctrl delete -t modems -m #{self.mac}")
  end

  private

  def ssh(script)
   Net::SSH.start(self.neighborhood.ssh_host, self.neighborhood.ssh_user, { port: self.neighborhood.ssh_port, password: self.neighborhood.ssh_pass } ) do |ssh|
     result = ssh.exec!(script)
       puts result
    end
  end



end

我叫c.modem.add c = Customer.first 这是我在调用它时从控制台获得的错误。

2.2.1 :032 > c.modem.add
 => "bash: -c: line 0: unexpected EOF while looking for matching `''\nbash: -c: line 1: syntax error: unexpected end of file\n" 

我做错了什么?

谢谢!

- 添

1 个答案:

答案 0 :(得分:1)

这是我的工作代码。

class Modem < ActiveRecord::Base
  require 'net/ssh'
  belongs_to :customer
  belongs_to :plan
  has_one :neighborhood, through: :customer
  # validates :mac, length: { maximum: 12 }
  before_save :remove_dashes

  enum status: [ :provisioned, :downgraded, :offline]

  def remove_dashes
    self.mac = mac.tr('-', '')
  end

  def add
    @script = "dhcp-ctrl add -t modems -g #{self.plan.cmts_group} -m #{self.mac}"
    output = ssh
    Event.create(name: 'provisioned modem on CMTS', customer_id: self.customer.id, details: output, new_plan_id: self.plan.id)
  end

  def change
    @script = "dhcp-ctrl edit -t modems -g #{self.plan.cmts_group} -m #{self.mac}"
    output = ssh
    Event.create(name: 'changed modem on CMTS', customer_id: self.customer.id, details: output, new_plan_id: self.plan.id)
  end

  def downgrade
    @script = "dhcp-ctrl edit -t modems -g 6mbps -m #{self.mac}"
    output = ssh
    Event.create(name: 'downgraded modem on CMTS', customer_id: self.customer.id, details: output, old_plan_id: self.plan.id, new_plan_id: self.neighborhood.plans.first.id)
  end

  def remove
    @script = "dhcp-ctrl delete -t modems -m #{self.mac}"
    output = ssh
    Event.create(name: 'removed modem on CMTS', customer_id: self.customer.id, details: output, old_plan_id: self.plan.id)
  end

  private

  # def ssh(script)
  #   Net::SSH.start(self.neighborhood.ssh_host, self.neighborhood.ssh_user, { port: self.neighborhood.ssh_port, password: self.neighborhood.ssh_pass } ) do |ssh|
  #     result = ssh.exec!(script)
  #       puts result
  #    end
  #  end
  def ssh
    Net::SSH.start("#{self.neighborhood.ssh_host}", "#{self.neighborhood.ssh_user}", { port: self.neighborhood.ssh_port, password: "#{self.neighborhood.ssh_pass}" } ) do |ssh|
      ssh.exec! @script
    end
  end



end