1

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

1
  • This is likely a problem with the method you're using to call the script. The code for Net::SSH does not include a call to bash. Commented Mar 11, 2016 at 22:05

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.