windows下修改ip地址的脚本 [英] Script to change ip address on windows

查看:26
本文介绍了windows下修改ip地址的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用我的计算机通过以太网与硬件进行通信.为了与这个设备通信,我将我的 ip 设置为 192 168 0 11,子网掩码设置为 255 255 255 0,并将默认网关设置为 IPv4 的 192 168 0 1.要使用互联网,我通过控制面板选择自动获取 IP 地址".

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.

我主要用 python 编程,但也许有批处理文件解决方案.

I program mostly in python but maybe there is a batch file solution.

谢谢,

巴里.

推荐答案

您可以使用 Python WMI 模块 执行此操作(在运行这些脚本之前安装 PyWin32 扩展 和 WMI 模块).以下是配置与硬件设备通信的方法:

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:

import wmi

# Obtain network adaptors configurations
nic_configs = wmi.WMI().Win32_NetworkAdapterConfiguration(IPEnabled=True)

# First network adaptor
nic = nic_configs[0]

# IP address, subnetmask and gateway values should be unicode objects
ip = u'192.168.0.11'
subnetmask = u'255.255.255.0'
gateway = u'192.168.0.1'

# Set IP address, subnetmask and default gateway
# Note: EnableStatic() and SetGateways() methods require *lists* of values to be passed
nic.EnableStatic(IPAddress=[ip],SubnetMask=[subnetmask])
nic.SetGateways(DefaultIPGateway=[gateway])

这里是如何恢复到自动获取 IP 地址(通过 DHCP):

Here is how to revert to obtaining an IP address automatically (via DHCP):

import wmi

# Obtain network adaptors configurations
nic_configs = wmi.WMI().Win32_NetworkAdapterConfiguration(IPEnabled=True)

# First network adaptor
nic = nic_configs[0]

# Enable DHCP
nic.EnableDHCP()

注意:在生产脚本中,您应该检查返回的值EnableStatic()SetGateways()启用DHCP().('0' 表示成功,'1' 表示需要重新启动,其他值在方法名称链接到的 MSDN 页面上进行了描述.注意:对于 EnableStatic() 和 SetGateways(),错误代码以列表形式返回).

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).

关于 Win32NetworkAdapterConfiguration 类的所有功能的完整信息也可以在 上找到MSDN.

Full information on all the functionality of the Win32NetworkAdapterConfiguration class can also be found on MSDN.

注意:我使用 Python 2.7 对此进行了测试,但由于 PyWIn32 和 WMI 模块可用于 Python 3,我相信您应该能够通过从字符串文字之前删除u"来使其适用于 Python 3.

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.

这篇关于windows下修改ip地址的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆