windows下修改ip地址的脚本
我使用计算机通过以太网与硬件进行通信。为了与此设备通信,我将 IP 设置为 192 168 0 11,子网掩码设置为 255 255 255 0,默认网关设置为 192 168 0 1(IPv4)。为了使用互联网,我通过控制面板选择“自动获取IP地址”。
我想要一个脚本,允许我快速选择一种或另一种以太网设置 - 硬件或互联网。
我主要用 python 编程,但也许有一个批处理文件解决方案。
谢谢,
巴里。
I use my computer to communicate with a piece of hardware via ethernet. To communicate with this device I set my ip to 192 168 0 11, subnet mask to 255 255 255 0, and default gateway to 192 168 0 1 for IPv4. To use the internet, I choose "Obtain an IP address automatically" via control panel.
I'd like to have a script that allows my to quickly choose one or the other ethernet setting - hardware or internet.
I program mostly in python but maybe there is a batch file solution.
Thanks,
Barry.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 subprocess 模块从命令行
启动此模块(不带[params])以获得有关如何使用它的帮助。然后你可以
更新:
对于那些太忙而无法使用rtfm的人,
这里
lan
是要配置的网络接口的名称,192.168.0.100是ip地址,255.255.255.0是网络掩码。第一个命令设置静态地址,第二个命令恢复为 dhcp。You can use the subprocess module to start
Start this from the commandline (without[params]) to get some help how to use it. Then you can do
Update:
For those who's too busy to rtfm,
here
lan
is the name of the network interface to configure, 192.168.0.100 is ip address, 255.255.255.0 is network mask. The first command sets static address, the second reverts to dhcp.实际上非常简单(仅限 Windows)(仅使用预安装的库):
Actually very simple to do (windows only) (uses preinstalled libraries only):
#再次恢复正常
#again back to normal
您可以使用 vbscript 更改 IP 地址,
You can use vbscript to change IP Address,
您可以使用 Python WMI 模块 来执行此操作(安装 PyWin32 扩展 和运行这些脚本之前的 WMI 模块)。以下是如何配置与硬件设备通信的方法:
以下是如何恢复自动获取 IP 地址(通过 DHCP):
注意:在生产脚本中,您应该检查 EnableStatic(), SetGateways() 和 启用 DHCP()。 (“0”表示成功,“1”表示需要重新启动,其他值在方法名称链接到的 MSDN 页面上进行了描述。注意:对于 EnableStatic() 和 SetGateways(),错误代码以列表形式返回)。
有关 Win32NetworkAdapterConfiguration 类所有功能的完整信息也可以在 MSDN 上找到。
注意:我使用 Python 2.7 对此进行了测试,但由于 PyWIn32 和 WMI 模块可用于 Python 3,我相信您应该能够通过删除字符串文字前面的“u”来使其适用于 Python 3。
You can use the Python WMI module to do this (install the PyWin32 extensions and the WMI module before running these scripts). Here is how to configure things to talk to the hardware device:
Here is how to revert to obtaining an IP address automatically (via DHCP):
Note: in a production script you should check the values returned by EnableStatic(), SetGateways() and EnableDHCP(). ('0' means success, '1' means reboot required and other values are described on the MSDN pages linked to by the method names. Note: for EnableStatic() and SetGateways(), the error codes are returned as lists).
Full information on all the functionality of the Win32NetworkAdapterConfiguration class can also be found on MSDN.
Note: I tested this with Python 2.7, but as PyWIn32 and WMI modules are available for Python 3, I believe you should be able to get this working for Python 3 by removing the "u" from before the string literals.