wireshark和pyshark关于tshark.exe目录的问题

本文介绍了解决PyShark在使用过程中找不到TShark路径的问题,主要原因是Wireshark未安装在默认路径。通过修改Python安装路径下Libsite-packagespyshark中的config.ini文件来指定正确的TShark路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

报错内容:
pyshark.tshark.tshark.TSharkNotFoundException: TShark not found. Try adding its location to the configuration file. Searched these paths: ['C:\\Program Files\\Wireshark\\tshark.exe', 'C:\\Program Files (x86)\\Wireshark\\tshark.exe', 'C:\\Program Files\\Wireshark\\tshark.exe']


出现该报错的主要原因是因为没有找到tshark.exe,没找到这个是因为你的wireshark没有安装在默认路径,如果重新安装比较麻烦,我们可以去修改config.ini中的tshark.exe的路径。


config.ini路径在python安装路径的\Lib\site-packages\pyshark在这里插入图片描述
修改前,我们先把原版备份一下,以备不时之需!
原版:

[tshark]
# Specify the path to the tshark executable.
# If the configured path does not exist, these locations will be searched:
# (Linux): /usr/bin/tshark
# (Linux): /usr/sbin/tshark
# (Linux): /usr/lib/tshark/tshark
# (Linux): /usr/local/bin/tshark
# (Windows): %ProgramFiles%\Wireshark\tshark.exe
# (Windows): %ProgramFiles(x86)%\Wireshark\tshark.exe
tshark_path = C:\Program Files\Wireshark\tshark.exe

[dumpcap]
dumpcap_path = C:\Program Files\Wireshark\dumpcap.exe

修改后:
在这里插入图片描述简单的代码终于没有出现问题了
在这里插入图片描述


希望能解决大家的问题,拜拜!!!!!!!!!!!!!!

Traceback (most recent call last): File "/data/user/0/com.cscjapp.python/files/ide_files/ide_run.py", line 31, in <module> start(fakepyfile,mainpyfile) File "/data/user/0/com.cscjapp.python/files/ide_files/ide_run.py", line 30, in start exec(open(mainpyfile).read(), __main__.__dict__) File "<string>", line 3, in <module> File "/data/user/0/com.cscjapp.python/files/aarch64-linux-android/lib/python3.11/site-packages/pyshark/capture/capture.py", line 212, in _packets_from_tshark_sync tshark_process = existing_process or self.eventloop.run_until_complete( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/data/user/0/com.cscjapp.python/files/aarch64-linux-android/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "/data/user/0/com.cscjapp.python/files/aarch64-linux-android/lib/python3.11/site-packages/pyshark/capture/capture.py", line 340, in _get_tshark_process parameters = [self._get_tshark_path(), "-l", "-n", "-T", output_type] + \ ^^^^^^^^^^^^^^^^^^^^^^^ File "/data/user/0/com.cscjapp.python/files/aarch64-linux-android/lib/python3.11/site-packages/pyshark/capture/capture.py", line 315, in _get_tshark_path return get_process_path(self.tshark_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/data/user/0/com.cscjapp.python/files/aarch64-linux-android/lib/python3.11/site-packages/pyshark/tshark/tshark.py", line 70, in get_process_path raise TSharkNotFoundException( pyshark.tshark.tshark.TSharkNotFoundException: TShark not found. Try adding its location to the configuration file. Searched these paths: ['C:\\Program Files\\Wireshark\\tshark.exe', '/data/user/0/com.cscjapp.python/files/tshark', '/data/user/0/com.cscjapp.python/files/bin/tshark', '/data/user/0/com.cscjapp.python/files/aarch64-linux-android/bin/tshark', '/busybox-virtual/tshark', '/product/bin/tshark', '/apex/com.android.runtime/bin/tshark', '/apex/com.android.art/bin/tshark', '/system_ext/bin/tshark', '/system/bin/tshark', '/vendor/bin/tshark'] /storage/emulated/0/Android/data/com.cscjapp.python/files/CJ_IDE/PythonProject/默认目录/Helloworld/src $
06-10
### 解决 TSharkNotFoundException 错误并配置 tshark 路径 在使用 `pyshark` 时,如果出现 `TSharkNotFoundException` 错误,通常是因为系统无法找到 `tshark.exe` 的路径。以下是解决此问题的详细方法: #### 方法一:直接指定 `tshark_path` 可以通过在调用 `FileCapture` 或 `LiveCapture` 时,直接传入 `tshark_path` 参数来指定 `tshark.exe` 的路径[^2]。例如: ```python import pyshark cap = pyshark.FileCapture("example.pcap", tshark_path="E:\\Wireshark64\\Wireshark\\tshark.exe") for packet in cap: print(packet) ``` #### 方法二:修改 `config.ini` 文件 如果希望永久性解决问题,可以修改 `pyshark` 的配置文件 `config.ini`,以指定正确的 `tshark_path`。具体操作如下: 1. 找到 `config.ini` 文件的位置。通常位于 Python 安装目录下的 `\Lib\site-packages\pyshark` 文件夹中。 2. 备份原始的 `config.ini` 文件以防万一。 3. 编辑 `config.ini` 文件,将 `tshark_path` `dumpcap_path` 修改为实际安装路径。例如: ```ini [tshark] # Specify the path to the tshark executable. tshark_path = C:\Program Files\Wireshark\tshark.exe [dumpcap] dumpcap_path = C:\Program Files\Wireshark\dumpcap.exe ``` 完成上述修改后保存文件即可[^3]。 #### 方法三:将 `tshark.exe` 添加到系统环境变量 另一种解决方案是将 `tshark.exe` 所在的目录添加到系统的 `PATH` 环境变量中。这样,系统能够在任何地方找到 `tshark.exe`,从而避免路径错误。具体步骤如下: 1. 打开系统属性对话框,选择“高级系统设置”。 2. 在“系统属性”窗口中,点击“环境变量”按钮。 3. 在“系统变量”部分,找到并编辑 `PATH` 变量,添加 `tshark.exe` 的完整路径(如 `C:\Program Files\Wireshark`)。 4. 保存更改并重新启动终端或 IDE。 通过这种方法,无需每次运行代码时都指定路径,也不需要修改 `config.ini` 文件[^1]。 #### 注意事项 - 如果仍然遇到问题,请确认 Wireshark 是否已正确安装,并确保 `tshark.exe` 文件确实存在于指定路径中。 - 使用非默认路径安装 Wireshark 时,必须明确指定 `tshark_path`,因为 `pyshark` 默认只会搜索常见的安装位置。 ```python import pyshark # 示例:检查 tshark 是否可用 try: pyshark.tshark.get_tshark_path() print("TShark found successfully.") except pyshark.tshark.tshark.TSharkNotFoundException: print("TShark not found. Please configure the path correctly.") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值