路由拓扑
要求
解题思路:
使用python telnetlib库来实现自动化
import telnetlib
import time
import tqdm
TN=telnetlib.Telnet('127.0.0.1',2000)
clis=['system-view',
'interface GigabitEthernet0/0/0',
' ip address 15.1.1.1 255.255.255.0',
'nat outbound 2000',
'interface GigabitEthernet0/0/1',
' ip address 192.168.1.1 255.255.255.0',
'interface Tunnel0/0/0',
' ip address 10.0.0.1 255.255.255.0',
' undo rip split-horizon',
' tunnel-protocol gre p2mp',
' source 15.1.1.1',
' nhrp entry multicast dynamic',
' nhrp network-id 10',
'interface Tunnel0/0/1',
' ip address 100.0.0.1 255.255.255.0',
' tunnel-protocol gre',
' source 15.1.1.1',
' destination 45.1.1.1',
'rip 1',
' undo summary',
' version 2',
' network 192.168.1.0',
' network 10.0.0.0',
' network 100.0.0.0',
'ip route-static 0.0.0.0 0.0.0.0 15.1.1.2',
]
count_err = []
count_ok = []
def auto(command):
for cli in tqdm.tqdm(command):
TN.write(cli.encode('ascii')+b'\n')
if cli=='system-view':
time.sleep(1)
time.sleep(0.5)
res=TN.read_very_eager().decode('ascii')
if 'Error' in res:
count_err.append(cli)
else:
count_ok.append(cli)
print(f'一共执行了{len(clis)}条命令')
print(f'成功了{len(count_ok)}', f'分别是{count_ok}')
print(f'失败了{len(count_err)}条', f'分别是{count_err}')
TN.close()
if __name__ == '__main__':
auto(clis)
最后pc1也是成功ping通AR5环回,与其他PC