赞
踩
使用 Eve-Ng 模拟器进行RS Lab试验时,经常会对路由器、交换机等进行初始化配置。这部分重复操作可以采用脚本方式完成。(仅供参考)
代码:
- # !/usr/bin/python3
- # -*- coding=utf-8 -*-
- # @Author : zunsi
- # @file: eve-ng设备批量初始化-批量版.py
- # @time: 2020-04-13 16:06:32
-
- from telnetlib import Telnet
- import time
- import os
-
-
- cisco_config = ['0',
- 'ip domain name zuneveng.com',
- 'enable secret cisc0123',
- 'username cisco password cisc0123!',
- 'line vty 0 4',
- 'login local',
- 'transport input ssh',
- 'exit',
- 'crypto key generate rsa',
- '1024',
- 'ip route 0.0.0.0 0.0.0.0 10.10.10.1',
- 'interface e0/0',
- 'no shutdown',
- 'end',
- 'write memory']
-
-
- def cisco_telnet(ip, port, cisco_config):
- print('\n开始连接{}的{}~~~'.format(ip, port))
- cisco_telnet = Telnet(ip, port)
-
- print('\n连接已建立~~~')
- print('\n写入数据中~~~')
-
- cisco_telnet.write(b'\n')
- cisco_telnet.read_until(b'[yes/no]:')
- cisco_telnet.write((b'no' + b'\n'))
- cisco_telnet.read_until(b'Press RETURN to get started!')
- time.sleep(2)
-
- i = 0
- while i < 10:
- cisco_telnet.write(b'\r\n')
- i += 1
-
- cisco_telnet.write(b'enable' + b'\n')
- cisco_telnet.read_until(b'Router#')
-
- print('#' * 70)
- print('\n进入配置模式~~~\n')
-
- cisco_telnet.write(b'configure t' + b'\n')
-
- for each_command in cisco_config:
- cisco_telnet.write(each_command.encode('utf-8') + b'\n')
- time.sleep(1)
-
- print('#' * 70)
- print('\n保存配置中~~~\n')
- cisco_telnet.write(b'exit' + b'\n')
- cisco_telnet.write(b'write momory' + b'\n')
- time.sleep(2)
- cisco_telnet.close()
- print('\n配置完成~~~\n')
- print('#' * 70)
-
-
- print('#' * 10)
- eveng_ip = input('\n请输入eve-ng地址: ')
-
- print('#' * 10)
- print('请输入当前账户的ID, 即POD号码, 此号码可以在EVE-NG左侧菜单中点击Status即可看到, 默认账户admin的POD号码为0. ')
- account_POD = input('\n请输入POD的号码:')
-
- print('#' * 10)
- print('请输入司刻设备名称, 并以英文逗号分隔, 例如R1,R2,R3,R4等')
- cisco_device_id = input('\n请输入设备名称: ').split(',')
-
- print('#' * 10)
- print('请输入管理网段的其实地址和掩码, \n例如起始地址为192.168.3.30/24, 那么第一台设备ip则为192.168.3.31/24')
- mgmt_subnet = input('\n 请输入起始地址和掩码: ')
-
- print('#' * 10)
- print("请输入管理网关ip地址,例如192.168.1.1")
- mgmt_gateway = input('\n请输入网关地址: ')
-
- print('#' * 10)
- print('\n生成设备列表中')
-
-
- cisco_detail = {}
- i = 0
- while i < len(cisco_device_id):
- cisco_detail[cisco_device_id[i]] = [cisco_device_id[i+1], 32768 + int(account_POD) + int(cisco_device_id[i+1])]
- i += 2
-
- print('#'*10)
- print('\n信息汇总如下: ')
- print('\nEVE-NG IP地址: {}'.format(eveng_ip))
- print('\n账号POD值: {}'.format(account_POD))
- print('\n管理网段: {}'.format(mgmt_subnet))
- print("\n网关地址: {}\n".format(mgmt_gateway))
-
- for key,value in cisco_detail.items():
- print('#' * 10)
- print('\n开始连接设备~~~')
- print('\n连接设备{}中,端口: {}'.format(key, value[1]))
- cisco_config[0] = 'hostname' + key
- cisco_config[10] = 'ip route 0.0.0.0 0.0.0.0 {}'.format(mgmt_gateway)
- cisco_config[12] = 'ip address {} {}'.format(ipaddress.IPv4Interface(mgmt_subnet).ip + int(value[0]), ip address.IPv4Interface(mgmt_subnet).netmask)
- cisco_telnet(eveng_ip,value[1],cisco_config)
-
- print('\n初始化完毕, 开始测试连接\n')
- print('#' * 10)
- print('\n从本机ping测试设备: {}'.format(key))
- os.system('ping -c 2 {}'.format(ipaddress.IPv4Interface(mgmt_subnet).ip + int(value[0])))
-
- print('\n完成退出~~~')
-
-
-
- print('#' * 10)
-
-
-
-
- cisco_device_id = 1
- device_port = 32768 + (128 * int(account_POD)) + int(cisco_device_id)
- hostname = 'R' + str(32768 + (128 * int(account_POD)) + int(cisco_device_id))
-
-
-
- if __name__ == "__main__":
- pass

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。