を行うことができます(これらのスクリプトを実行する前PyWin32 extensionsとWMIモジュールをインストールします)。ここでは、ハードウェアデバイスと通信するために物事を設定する方法である:
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])
ここでは(DHCP経由)IPアドレスを自動的に取得するに戻す方法をされています
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()、およびEnableDHCP()によって返される値をチェックする必要があります。注:EnableStatic()およびSetGateways()の場合は、エラーコードがリストとして返されます)(「0」は成功を意味し、「1」は再起動が必要なことを意味し、メソッド名によってリンクされたMSDNページには他の値が記述されます。
Win32NetworkAdapterConfigurationクラスのすべての機能に関する完全な情報は、found on MSDNでもかまいません。
注:私はこれをPython 2.7でテストしましたが、PyWin32とWMIモジュールがPython 3で利用可能であるため、文字列リテラルの前から "u"を削除することでPython 3のこの機能を利用できるはずです。
関連する質問:http://stackoverflow.com/questions/83756/how-to-programmatically-enable-disable-network-interfaces-windows-xp – tzot