我无法使用netsh读取ipv6地址

问题描述 投票:0回答:1

我想创建一个脚本,删除我在以太网适配器中手动设置的所有地址。首先,我想读取 IP 地址。在乞讨时效果很好,但在我尝试删除它们之后,即使读取功能也无法工作。即使重新启动后,我仍然收到相同的错误:

a = subprocess.getoutput(f'netsh interface ipv6 show address LocalNetwork')

接收:

ERROR:cache_util_win.cc(20)] Unable to move the cache: Access is denied. (0x5)
ERROR:disk_cache.cc(205)] Unable to create cache
python subprocess netsh
1个回答
0
投票

问题出在权限或环境上。

尝试进行错误处理:

import subprocess

try:
    command = 'netsh interface ipv6 show address LocalNetwork'
    output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True)
    print(output.decode())
except subprocess.CalledProcessError as e:
    print(f"Command '{e.cmd}' returned non-zero exit status {e.returncode}.")
    print(f"Error message: {e.output.decode()}")
except Exception as e:
    print(f"An unexpected error occurred: {str(e)}")

它使用 subprocess.check_output 而不是 getoutput 来更好地处理错误。

© www.soinside.com 2019 - 2024. All rights reserved.