简体   繁体   English

python中有没有一种方法可以访问Windows中名为“网络连接”的IP地址? 例如:“以太网”或“本地连接”

[英]Is there a method in python for accessing the IP addresses of named “Network Connections” in Windows? Ex: “Ethernet” or “Local Area Connection”

I just started using Python the other day so forgive me if this is a dumb question.我前几天刚开始使用 Python,所以如果这是一个愚蠢的问题,请原谅我。 I am creating a small functional windows application via Tkinter that will allow myself and my coworkers to quickly change our IP addresses with the click of a button.我正在通过 Tkinter 创建一个小型功能 Windows 应用程序,它允许我自己和我的同事通过单击按钮快速更改我们的 IP 地址。 This feature already works, however, I want to create a label in my window that displays the computer's Ethernet IP address upon opening the application and upon clicking a button to change the IP to show that it actually worked.此功能已经可用,但是,我想在我的窗口中创建一个标签,在打开应用程序和单击按钮更改 IP 以显示它实际工作时显示计算机的以太网 IP 地址。 I found a library called "Ifaddr" that I thought would work, but instead of displaying the connection name like "Ethernet" or "Local Area Connection" it displays the adapter name like "Realtek PCIe 2.5GbE Family Controller".我找到了一个名为“Ifaddr”的库,我认为它可以工作,但它显示的不是像“以太网”或“本地连接”这样的连接名称,而是像“Realtek PCIe 2.5GbE 系列控制器”这样的适配器名称。 The only problem with this is that not all of my coworkers have laptops with a Realtek Controller some are Intel or what have you.唯一的问题是,并非我所有的同事都有配备 Realtek 控制器的笔记本电脑,有些是英特尔的,或者你有什么。 Any solutions would be very appreciated.任何解决方案将不胜感激。

Thanks to @acw1668 for your comment, but I think I may have found an even better solution than what those answers suggest.感谢@acw1668 的评论,但我想我可能找到了比这些答案所建议的更好的解决方案。 I found this article https://janakiev.com/blog/python-shell-commands/ and after reading I tried using the os.popen command and it worked just fine, and the best part is you can use the standard library to achieve what I wanted.我发现这篇文章https://janakiev.com/blog/python-shell-commands/阅读后我尝试使用 os.popen 命令并且它工作得很好,最好的部分是你可以使用标准库来实现我想要什么。 Here is an example:下面是一个例子:

import os
ethernetset1=os.popen("netsh interface ip show addresses Ethernet").read().split('\n')
print (ethernetset1)

Which results in:结果是:

['', 'Configuration for interface "Ethernet"', '    DHCP enabled:                         No', '    IP Address:                           10.74.126.120', '    Subnet Prefix:                        10.74.124.0/22 (mask 255.255.252.0)', '    Default Gateway:                      0.0.0.0', '    Gateway Metric:                       1', '    InterfaceMetric:                      35', '', '']

This is great in my opinion because you can print all the details about a connection name and even access the list elements to get what you want(IP, Gateway, Mask).我认为这很棒,因为您可以打印有关连接名称的所有详细信息,甚至可以访问列表元素以获取所需内容(IP、网关、掩码)。 Is this the most ideal way to do this?这是最理想的方法吗? I can't answer that as I'm new to programming in general.我无法回答这个问题,因为我一般不熟悉编程。 Hopefully this will help someone in the future.希望这会在将来对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM