拓扑图:(参照Qos拓扑即可)
实验准备:
1. pythonping 为第三方模块,通过pip下载,VMware虚拟机:pip3.8 install pythonping;
2.新建一个目录:mkdir lab2,在该文件目录下创建实验的python脚本;
3.在 lab2 目录vim 代码1.py
结果:
4.在lab2目录 vim 代码2.py
代码1:
from pythonping import ping import os if os.path.exists('reachable_ip.txt'): os.remove('reachable_ip.txt') third_octet = range(1,3) #IP地址第三网段 last_octet = range(11,14) #IP地址第四网段:例如:192.168.88.10 for ip3 in third_octet: for ip4 in last_octet: ip ='192.168.' + str(ip3) + '.' + str(ip4) ping_result = ping(ip) f =open('reachable_ip.txt','a') if 'Reply' in str(ping_result): print(ip + '可达!') f.write(ip + '\n') else: print(ip + '不可达!') f.close()
代码2:
import paramiko import time import re from datetime import datetime import socket import getpass username = input('用户名:') password = getpass.getpass('密码:') now = datetime.now() date = "%s-%s-%s" % (now.month,now.day,now.year) time_now = "%s:%s:%s" % (now.hour,now.minute,now.second) switch_with_tacacs_issue = [] switch_not_reachable = [] total_number_of_up_port = 0 iplist = open('reachable_ip.txt') number_of_switch = len(iplist.readlines()) total_number_of_up_ports = number_of_switch * 1 #此"1"为下行PC接口数 iplist.seek(0) for line in iplist.readlines(): try: ip = line.strip() ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect(hostname=ip,username=username,password=password) print("\n你已成功连接到" + ip) command = ssh_client.invoke_shell() command.send('term len 0\n') command.send('show ip int b | i up\n') time.sleep(1) output = command.recv(65535).decode() #print(output) search_up_port = re.findall(r'GigabitEthernet',output) number_of_up_port = len(search_up_port) print(ip + "有" + str(number_of_up_port) + "个up端口。") total_number_of_up_port += number_of_up_port except paramiko.ssh_exception.AuthenticationException: print(ip + "TACACS不可用" + "!") switch_with_tacacs_issue.append(ip) except socket.error: print(ip + "不可达!") switch_not_reachable.append(ip) iplist.close() print("\n") print("网络中总共有" + str(total_number_of_up_ports) + "个可用端口。") print("当前有" + str(total_number_of_up_port) + "个up端口。") print("端口开启速度为%.2f%%" % (total_number_of_up_port / float(total_number_of_up_ports) * 100)) print('\n以下交换机TACACS不可用:') for i in switch_with_tacacs_issue: print(i) print('\n以下交换机不可达:') for i in switch_not_reachable: print(i) f = open(date + ".txt","a+") f.write('自' + date + " " + "的" + time_now) f.write("\n\n网络中总共有" +str(total_number_of_up_ports) + "个可用端口。") f.write("\n当前有" + str(total_number_of_up_port) + "个up端口。") f.write("\n端口开启速度为%.2f%%" % (total_number_of_up_port / float(total_number_of_up_ports) * 100)) f.write("\n************************************************************\n\n") f.close()
IP、路由、SSH 配置:
VM虚拟机路由配置:
route add -net 0.0.0.0 netmask 0.0.0.0 gw 192.168.88.10
——————————————————————————
sw1: en conf t host sw1 ip routing int g0/0 no sw ip add 192.168.88.10 255.255.255.0 no shut int g0/1 no sw ip add 192.168.1.10 255.255.255.0 no shu int g0/2 no sw ip add 192.168.2.10 255.255.255.0 no shut ip domain-name cisco.com crypto key generate rsa 1024 username python privilege 15 password 123 enable password 123 ip ssh time-out 120 ip ssh authentication-retries 5 ip ssh version 2 line vty 0 4 transport input all login local end write pc1: en conf t host pc1 ip routing ip route 0.0.0.0 0.0.0.0 192.168.1.10 int g0/0 no sw ip add 192.168.1.11 255.255.255.0 ip domain-name cisco.com crypto key generate rsa 1024 username python privilege 15 password 123 enable password 123 ip ssh time-out 120 ip ssh authentication-retries 5 ip ssh version 2 line vty 0 4 transport input all login local end write pc2: en conf t host pc2 ip routing ip route 0.0.0.0 0.0.0.0 192.168.1.10 int g0/0 no sw ip add 192.168.1.12 255.255.255.0 ip domain-name cisco.com crypto key generate rsa 1024 username python privilege 15 password 123 enable password 123 ip ssh time-out 120 ip ssh authentication-retries 5 ip ssh version 2 line vty 0 4 transport input all login local end write pc3: en conf t host pc3 ip routing ip route 0.0.0.0 0.0.0.0 192.168.1.10 int g0/0 no sw ip add 192.168.1.13 255.255.255.0 ip domain-name cisco.com crypto key generate rsa 1024 username python privilege 15 password 123 enable password 123 ip ssh time-out 120 ip ssh authentication-retries 5 ip ssh version 2 line vty 0 4 transport input all login local end write pc4: en conf t host pc4 ip routing ip route 0.0.0.0 0.0.0.0 192.168.2.10 int g0/0 no sw ip add 192.168.2.11 255.255.255.0 ip domain-name cisco.com crypto key generate rsa 1024 username python privilege 15 password 123 enable password 123 ip ssh time-out 120 ip ssh authentication-retries 5 ip ssh version 2 line vty 0 4 transport input all login local end write pc5: en conf t host pc5 ip routing ip route 0.0.0.0 0.0.0.0 192.168.2.10 int g0/0 no sw ip add 192.168.2.12 255.255.255.0 ip domain-name cisco.com crypto key generate rsa 1024 username python privilege 15 password 123 enable password 123 ip ssh time-out 120 ip ssh authentication-retries 5 ip ssh version 2 line vty 0 4 transport input all login local end write pc6: en conf t host pc6 ip routing ip route 0.0.0.0 0.0.0.0 192.168.2.10 int g0/0 no sw ip add 192.168.2.13 255.255.255.0 ip domain-name cisco.com crypto key generate rsa 1024 username python privilege 15 password 123 enable password 123 ip ssh time-out 120 ip ssh authentication-retries 5 ip ssh version 2 line vty 0 4 transport input all login local end write