2011-09-11 25 views
2

我在16x2液晶显示屏上显示来自PC的几个信息。我使用一个小型的python程序来检索和解析数据(磁盘空间,温度等)并通过串行发送到LCD。通过cmd获取网速(Windows)

我想显示实际的下载/上传速度,但我找不到任何cmd脚本或类似的东西使用。有什么建议么?

回答

3

您需要查询WMI以获取此信息。从Python中做到这一点最简单的方法是简单地执行这个VB脚本(它的工作对我来说):改编自http://www.itworld.com/nlswindows070320?page=0,1

复制

Option Explicit 
On Error Resume Next 
dim strComputer 
dim wmiNS 
dim wmiQuery 
dim objWMIService 
Dim objLocator 
dim colItems 
dim objItem 
Dim strUsr, strPWD, strLocl, strAuth, iFLag 'connect server parameters 
Dim colNamedArguments 'WshNamed object 

subCheckCscript 'check to see if running in cscript 
    Set colNamedArguments = WScript.Arguments.Named 
    strComputer = colNamedArguments("c") 
subCheckArguments 

wmiNS = "\root\cimv2" 
wmiQuery = "Select BytesTotalPerSec from Win32_PerfFormattedData_Tcpip_NetworkInterface" 
strUsr = colNamedArguments("u") '""'Blank for current security. Domain\Username 
strPWD = colNamedArguments("p") '""'Blank for current security. 
strLocl = "" '"MS_409" 'US English. Can leave blank for current language 
strAuth = ""'if specify domain in strUsr this must be blank 
iFlag = "0" 'only two values allowed here: 0 (wait for connection) 128 (wait max two min) 

Set objLocator = CreateObject("WbemScripting.SWbemLocator") 
Set objWMIService = objLocator.ConnectServer(strComputer, _ 
    wmiNS, strUsr, strPWD, strLocl, strAuth, iFLag) 
Set colItems = objWMIService.ExecQuery(wmiQuery) 

For Each objItem in colItems 
    Wscript.Echo funLine("Name: " & objItem.name) 
    Wscript.Echo "CurrentBandwidth: " & funConvert("M",objItem.BytesTotalPerSec) 

Next 


' *** subs are below *** 
Sub subCheckCscript 
If UCase(Right(Wscript.FullName, 11)) = "WSCRIPT.EXE" Then 
    Wscript.Echo "This script must be run under CScript" 
    WScript.Quit 
End If 
end Sub 

Sub subCheckArguments 
If colNamedArguments.Count < 4 Then 
    If colNamedArguments.Exists("?") Then 
     WScript.Echo "Uses Win32_PerfFormattedData_Tcpip_NetworkInterface to determine bandwidth" _ 
    & VbCrLf & "This script can take arguments. It will analyze bandwidth of network adapter"_ 
    & VbCrLf & "This is useful when you want to see the actual bandwidth of a network adapter" _ 
    & VbCrLf & "Perhaps to troubleshoot cases when the adapter autodetects the wrong speed" _ 
    & VbCrLf & "Alternate credentials can ONLY be supplied for remote connections" _ 
    & VbCrLf & "Try this: cscript " & WScript.ScriptName & " [/c:yourcomputername] [/u:domainName\UserName] [/p:password]" _ 
    & VbCrLf & "Example: cscript " & WScript.ScriptName & " /c:london /u:nwtraders\londonAdmin /p:[email protected]" _ 
    & VbCrLf & vbTab & " Connects to a remote machine called london in the nwtraders domain with the londonAdmin user" _ 
    & VbCrLf & vbTab & " account and the password of [email protected] It returns the speed of each network adapter" _ 
    & VbCrLf & "Example: cscript " & WScript.ScriptName _ 
    & VbCrLf & vbTab & " Returns the speed of each network adapter on local machine" 
    WScript.Quit 
    End If 
WScript.Echo "checking arguments" 
    If Not colNamedArguments.Exists("c") Then 
     WScript.Echo "Executing on Local Machine only" 
     strComputer = "localHost" 
    End If 
    If Not colNamedArguments.Exists("u") Then 
     WScript.Echo "Executing using current user name" 
     strUsr = "" 
    End If 
    If Not colNamedArguments.Exists("p") Then 
     WScript.Echo "Executing using current user password" 
     strPWD = "" 
    End If 
End If 
If colNamedArguments.Count = 0 Then 
    Exit Sub 
End If 
End Sub 

Function funConvert(strC,intIN) 
Select Case strC 
Case "K" 
funConvert = formatNumber(intIN/1000) & " KiloBytes" 
Case "M" 
funConvert = formatNumber(intIN/1000000) & " MegaBytes" 
Case "G" 
funConvert = formatNumber(intIN/1000000000) & " GigaBytes" 
End Select 
End Function 

Function funLine(lineOfText) 
Dim numEQs, separator, i 
numEQs = Len(lineOfText) 
For i = 1 To numEQs 
    separator= separator & "=" 
Next 
FunLine = VbCrLf & lineOfText & vbcrlf & separator 
End Function 

并粘贴VB代码到一个名为bandwidth.vbs和像这样从cmd运行:

cscript bandwidth.vbs 
+0

谢谢!似乎脚本显示的接口带宽不是速度虽然(即在我的情况下,显示1000兆我的千兆以太网和54兆我的wlan G)我正在寻找类似的东西(我使用WMI来获得硬盘的温度和容量)显示实际的下载/上传速度(在我的情况下,无需打开PC屏幕即可监视山洪)。我发现这个程序http://www.floriangilles.com/software/netspeedmonitor/这是我所需要的,但没有命令接口... – Manuel

+0

看看Win32_NetworkAdapter.Speed而不是CurrentBandwidth ... http: //msdn.microsoft.com/en-us/library/aa394554(VS.85).aspx –

+0

MaxSpeed和Speed。第一个返回网络适配器的最大速度。第二个返回当前的带宽使用情况。两者都以每秒位数的形式返回值。 –