通过cmd获取网速(Windows)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7376778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:03:00  来源:igfitidea点击:

Obtaining net speed through cmd (Windows)

windowsperformancenetworkingcmd

提问by Manuel

I'm displaying several info from a PC in a 16x2 LCD Screen. I use a tiny python program that retrieves and parse data (disk space, temperatures, etc) and send to LCD by serial.

我在 16x2 LCD 屏幕上显示来自 PC 的几个信息。我使用一个微型 python 程序来检索和解析数据(磁盘空间、温度等)并通过串行发送到 LCD。

I want to show the actual download/upload speed, but I can't find any cmd script or something like that to use. Any suggestions?

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

回答by Ben H

You need to query WMI to get this information. The easiest way to do it from Python is to simply execute this VB Script (it worked for me):

您需要查询 WMI 以获取此信息。从 Python 执行此操作的最简单方法是简单地执行此 VB 脚本(它对我有用):

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:P@ssw0rd" _
    & VbCrLf & vbTab & " Connects to a remote machine called london in the nwtraders domain with the londonAdmin user" _
    & VbCrLf & vbTab & " account and the password of P@ssw0rd. 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

adapted from http://www.itworld.com/nlswindows070320?page=0,1

改编自http://www.itworld.com/nlswindows070320?page=0,1

Copy and paste the VB code into a file called bandwidth.vbs and run like this from cmd:

将 VB 代码复制并粘贴到一个名为bandwidth.vbs 的文件中,然后从 cmd 像这样运行:

cscript bandwidth.vbs