标题: [网络连接] 【求助】检测网速然后自动休眠的脚本 [打印本页]
作者: 435718680 时间: 2021-12-6 16:13 标题: 【求助】检测网速然后自动休眠的脚本
本帖最后由 435718680 于 2021-12-13 14:48 编辑
求写个 检测网速并自动系统休眠的脚本,有一个win10平板下载的dontsleep用不了,不能阻止平板休眠。
http://www.softwareok.com/?seite=Microsoft/DontSleep
所以只能手动切换休眠,网上找的命令行
永不休眠
powercfg /change standby-timeout-ac 0
powercfg /change standby-timeout-dc 0
3分钟休眠
powercfg /change standby-timeout-ac 3
powercfg /change standby-timeout-dc 3
求写个脚本检测电脑当前下载速度,当下载速度一直低于某值(20kb)一段时间,比如20分钟,切换为3分钟休眠。
如果脚本运行前能读取当前电脑休眠设置参数powercfg /change standby-timeout-ac 3更好,如启动脚本读取powercfg /change standby-timeout-ac 3则设置powercfg /change standby-timeout-ac 0以免下载时休眠导致中断下载。之后再判断 当下载速度一直低于某值(20kb)一段时间,比如20分钟,切换为3分钟休眠。
作者: 435718680 时间: 2021-12-9 19:53
自顶,求教各位大佬
作者: 435718680 时间: 2021-12-11 13:59
求教各位大佬
作者: flashercs 时间: 2021-12-12 12:56
可以用performance counter class监控下载速度.
作者: 435718680 时间: 2021-12-12 17:11
回复 4# flashercs
不懂代码呀,以前在百度搜到过vbs监控网卡速度的代码,复制了改一下就能用,现在搜不到了
作者: flashercs 时间: 2021-12-12 19:53
本帖最后由 flashercs 于 2021-12-13 16:34 编辑
回复 5# 435718680
vbs脚本- Option Explicit
- On Error Resume Next
- ' Const conBPSReceived = "BytesReceivedPersec"
- ' Const conBPSSent = "BytesSentPersec"
- ' Const conBPSTotal = "BytesTotalPersec"
- Const conInterval = 1200 ' 时间间隔(秒)
- Const conSpeedMax = 20000 ' 网速上限bps(bits/s)
- Const conFrequency = 5000 ' 监控网速间隔(毫秒)
- Const conShowWindow = True ' 是否显示窗口;显示=True, 隐藏=False
-
- Main
-
- Sub Main()
- On Error Resume Next
- RunasAdmin
- If conShowWindow Then
- RunasScriptHost "cscript.exe"
- Else
- RunasScriptHost "wscript.exe"
- End If
-
- Dim strComputer, objWMIService, objRefresher, objShell, dtm1, dtm2, speed, stack
- Dim objRefreshableItem1, objPerfNetworkInterface
- strComputer = "."
- Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
- Set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
- Set objShell = CreateObject("WScript.Shell")
- ' 先设置standby-timeout = 0
- objShell.Run "powercfg.exe /change standby-timeout-ac 0", 0, True
- objShell.Run "powercfg.exe /change standby-timeout-dc 0", 0, True
- ' addrefresh
- dtm1 = Now
- stack = 0
- Set objRefreshableItem1 = objRefresher.AddEnum(objWMIService, "Win32_PerfFormattedData_Tcpip_NetworkInterface")
-
- Do
- objRefresher.Refresh
- speed = 0
- If objRefreshableItem1.IsSet Then
- For Each objPerfNetworkInterface In objRefreshableItem1.ObjectSet
- speed = speed + objPerfNetworkInterface.BytesReceivedPersec
- Next
- Else
- speed = speed + objRefreshableItem1.Object.BytesReceivedPersec
- End If
- speed = speed * 8
- If conShowWindow Then
- WScript.Echo "speed = " & FormatBps(speed)
- End If
- If speed > conSpeedMax Then
- stack = 0
- Else
- If stack = 0 Then
- dtm1 = Now
- stack = 1
- End If
- dtm2 = Now
- ' WScript.Echo "min = " & DateDiff("s", dtm1, dtm2)
- If DateDiff("s", dtm1, dtm2) >= conInterval Then ' 超过指定时间,设置睡眠时间3
- objShell.Run "powercfg.exe /change standby-timeout-ac 3", 0, True
- objShell.Run "powercfg.exe /change standby-timeout-dc 3", 0, True
- Exit Do
- End If
- End If
- WScript.Sleep conFrequency
- Loop
- Set objShell = Nothing
- Set objRefresher = Nothing
- Set objWMIService = Nothing
- End Sub
- Function FormatBps(ByVal bps) ' As String
- On Error Resume Next
- If bps < 1000 Then
- FormatBps = FormatNumber(bps, 0) & "b/s"
- ElseIf bps < 1000000 Then
- FormatBps = FormatNumber(bps / 1000, 0) & "kb/s"
- ElseIf bps < 1000000000 Then
- FormatBps = FormatNumber(bps / 1000000, 0) & "mb/s"
- Else
- FormatBps = FormatNumber(bps / 1000000000, 0) & "gb/s"
- End If
- End Function
-
- Sub RunasScriptHost(strWSH)
- ' runas cscript.exe or wscript.exe
- On Error Resume Next
- If IsNull(strWSH) Or Not (StrComp(strWSH,"cscript.exe",vbTextCompare) = 0 Or StrComp(strWSH,"wscript.exe",vbTextCompare) = 0) Then
- Exit Sub
- End If
- Dim fso
- Set fso = CreateObject("Scripting.FileSystemObject")
- If StrComp(fso.GetFileName(WScript.FullName),strWSH,vbTextCompare) <> 0 Then
- Dim str,arg,shell
- Set shell = CreateObject("Shell.Application")
- str = ""
- For Each arg In WScript.Arguments
- str = str & " """ & arg & """"
- Next
- shell.ShellExecute strWSH,"//nologo """ & WScript.ScriptFullName & """ " & str, "", "open", 1
- Set shell = Nothing
- Set fso = Nothing
- WScript.Quit
- Else
- Set fso = Nothing
- End If
- End Sub
-
- Sub RunasAdmin()
- On Error Resume Next
- If Not IsVista(".") Then Exit Sub
- Dim wshell
- Set wshell = CreateObject("WScript.Shell")
- wshell.RegRead "HKEY_USERS\S-1-5-19\Environment\TEMP"
- If Err.Number <> 0 Then
- Dim str,arg,shell
- str = ""
- Set shell = CreateObject("Shell.Application")
- For Each arg In WScript.Arguments
- str = str & " """ & arg & """"
- Next
- shell.ShellExecute WScript.FullName,"//nologo """ & WScript.ScriptFullName & """ " & str, "", "runas", 1
- Set shell = Nothing
- Set wshell = Nothing
- WScript.Quit
- Else
- Set wshell = Nothing
- End If
- End Sub
-
- Function IsVista(strComputer)
- On Error Resume Next
- IsVista = False
- Dim objWMIService, colOperationSystems, objOperationSystem
- Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
- Set colOperationSystems = objWMIService.ExecQuery("Select Version from Win32_OperatingSystem")
- For Each objOperationSystem In colOperationSystems
- If CInt(Left(objOperationSystem.Version, InStr(1,objOperationSystem.Version,".") - 1)) > 5 Then
- IsVista = True
- Exit For
- End If
- Next
- Set colOperationSystems = Nothing
- Set objWMIService = Nothing
- End Function
复制代码
作者: 435718680 时间: 2021-12-13 15:07
本帖最后由 435718680 于 2021-12-13 15:09 编辑
回复 6# flashercs
你好,谢谢大神帮助,目前可以稳定使用,配合WScript.Sleep 60000 可以实现一分钟一次检测,20分钟休眠,也可以减少窗口打印信息过于密集的问题
但是,你能不能修改一下,那个打印网速自动识别为kb/s或者mb/s甚至gb/s而且带上单位不用kbps而用kb/s呢?这样显示窗口还可以兼做网速监测。而且我自己修改speed = speed * 8 为speed = speed / 1024 会导致小数点很长,非常难看,最好能去掉小数点后面的数据,方便观测。
还有就是能不能做个设置用来开启关闭网速窗口?有时候可以不用窗口也行,就在后台运行就可以了,我自己改不出来, 去掉 WScript.Echo "speed = " & speed就运行不成功了。以前找的那个就不用显示窗口可以运行。
如果都能实现那就完美了,再次感谢大神帮助
作者: flashercs 时间: 2021-12-13 16:35
回复 7# 435718680
源代码已修改.
作者: 435718680 时间: 2021-12-14 13:58
回复 8# flashercs
完美使用,谢谢
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |