timothy wright
timothy wright

Reputation: 93

Rails and Net:SSH

So I am trying to ssh to a linux server from my app and run a script to provision a modem.

Here is the modem model.

  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

I call c.modem.add c = Customer.first Here is the error I am getting from the console when I call it.

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" 

What am I doing wrong?

Thanks!

--Tim

Upvotes: 1

Views: 63

Answers (1)

timothy wright
timothy wright

Reputation: 93

Here is my working code.

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

Upvotes: 1

Related Questions