
API Ruby class
195
>>> END-OF SENTENCE
Updating DNS settings on multiple devices
Imagine I have a list of RouterOS devices that all need primary and secondary DNS settings updated. Here's an
example Ruby script to do this:
#!/usr/bin/env ruby
require 'rubygems'
require 'mtik'
## List of devices (hostnames/IPs) to contact:
devlist = [
'10.0.0.4',
'10.0.0.5',
'10.0.0.22',
'10.1.44.22',
'10.1.44.79'
]
## Example assumes all devices use the same API user/pass:
USERNAME = 'admin'
PASSWORD = 'password'
## Set DNS to these IPs (if these were the name servers in question):
PRIMARYDNS = '10.20.30.2'
SECONDARYDNS = '192.168.44.2'
## Set to 1 to do each device serially, or greater to fork parallel processes
MAXFORK = 1
children = 0
devlist.each do |host|
Kernel.fork do
puts "#{host}: Connecting..."
mt = nil
begin
mt = MTik::Connection.new(
:host=>host,
:user=>USERNAME,
:pass=>PASSWORD
)
rescue Errno::ETIMEDOUT, Errno::ENETUNREACH, Errno::EHOSTUNREACH => e
puts "#{host}: Error connecting: #{e}"
exit
end
## The MTik::Connection#get_reply() method executes a command, then waits
Komentáře k této Příručce