// 切换至 R1 switch_router localhost 5000 // 伪指令,切换到localhost:5000 // 根据 server_settings localhost:5000 是指第1个路由器,即为 R1 config t int s1/0 ip address 11.11.88.1 255.255.255.252 no shutdown exit int loopback0 ip address 11.11.0.1 255.255.255.255 end write show ip int b
// 切换至 R2 switch_router localhost 5001 config t int s1/1 ip address 11.11.88.2 255.255.255.252 no shutdown exit int s1/0 ip address 11.11.88.5 255.255.255.252 no shutdown exit int loopback0 ip address 11.11.0.2 255.255.255.255 end write show ip int b
defload_commands_from_file(filename): withopen(filename, 'r', encoding='utf-8') as file: commands = [line.strip() for line in file.readlines() if line.strip()] return commands
defprocess_commands(commands): global HOSTNAME, FIRSTFLAG tn = None for command in commands: command = command.split('//')[0].strip() ifnot command: continue # 伪指令:切换路由器 switch_router if command.startswith('switch_router'): _, new_host, new_port = command.split() HOSTNAME = f"R{int(new_port) - 4999}" FIRSTFLAG = True if tn: tn.write(b"exit\r") print("exit") tn.close() print(f"\n[NOTE] Switching to router {new_host} on port {new_port}") tn = telnet_login_and_wait_for_prompt(new_host, int(new_port)) else: if tn: send_telnet_command(tn, command) else: print("[Error] No active Telnet session. Skipping command.") return tn
# if __name__ == "__main__": # with open("output.txt", "w") as file: # # 重定向标准输出到文件 # sys.stdout = file # # 示例打印内容 # print("This will be written to the file.") # print("Another line in the file.") # configure_router() # print("exit\n[Success] Configure finished") if __name__ == "__main__": configure_router() print("exit\n[Success] Configure finished")