当前位置:   article > 正文

Python -- 脚本初始化 Eve-Ng模拟器的设备配置_eveng 初始化

eveng 初始化

使用 Eve-Ng 模拟器进行RS Lab试验时,经常会对路由器、交换机等进行初始化配置。这部分重复操作可以采用脚本方式完成。(仅供参考)

代码:

  1. # !/usr/bin/python3
  2. # -*- coding=utf-8 -*-
  3. # @Author : zunsi
  4. # @file: eve-ng设备批量初始化-批量版.py
  5. # @time: 2020-04-13 16:06:32
  6. from telnetlib import Telnet
  7. import time
  8. import os
  9. cisco_config = ['0',
  10. 'ip domain name zuneveng.com',
  11. 'enable secret cisc0123',
  12. 'username cisco password cisc0123!',
  13. 'line vty 0 4',
  14. 'login local',
  15. 'transport input ssh',
  16. 'exit',
  17. 'crypto key generate rsa',
  18. '1024',
  19. 'ip route 0.0.0.0 0.0.0.0 10.10.10.1',
  20. 'interface e0/0',
  21. 'no shutdown',
  22. 'end',
  23. 'write memory']
  24. def cisco_telnet(ip, port, cisco_config):
  25. print('\n开始连接{}的{}~~~'.format(ip, port))
  26. cisco_telnet = Telnet(ip, port)
  27. print('\n连接已建立~~~')
  28. print('\n写入数据中~~~')
  29. cisco_telnet.write(b'\n')
  30. cisco_telnet.read_until(b'[yes/no]:')
  31. cisco_telnet.write((b'no' + b'\n'))
  32. cisco_telnet.read_until(b'Press RETURN to get started!')
  33. time.sleep(2)
  34. i = 0
  35. while i < 10:
  36. cisco_telnet.write(b'\r\n')
  37. i += 1
  38. cisco_telnet.write(b'enable' + b'\n')
  39. cisco_telnet.read_until(b'Router#')
  40. print('#' * 70)
  41. print('\n进入配置模式~~~\n')
  42. cisco_telnet.write(b'configure t' + b'\n')
  43. for each_command in cisco_config:
  44. cisco_telnet.write(each_command.encode('utf-8') + b'\n')
  45. time.sleep(1)
  46. print('#' * 70)
  47. print('\n保存配置中~~~\n')
  48. cisco_telnet.write(b'exit' + b'\n')
  49. cisco_telnet.write(b'write momory' + b'\n')
  50. time.sleep(2)
  51. cisco_telnet.close()
  52. print('\n配置完成~~~\n')
  53. print('#' * 70)
  54. print('#' * 10)
  55. eveng_ip = input('\n请输入eve-ng地址: ')
  56. print('#' * 10)
  57. print('请输入当前账户的ID, 即POD号码, 此号码可以在EVE-NG左侧菜单中点击Status即可看到, 默认账户admin的POD号码为0. ')
  58. account_POD = input('\n请输入POD的号码:')
  59. print('#' * 10)
  60. print('请输入司刻设备名称, 并以英文逗号分隔, 例如R1,R2,R3,R4等')
  61. cisco_device_id = input('\n请输入设备名称: ').split(',')
  62. print('#' * 10)
  63. print('请输入管理网段的其实地址和掩码, \n例如起始地址为192.168.3.30/24, 那么第一台设备ip则为192.168.3.31/24')
  64. mgmt_subnet = input('\n 请输入起始地址和掩码: ')
  65. print('#' * 10)
  66. print("请输入管理网关ip地址,例如192.168.1.1")
  67. mgmt_gateway = input('\n请输入网关地址: ')
  68. print('#' * 10)
  69. print('\n生成设备列表中')
  70. cisco_detail = {}
  71. i = 0
  72. while i < len(cisco_device_id):
  73. cisco_detail[cisco_device_id[i]] = [cisco_device_id[i+1], 32768 + int(account_POD) + int(cisco_device_id[i+1])]
  74. i += 2
  75. print('#'*10)
  76. print('\n信息汇总如下: ')
  77. print('\nEVE-NG IP地址: {}'.format(eveng_ip))
  78. print('\n账号POD值: {}'.format(account_POD))
  79. print('\n管理网段: {}'.format(mgmt_subnet))
  80. print("\n网关地址: {}\n".format(mgmt_gateway))
  81. for key,value in cisco_detail.items():
  82. print('#' * 10)
  83. print('\n开始连接设备~~~')
  84. print('\n连接设备{}中,端口: {}'.format(key, value[1]))
  85. cisco_config[0] = 'hostname' + key
  86. cisco_config[10] = 'ip route 0.0.0.0 0.0.0.0 {}'.format(mgmt_gateway)
  87. cisco_config[12] = 'ip address {} {}'.format(ipaddress.IPv4Interface(mgmt_subnet).ip + int(value[0]), ip address.IPv4Interface(mgmt_subnet).netmask)
  88. cisco_telnet(eveng_ip,value[1],cisco_config)
  89. print('\n初始化完毕, 开始测试连接\n')
  90. print('#' * 10)
  91. print('\n从本机ping测试设备: {}'.format(key))
  92. os.system('ping -c 2 {}'.format(ipaddress.IPv4Interface(mgmt_subnet).ip + int(value[0])))
  93. print('\n完成退出~~~')
  94. print('#' * 10)
  95. cisco_device_id = 1
  96. device_port = 32768 + (128 * int(account_POD)) + int(cisco_device_id)
  97. hostname = 'R' + str(32768 + (128 * int(account_POD)) + int(cisco_device_id))
  98. if __name__ == "__main__":
  99. pass

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号