netcup vps G11刷流限速

Views: 43

netcup新出的G11 G12对每日流量均有限制,任何时刻检测上24小时上+下行流量是否超过2T或者3T,超过则限速20m。对于刷流党来说等于“废了”。

经历一番折腾,记录下非常硬核且充满曲折的“排坑实录”,使用 qBittorrent 配合 Vertex 自动化刷流;大致意思是用监控脚本,流量快超标时自动停止,流量恢复后自动开启,并且要最大化上传,最小化下载。

一、安装vnstat用于监控读取vps出入总流量。

# 1. 更新源
sudo apt update
# 2. 安装 vnstat
sudo apt install vnstat -y
# 3. 启动服务
sudo systemctl start vnstat
sudo systemctl enable vnstat

 

二、安装python和qb依赖

sudo apt install python3 python3-pip -y
pip3 install qbittorrent-api

三、使用python脚本,我保存的位置是/opt/qb.py

import subprocess
import json
import os
import time
from datetime import datetime
from qbittorrentapi import Client
from qbittorrentapi.exceptions import APIConnectionError, LoginFailed
# ==============================================================================
# --- 用户配置 ---
# ==============================================================================
QB_HOST = '127.0.0.1' # 既然脚本在本地运行,直接填本地 IP 即可
QB_PORT = 8080 #qb端口号
QB_USER = 'admin'  #qb用户名
QB_PASS = 'admin' #qb密码
SERVICE_NAME = 'qbittorrent-nox' # 你的 qBittorrent 服务名称
# 流量限制参数 (2TB 限制)
TRAFFIC_THRESHOLD_GIB = 1950 # 停止阈值:1.95 TiB (到达即关QB)
TRAFFIC_RESUME_GIB = 200 # 启动阈值:200 GiB (只有流量归零/降低才开QB)
NETWORK_INTERFACE = 'eth0' # 网卡名称
# ==============================================================================
def get_24h_vnstat_traffic_gib(interface):
""" 获取过去 24 小时滚动流量 (GiB) """
try:
result = subprocess.run(['vnstat', '--json', 'h'], capture_output=True, text=True, check=True)
data = json.loads(result.stdout)
total_traffic_gib = 0
for iface_data in data.get('interfaces', []):
if iface_data.get('name') == interface:
traffic_blocks = iface_data.get('traffic', {}).get('hour', [])
recent_24h_blocks = traffic_blocks[-24:]
for block in recent_24h_blocks:
rx_bytes = block.get('rx', 0)
tx_bytes = block.get('tx', 0)
# 转换 Bytes -> GiB
rx_gib = rx_bytes / (1024 ** 3)
tx_gib = tx_bytes / (1024 ** 3)
total_traffic_gib += (rx_gib + tx_gib)
return total_traffic_gib
return -1
except Exception as e:
print(f"Error calculating traffic: {e}")
return 0
def is_service_active(service_name):
""" 检查服务是否正在运行 """
try:
result = subprocess.run(['systemctl', 'is-active', service_name], capture_output=True, text=True)
return result.stdout.strip() == 'active'
except Exception:
return False
def manage_resume_logic(now_str):
print(f"[{now_str}] Starting {SERVICE_NAME}...")
subprocess.run(['systemctl', 'start', SERVICE_NAME], check=True)
print(f"[{now_str}] Waiting 15s for qBittorrent to initialize...")
time.sleep(15)
try:
qb = Client(host=QB_HOST, port=QB_PORT, username=QB_USER, password=QB_PASS)
qb.auth_log_in()
all_torrents = qb.torrents.info()
hashes_to_delete = []
for t in all_torrents:
if t.progress < 1:
hashes_to_delete.append(t.hash)
if hashes_to_delete:
print(f"[{now_str}] *** KILL MODE: Deleting {len(hashes_to_delete)} incomplete torrents (with data) to prevent download. ***")
qb.torrents.delete(torrent_hashes=hashes_to_delete, delete_files=True)
else:
print(f"[{now_str}] Smart Resume: No incomplete downloads found. All torrents are seeding. Safe to run.")
except Exception as e:
print(f"[{now_str}] Smart Resume Warning: Could not connect to API to clean torrents ({e}). But service is running.")
def main():
now_str = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
traffic_gib = get_24h_vnstat_traffic_gib(NETWORK_INTERFACE)
if traffic_gib < 0:
return
service_active = is_service_active(SERVICE_NAME)
print(f"[{now_str}] Traffic: {traffic_gib:.2f} GiB. Status: {'RUNNING' if service_active else 'STOPPED'}.")
if traffic_gib >= TRAFFIC_THRESHOLD_GIB:
if service_active:
print(f"[{now_str}] *** DANGER: Traffic limit reached! STOPPING qBittorrent Service immediately. ***")
subprocess.run(['systemctl', 'stop', SERVICE_NAME], check=True)
else:
pass
elif traffic_gib <= TRAFFIC_RESUME_GIB:
if not service_active:
print(f"[{now_str}] *** SAFE: Traffic is low. Performing SMART RESUME. ***")
manage_resume_logic(now_str)
else:
pass
else:
pass
if __name__ == "__main__":
main()

 

四、执行及定时任务

保存上述脚本执行
/usr/bin/python3 /opt/qb.py
由于流量是实时变动的,加入定时任务每5分钟执行监控1次
crontab -e
*/5 * * * * /usr/bin/python3 /opt/qb_traffic_monitor.py >> /var/log/qb_monitor.log 2>&1

Netcup 的限制不是每天凌晨清零,而是一条传送带。脚本的作用就是确保传送带上最近 24 小时内的货物总重量不超过 2 吨。一旦超重,机器停转;等到前面的货物掉下传送带(过期),腾出了空间,机器自动重启继续工作。

 

 

版权声明:本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。

转载请注明来自 网上另居

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注