没有网络流量时关闭系统的工具

cir*_*tor 14 shutdown network-monitoring

我正在寻找一个脚本或工具,我可以在需要时打开它,如果没有网络流量,例如 10 分钟或低于 100kb,它会关闭我的计算机。

对于自动下载来说真的很方便。我知道这样做有缺点,互联网连接挂起,下载程序挂起,所以如果你有更好的主意,请告诉我。

提前致谢。

kin*_*ilo 15

有几种方法可以解决这个问题,我编写了一个非常简单的 bash 脚本,当下载速度低于最小值(您可以设置)时,您可以使用它来监控所需接口的速度(以KB p/s为单位) ,然后您的计算机将被关闭。

这里要记住的一些事情是:

  • 这是我快速组合在一起的 bash 脚本,有许多不同的技术可以实现相同的结果,但是这是一个易于理解和实现的方法。

  • 您需要以root 身份cron执行 bash 脚本,这意味着您需要以 root 用户身份打开 cron 并根据需要添加一个 cronjob。它需要在 root 的 cron 中的原因是你将无法在没有 root 的情况下从命令行关闭你的计算机,并且当你离开键盘时你不能使用 sudo。有很多方法可以解决它,但我试图让它尽可能简单。

  • 我使用了一个名为ifstat的 linux 工具,因此您需要安装它,否则脚本将无法工作:

    sudo apt-get install ifstat
    
    Run Code Online (Sandbox Code Playgroud)
  • 您可以在下面的脚本中修改 2 个选项,即INTERFACEMIN_SPEED。INTERFACE 需要设置为您用于下载的接口,有线设备为 eth0 或无线设备为 wlan0,您可以在命令行中使用ifconfig命令来查看您有哪些接口可用。MIN_SPEED 根据需要设置,在我的示例中,我将其设置为数字5,这意味着如果我的下载速度低于每秒 5 KB,那么我的计算机将关闭。

  • 最后,为了改进脚本,我们可以使用while 循环并检查指定时间段内的下载速度,如果平均值小于我们将关闭的最小值,并将脚本作为服务运行,这是一个解决问题的更准确方法,如果这是您想要遵循的路线,我将很乐意为您提供帮助。

将以下代码复制并粘贴到计算机上您选择的目录中的文件中,例如i_speed.sh,然后非常重要的是,使文件可执行,如果您的文件名为 i_speed.sh,则可以从命令行执行此操作如下:

    chmod +x i_speed.sh 
Run Code Online (Sandbox Code Playgroud)

现在您可以 sudo -i 到 root 并设置您的 cronjob 以在您想要的时间间隔调用脚本。

将代码复制并粘贴到名为 i_speed.sh 的文件中:

#!/bin/bash

# Bash script to determine a network interfaces current transfer speed and 
  shutdown the computer if the current transfer speed is less than MIN_SPEED

# Set INTERFACE to the network interface you would like to monitor
INTERFACE='wlan0'

# Set MIN_SPEED in KB per second that network interface (INTERFACE) speed 
  must be larger than, if speed falls below this number then computer will shutdown.
MIN_SPEED=5


# This is where the work get's done:
CURRENT_SPEED=`ifstat -i $INTERFACE 1 1 | awk '{print $1}' | sed -n '3p'`
INT=${CURRENT_SPEED/\.*}

if [ $INT -lt $MIN_SPEED ]; then
    shutdown -h now
else
    exit
fi
Run Code Online (Sandbox Code Playgroud)

更新

我写了一个小的 python 程序作为上面 bash 脚本的更新,它允许你设置额外的变量,比如重试和间隔,以获得指定时间段内的平均最小速度。进一步的更新将包括此程序的 GUI。只需将下面的代码复制并粘贴到文件中,download_monitor.py然后按如下方式运行它sudo python download_monitor.py

## Download Monitor v0.1 - March 2012

# Set the interface you wish to monitor, eg: eth0, wlan0, usb0
INTERFACE = "eth0"

# Set the minimum download speed in KB/s that must be achieved.
MINIMUM_SPEED = 15

# Set the number of retries to test for the average minimum speed. If the average speed is less
# than the minimum speed for x number of retries, then shutdown.
RETRIES = 5

# Set the interval (in seconds), between retries to test for the minimum speed.
INTERVAL = 10


import os, time
from commands import getoutput

def worker ():
    RETRIES_COUNT = RETRIES
    while True:
        SPEED = int(float(getoutput("ifstat -i %s 1 1 | awk '{print $1}' | sed -n '3p'" % INTERFACE)))
        if (SPEED < MINIMUM_SPEED and RETRIES_COUNT <= 0):
            os.system("shutdown -h now")
        elif SPEED < MINIMUM_SPEED:
            RETRIES_COUNT -= 1
            time.sleep(INTERVAL)
        else:
            RETRIES_COUNT = RETRIES
            time.sleep(INTERVAL)

worker()
Run Code Online (Sandbox Code Playgroud)