Windows XP のスクリプトからネットワーク インターフェイスを完全に有効/無効にする必要があります。Python ソリューションを探していますが、一般的な方法 (WMI、netsh のコマンドライン、Windows 呼び出しなど) は歓迎され、調整されます。ありがとう。
7 に答える
netshインターフェースの使用使用法setinterface[name =] IfName [[admin =] ENABLED | DISABLED [connect =] CONNECTED | DISCONNECTED [newname =] NewName]
外側の括弧内にすべてを含めてみてください:netsh interface set interface name = "thename" admin = disable connect = DISCONNECTED newname = "thename"
次のMSKBページも参照してください。http://support.microsoft.com/kb/262265/ どちらの提案に従うこともできます。アダプタを無効にするには、ハードウェアデバイスを参照する方法を決定する必要があります。コンピューター上に同じ名前のアダプターが複数存在しない場合は、インターフェースの説明から外れる可能性があります(または、PCI IDが適切に機能します)。その後、devconを使用します(無効|有効)。Devconは、デバイスマネージャー用のアドオンコンソールインターフェイスです。
これまでのところ、次の Python ソリューションを見つけました。
>>> import wmi; c=wmi.WMI()
>>> o=c.query("select * from Win32_NetworkAdapter where NetConnectionID='wifi'")[0]
>>> o.EnableDevice(1)
(-2147217407,)
これは、AFAIU で一般的な WMI エラー 0x80041001 に変換されます。権限である可能性があります。
インターネットでこの .VBS スクリプトを見つけました。この目的のために NETSH を動作させることができないマシンで実際に動作するという優れた利点があります。
Const ssfCONTROLS = 3
sConnectionName = "Local Area Connection"
sEnableVerb = "En&able"
sDisableVerb = "Disa&ble"
set shellApp = createobject("shell.application")
set oControlPanel = shellApp.Namespace(ssfCONTROLS)
set oNetConnections = nothing
for each folderitem in oControlPanel.items
if folderitem.name = "Network Connections" then
set oNetConnections = folderitem.getfolder: exit for
end if
next
if oNetConnections is nothing then
msgbox "Couldn't find 'Network Connections' folder"
wscript.quit
end if
set oLanConnection = nothing
for each folderitem in oNetConnections.items
if lcase(folderitem.name) = lcase(sConnectionName) then
set oLanConnection = folderitem: exit for
end if
next
if oLanConnection is nothing then
msgbox "Couldn't find '" & sConnectionName & "' item"
wscript.quit
end if
bEnabled = true
set oEnableVerb = nothing
set oDisableVerb = nothing
s = "Verbs: " & vbcrlf
for each verb in oLanConnection.verbs
s = s & vbcrlf & verb.name
if verb.name = sEnableVerb then
set oEnableVerb = verb
bEnabled = false
end if
if verb.name = sDisableVerb then
set oDisableVerb = verb
end if
next
'debugging displays left just in case...
'
'msgbox s ': wscript.quit
'msgbox "Enabled: " & bEnabled ': wscript.quit
'not sure why, but invokeverb always seemed to work
'for enable but not disable.
'
'saving a reference to the appropriate verb object
'and calling the DoIt method always seems to work.
'
if bEnabled then
' oLanConnection.invokeverb sDisableVerb
oDisableVerb.DoIt
else
' oLanConnection.invokeverb sEnableVerb
oEnableVerb.DoIt
end if
'adjust the sleep duration below as needed...
'
'if you let the oLanConnection go out of scope
'and be destroyed too soon, the action of the verb
'may not take...
'
wscript.sleep 1000
RAS API を除いて、MSDN のインターフェイスを制御するための基本的な API を見つけることができないようですが、非ダイヤルアップ接続には適用されないと思います。あなたが提案したように、netsh はオプションである可能性があります。おそらく、プログラム インターフェイスも備えています。
純粋な Python になりたい場合は、一連のパイプを開いて netsh プロセスと通信することができます。
WMI を使用する必要がある場合があります。これは良い出発点として役立つかもしれません: http://msdn.microsoft.com/en-us/library/aa394595.aspx
devconツールは NIC を制御できますが、インターフェイスを直接制御することはできません。これは、デバイス マネージャー アプレットのコマンド ライン バージョンです。
devcon disable (id or portion of name)
devcon enable (id or portion of name)
これはVB.Netです
Dim objectQuery As New ObjectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId IS NOT NULL")
Dim searcher As New ManagementObjectSearcher(scope, objectQuery)
Dim os As ManagementObject
Dim moColl As ManagementObjectCollection = searcher.Get()
Dim _list As String = ""
For Each os In moColl
Console.WriteLine(os("NetConnectionId"))
Next os
これにより、コンピューター上のすべてのインターフェイスが取得されます。次に、netsh を実行して無効にします。
netsh インターフェイスは、インターフェイスを無効に設定します