diff options
| author | 赵坤 <[email protected]> | 2024-05-28 08:42:39 +0000 |
|---|---|---|
| committer | 赵坤 <[email protected]> | 2024-05-28 08:42:39 +0000 |
| commit | 50d6f37067d2a558401bb91a4787a8d90bed17c4 (patch) | |
| tree | 0577e5c3b3dcb68d3deb63176a4cd0835cc0218b | |
| parent | d46a761482dcc60e7f7ba859603f88df20d6e708 (diff) | |
上传新文件
| -rw-r--r-- | install_trex.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/install_trex.py b/install_trex.py new file mode 100644 index 0000000..99a2bf3 --- /dev/null +++ b/install_trex.py @@ -0,0 +1,59 @@ +# -*- coding: UTF-8 -*- +import os +import subprocess + +def install_trex(): + try: + # 安装TRex + output = subprocess.check_output("find /etc -name 'trex_cfg.yaml'", shell=True).decode('utf-8') + if len(output) > 0: + print("TRex has been installed.") + else: + subprocess.check_call(["mkdir", "-p", "/opt/trex"]) + os.chdir("/opt/trex") + subprocess.check_call(["wget", "--no-check-certificate", "--no-cache", "https://trex-tgn.cisco.com/trex/release/latest"]) + subprocess.check_call(["tar", "-xzf", "latest"]) + subprocess.check_call(["cp", "/opt/trex/v3.04/cfg/simple_cfg.yaml", "/etc/trex_cfg.yaml"]) + os.chdir("/opt/trex/v3.04") + output = subprocess.check_output("./dpdk_setup_ports.py -s", shell=True).decode('utf-8') + # 注意:eth1和eth2是网卡名称,根据实际情况修改,并且需要保证这两个网卡的防火墙是关闭的 + intf_list = search(output, ["eth2", "eth3"]) + file_path = "/etc/trex_cfg.yaml" + file_overwrite(file_path, intf_list) + print("Install TRex successfully.") + except: + print("Fail to install TRex.") + +def search(output, list): + try: + temp_list = [] + lines = output.split('\n') + for i in range(len(list)): + for line in lines: + if list[i] in line: + temp = line.split() + if temp: + temp_list.append(temp[0]) + print("Search successfully.") + return temp_list + except: + print("Fail to search.") + return temp_list + +def file_overwrite(file_path, replacement_value): + try: + target_value = "interfaces" + with open(file_path, 'r') as file: + lines = file.readlines() + fourth_line_parts = lines[3].split(": ") + if target_value == fourth_line_parts[0].strip(): + temp = "[" + ','.join(f"'{item}'" for item in replacement_value) + "]" + lines[3] = " " + target_value + ": " + temp + "\n" + with open(file_path, 'w') as file: + file.writelines(lines) + print("Overwrite successfully.") + except: + print("Fail to overwrite.") + +if __name__ == '__main__': + install_trex()
\ No newline at end of file |
