Executing multiple JUNOS router commands with ruby

358 views Asked by At

Has anyone tried to execute multiple commands in a JUNOS router using ruby with the net/ssh and net/ssh/telnet gems where you have to go into configure mode? It never wants to accept the configure commands and I don't know why.

Here is my code:

def exec_router(host_type, commands)
  puts "commands: #{commands}"
  output = ""
  ssh = Net::SSH.start(HOST_MAP[host_type], QA_USER, :password => QA_USER_PASSWORD)
  t = Net::SSH::Telnet.new("Session" => ssh, "Prompt" => /.+@.+>/, "Output_log" => "/tmp/test.outputi")
  commands.each { |command| output << t.cmd(command) }
  puts output
  t.close
  ssh.close
end

And here is the output that it produces:

  commands: ["configure", "show policy-options prefix-list greautomation-676872"]
  configure
              ^
  unknown command.

  {master:member0-re0}
  [email protected]> show policy-options
                   ^
  syntax error, expecting <command>.

[email protected]> show policy-optionsprefix-list ^ syntax error, expecting . [email protected]> show policy-optionsprefix-listgreautomation-676872 ^ syntax error, expecting .

I know my ssh/telnet stuff is working because I can replace the block that iterates through the command array with t.cmd('?') and I get the expected output with no errors.

My Junos version is 15.1F6-S3.8 and I am using ruby 2.3.0.

Thanks in advance

Craig

3

There are 3 answers

0
Dwarakanath On

Have you checked https://github.com/Juniper/net-netconf? It is a Ruby Gem for doing NETCONF based interactions with Junos devices.

0
Nitin Kr On

You should use RubyEZ.

Refer: https://github.com/Juniper/ruby-junos-ez-stdlib

to get the configuration of the device, we have get-configuration rpc.

data = ndev.rpc.get_configuration                   # Junos specific RPC
puts "Showing 'policy-options' hierarchy ..."
puts cfgall.xpath('policy-options')  

To call operational rpc (for ex "show chassis hardware" corresponds to get-chassis-inventory rpc), hence

data = ndev.rpc.get_chassis_inventory  
1
Diogo Montagner On

Although I would recommend you to use RubyEZ libraries, your problem is related to the fact that you are trying to execute a configuration syntax command in the operational mode.

Here is your problem:

commands: ["configure", "show policy-options prefix-list greautomation-676872"]

Make the following changes:

  • remove the "configure" command
  • replace "show policy-options prefix-list greautomation-676872" with "show configuration policy-options prefix-list greautomation-676872"

This should solve your problem.

I highly recommend you to look into RubyEZ libraries from Juniper.