gpt4 book ai didi

windows - 通过cmd获取网速(Windows)

转载 作者:可可西里 更新时间:2023-11-01 10:08:38 27 4
gpt4 key购买 nike

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

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

最佳答案

您需要查询 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

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

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

cscript bandwidth.vbs

关于windows - 通过cmd获取网速(Windows),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7376778/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com