• 《思科 - GNS3 - Pythonping》


    拓扑图:(参照Qos拓扑即可)

    实验准备:

    1. pythonping 为第三方模块,通过pip下载,VMware虚拟机:pip3.8 install pythonping

    2.新建一个目录:mkdir  lab2,在该文件目录下创建实验的python脚本;

    3.在 lab2 目录vim 代码1.py

    结果:

    4.在lab2目录 vim 代码2.py

    代码1:

    1. from pythonping import ping
    2. import os
    3. if os.path.exists('reachable_ip.txt'):
    4. os.remove('reachable_ip.txt')
    5. third_octet = range(1,3) #IP地址第三网段
    6. last_octet = range(11,14) #IP地址第四网段:例如:192.168.88.10
    7. for ip3 in third_octet:
    8. for ip4 in last_octet:
    9. ip ='192.168.' + str(ip3) + '.' + str(ip4)
    10. ping_result = ping(ip)
    11. f =open('reachable_ip.txt','a')
    12. if 'Reply' in str(ping_result):
    13. print(ip + '可达!')
    14. f.write(ip + '\n')
    15. else:
    16. print(ip + '不可达!')
    17. f.close()

    代码2:

    1. import paramiko
    2. import time
    3. import re
    4. from datetime import datetime
    5. import socket
    6. import getpass
    7. username = input('用户名:')
    8. password = getpass.getpass('密码:')
    9. now = datetime.now()
    10. date = "%s-%s-%s" % (now.month,now.day,now.year)
    11. time_now = "%s:%s:%s" % (now.hour,now.minute,now.second)
    12. switch_with_tacacs_issue = []
    13. switch_not_reachable = []
    14. total_number_of_up_port = 0
    15. iplist = open('reachable_ip.txt')
    16. number_of_switch = len(iplist.readlines())
    17. total_number_of_up_ports = number_of_switch * 1
    18. #此"1"为下行PC接口数
    19. iplist.seek(0)
    20. for line in iplist.readlines():
    21. try:
    22. ip = line.strip()
    23. ssh_client = paramiko.SSHClient()
    24. ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    25. ssh_client.connect(hostname=ip,username=username,password=password)
    26. print("\n你已成功连接到" + ip)
    27. command = ssh_client.invoke_shell()
    28. command.send('term len 0\n')
    29. command.send('show ip int b | i up\n')
    30. time.sleep(1)
    31. output = command.recv(65535).decode()
    32. #print(output)
    33. search_up_port = re.findall(r'GigabitEthernet',output)
    34. number_of_up_port = len(search_up_port)
    35. print(ip + "有" + str(number_of_up_port) + "个up端口。")
    36. total_number_of_up_port += number_of_up_port
    37. except paramiko.ssh_exception.AuthenticationException:
    38. print(ip + "TACACS不可用" + "!")
    39. switch_with_tacacs_issue.append(ip)
    40. except socket.error:
    41. print(ip + "不可达!")
    42. switch_not_reachable.append(ip)
    43. iplist.close()
    44. print("\n")
    45. print("网络中总共有" + str(total_number_of_up_ports) + "个可用端口。")
    46. print("当前有" + str(total_number_of_up_port) + "个up端口。")
    47. print("端口开启速度为%.2f%%" % (total_number_of_up_port / float(total_number_of_up_ports) * 100))
    48. print('\n以下交换机TACACS不可用:')
    49. for i in switch_with_tacacs_issue:
    50. print(i)
    51. print('\n以下交换机不可达:')
    52. for i in switch_not_reachable:
    53. print(i)
    54. f = open(date + ".txt","a+")
    55. f.write('自' + date + " " + "的" + time_now)
    56. f.write("\n\n网络中总共有" +str(total_number_of_up_ports) + "个可用端口。")
    57. f.write("\n当前有" + str(total_number_of_up_port) + "个up端口。")
    58. f.write("\n端口开启速度为%.2f%%" % (total_number_of_up_port / float(total_number_of_up_ports) * 100))
    59. f.write("\n************************************************************\n\n")
    60. f.close()

    IP、路由、SSH 配置:

    VM虚拟机路由配置:

    route add -net 0.0.0.0 netmask 0.0.0.0 gw 192.168.88.10

    ——————————————————————————

    1. sw1:
    2. en
    3. conf t
    4. host sw1
    5. ip routing
    6. int g0/0
    7. no sw
    8. ip add 192.168.88.10 255.255.255.0
    9. no shut
    10. int g0/1
    11. no sw
    12. ip add 192.168.1.10 255.255.255.0
    13. no shu
    14. int g0/2
    15. no sw
    16. ip add 192.168.2.10 255.255.255.0
    17. no shut
    18. ip domain-name cisco.com
    19. crypto key generate rsa
    20. 1024
    21. username python privilege 15 password 123
    22. enable password 123
    23. ip ssh time-out 120
    24. ip ssh authentication-retries 5
    25. ip ssh version 2
    26. line vty 0 4
    27. transport input all
    28. login local
    29. end
    30. write
    31. pc1:
    32. en
    33. conf t
    34. host pc1
    35. ip routing
    36. ip route 0.0.0.0 0.0.0.0 192.168.1.10
    37. int g0/0
    38. no sw
    39. ip add 192.168.1.11 255.255.255.0
    40. ip domain-name cisco.com
    41. crypto key generate rsa
    42. 1024
    43. username python privilege 15 password 123
    44. enable password 123
    45. ip ssh time-out 120
    46. ip ssh authentication-retries 5
    47. ip ssh version 2
    48. line vty 0 4
    49. transport input all
    50. login local
    51. end
    52. write
    53. pc2:
    54. en
    55. conf t
    56. host pc2
    57. ip routing
    58. ip route 0.0.0.0 0.0.0.0 192.168.1.10
    59. int g0/0
    60. no sw
    61. ip add 192.168.1.12 255.255.255.0
    62. ip domain-name cisco.com
    63. crypto key generate rsa
    64. 1024
    65. username python privilege 15 password 123
    66. enable password 123
    67. ip ssh time-out 120
    68. ip ssh authentication-retries 5
    69. ip ssh version 2
    70. line vty 0 4
    71. transport input all
    72. login local
    73. end
    74. write
    75. pc3:
    76. en
    77. conf t
    78. host pc3
    79. ip routing
    80. ip route 0.0.0.0 0.0.0.0 192.168.1.10
    81. int g0/0
    82. no sw
    83. ip add 192.168.1.13 255.255.255.0
    84. ip domain-name cisco.com
    85. crypto key generate rsa
    86. 1024
    87. username python privilege 15 password 123
    88. enable password 123
    89. ip ssh time-out 120
    90. ip ssh authentication-retries 5
    91. ip ssh version 2
    92. line vty 0 4
    93. transport input all
    94. login local
    95. end
    96. write
    97. pc4:
    98. en
    99. conf t
    100. host pc4
    101. ip routing
    102. ip route 0.0.0.0 0.0.0.0 192.168.2.10
    103. int g0/0
    104. no sw
    105. ip add 192.168.2.11 255.255.255.0
    106. ip domain-name cisco.com
    107. crypto key generate rsa
    108. 1024
    109. username python privilege 15 password 123
    110. enable password 123
    111. ip ssh time-out 120
    112. ip ssh authentication-retries 5
    113. ip ssh version 2
    114. line vty 0 4
    115. transport input all
    116. login local
    117. end
    118. write
    119. pc5:
    120. en
    121. conf t
    122. host pc5
    123. ip routing
    124. ip route 0.0.0.0 0.0.0.0 192.168.2.10
    125. int g0/0
    126. no sw
    127. ip add 192.168.2.12 255.255.255.0
    128. ip domain-name cisco.com
    129. crypto key generate rsa
    130. 1024
    131. username python privilege 15 password 123
    132. enable password 123
    133. ip ssh time-out 120
    134. ip ssh authentication-retries 5
    135. ip ssh version 2
    136. line vty 0 4
    137. transport input all
    138. login local
    139. end
    140. write
    141. pc6:
    142. en
    143. conf t
    144. host pc6
    145. ip routing
    146. ip route 0.0.0.0 0.0.0.0 192.168.2.10
    147. int g0/0
    148. no sw
    149. ip add 192.168.2.13 255.255.255.0
    150. ip domain-name cisco.com
    151. crypto key generate rsa
    152. 1024
    153. username python privilege 15 password 123
    154. enable password 123
    155. ip ssh time-out 120
    156. ip ssh authentication-retries 5
    157. ip ssh version 2
    158. line vty 0 4
    159. transport input all
    160. login local
    161. end
    162. write

  • 相关阅读:
    JPA联合主键
    freertos简单串口
    Redis缓存穿透,背八股文 居然没用!!!
    Win10常用快捷键
    MediaPlayer_Analyze-3-native
    【数据结构】建堆的方式、堆排序以及TopK问题
    TrendMicro:Apex One Server 工具文件夹
    国家开放大学 模拟试题训练
    Spring Cloud入门看这一篇就够了
    趣味问题《寻人启事》的Python程序解决
  • 原文地址:https://blog.csdn.net/weixin_57099902/article/details/127552234