本文介绍了编写自动化脚本来配置设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的需求是这样的:
我需要登录到远程设备(比如路由器/交换机)并执行以下命令.
I need to log in to a remote device (say Router/switch) and execute following commands.
telnet xx.xx.xx.xx
//give password here
sys
interface g x/x/x
shut
desc free-port
exit
有数百种设备,我不能浪费时间做 100 次以上该死的事情.我需要编写一个自动脚本来完成它.所以我的问题如下:
There are Hundreds of devices for which I cannot waste time doing above damn thing 100 times. I need to write a automated script which does it. so My questions are as follows:
- 我使用 Windows 系统,所以最好使用什么脚本语言:Ruby/shell script/perl ?(我以前是 ROR 开发人员,所以我知道 Ruby、Linux 终端.现在我在网络领域工作.)
- 我的想法是:将所有设备放入一个数组并使用 for 循环,一个一个调用设备并执行上述命令.
- 我没有脚本方面的知识,所以请进一步指导我.我不知道从哪里开始.
- I use Windows system, so What is the best scripting language to be used : Ruby / shell script / perl ? (I was formerly ROR Developer, so i know Ruby, Linux terminal. Now I am working in networking domain. )
- What I thought was : Put all Devices into an array and using for loop, call devices one by one and execute above said commands.
- I don't have knowledge of scripting, so please guide me further. I don't know where to start from.
推荐答案
第一步:决定程序的文件结构.
比如这是最简单的结构
Step 1: decide the file structure of your program.
For example, this is the simplest structure
if_admin/
|--config.yml
|--run.rb
第 2 步:编写一个或一堆配置文件,其中包含您需要在目标上运行的命令的不同部分.
例如,您可以使用这样的 yaml 文件:
Step 2: write a config file or a bunch of config files that contain the different parts of the commands you need to run on the targets.
For example, you can use a yaml file like this:
xx.xx.xx.xx:
password: s3cret
router-shelf: x
slot: x
port: x
yy.yy.yy.yy:
...
第 3 步:实施您想做的事情
require 'yaml'
require 'net/telnet'
config = YAML.load_file('./config.yml')
config.each do |host, conf|
telnet = Net::Telnet.new('Host' => host)
telnet.login(conf['password'])
telnet.puts <<-CMD
sys
interface g #{conf['router-shelf']}/#{conf['slot']}/#{conf['port']}
shut
desc free-port
CMD
telnet.close
end
这篇关于编写自动化脚本来配置设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!