كيفية تمكين/تعطيل واجهات الشبكة برمجياً؟(ويندوز إكس بي)
-
01-07-2019 - |
سؤال
أحتاج إلى تمكين/تعطيل واجهات الشبكة بالكامل من برنامج نصي في نظام التشغيل Windows XP.أنا أبحث عن حل بلغة python، ولكن أي طريقة عامة (مثل WMI، أو بعض سطر الأوامر على غرار netsh، أو بعض مكالمات Windows) مرحب بها وسيتم تعديلها.شكرًا.
المحلول
باستخدام واجهة تعيين استخدام واجهة Netsh [name =] ifname [[admin =] usabled | تعطل [connect =] connected | diswanded [newName =] newName
حاول تضمين كل شيء داخل الأقواس الخارجية:تعيين واجهة netsh اسم الواجهة = "thename" admin = اتصال معطل = اسم جديد غير متصل = "thename"
راجع أيضًا صفحة MS KB هذه: http://support.microsoft.com/kb/262265/يمكنك متابعة أي من اقتراحاتهم.لتعطيل المحول، ستحتاج إلى تحديد طريقة للإشارة إلى الجهاز.إذا لم يكن هناك محولات متعددة بنفس الاسم على الكمبيوتر، فمن المحتمل أن تخرج عن وصف الواجهة (أو يعمل معرف PCI بشكل جيد).بعد ذلك، استخدم devcon (تعطيل | تمكين).Devcon عبارة عن واجهة وحدة تحكم إضافية لإدارة الأجهزة.
نصائح أخرى
لقد وجدت حتى الآن حل بايثون التالي:
>>> 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
لا يمكنني العثور على أي واجهة برمجة تطبيقات أساسية للتحكم في الواجهات على MSDN، بصرف النظر عن واجهات برمجة تطبيقات RAS، لكنني لا أعتقد أنها تنطبق على الاتصالات غير الطلب الهاتفي.كما تقترح بنفسك، قد يكون netsh خيارًا، ومن المفترض أنه يحتوي أيضًا على واجهة برمجية: http://msdn.microsoft.com/en-us/library/ms708353(VS.85).aspx
إذا كنت تريد أن تكون لغة بايثون خالصة، فربما يمكنك فتح مجموعة من الأنابيب للتواصل مع عملية netsh.
هذا هو 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
ال devcon يمكن للأداة التحكم في بطاقة NIC، ولكن ليس في الواجهة مباشرةً.إنه إصدار سطر أوامر لبرنامج إدارة الأجهزة.
devcon disable (id or portion of name)
devcon enable (id or portion of name)
قد تحتاج إلى استخدام WMI.قد يكون هذا بمثابة نقطة انطلاق جيدة:http://msdn.microsoft.com/en-us/library/aa394595.aspx