speedometer指令 - Linux

Jake avatar
By Jake
at 2018-04-12T13:08

Table of Contents

※ 引述《smile2359630 (。。。)》之銘言:
: 小弟剛步入linux系統沒多久而已,目前遇到一個問題
: 我想在macbook上使用speedometer這個網路監測軟體,想說都是以unix下去發展出來的OS,
: 執行上照理說可行的,但目前遇到下面問題
: speedometer -rx eth0 時顯示以下
: Traceback (most recent call last):
: File "/usr/local/bin/speedometer", line 1093, in <module>
: console()
: File "/usr/local/bin/speedometer", line 797, in console
: wait_all(cols)
: File "/usr/local/bin/speedometer", line 1088, in wait_all
: tap.wait_creation()
: File "/usr/local/bin/speedometer", line 856, in wait_creation
: if self.feed() is None:
: File "/usr/local/bin/speedometer", line 610, in networkfn
: f = open('/proc/net/dev')
: IOError: [Errno 2] No such file or directory: '/proc/net/dev'

這個程式寫法是抓 linux 特有的 /proc/net/dev 分析內容,所以 linux 適合使用,
mac 沒該檔案所以運作失敗。

要改寫支援 mac 也很簡單,剛看一下大約這樣子

/usr/local/bin/speedometer.py 檔案打開,幾個地方要改:

1. 找一下一開始 import 部分語法

#!/usr/local/opt/python@2/bin/python2.7

# speedometer.py
# Copyright (C) 2001-2011 Ian Ward
#
# This module is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This module is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.

__version__ = "2.8"

import time
import sys
import os
import string
import math
import re

在 import re 之後,多加一行:

import psutil

2. 下面這段要改:

def network_feed(device,rxtx):
"""network_feed(device,rxtx) -> function that returns given device stream speed
rxtx is "RX" or "TX"
"""
assert rxtx in ["RX","TX"]
r = re.compile(r"^\s*" + re.escape(device) + r":(.*)$", re.MULTILINE)

def networkfn(devre=r,rxtx=rxtx):
f = open('/proc/net/dev')
dev_lines = f.read()
f.close()
match = devre.search(dev_lines)
if not match:
return None

parts = match.group(1).split()
if rxtx == 'RX':
return long(parts[0])
else:
return long(parts[8])

return networkfn

改成:

def network_feed(device,rxtx):
"""network_feed(device,rxtx) -> function that returns given device stream speed
rxtx is "RX" or "TX"
"""
assert rxtx in ["RX","TX"]
r = re.compile(r"^\s*" + re.escape(device) + r":(.*)$", re.MULTILINE)

def networkfn(devre=r,rxtx=rxtx):

if rxtx == 'RX':
return psutil.net_io_counters(pernic=True)[device].bytes_recv
else:
return psutil.net_io_counters(pernic=True)[device].bytes_sent

return networkfn


主要是 networkfn() 裡面改改就好。

最後系統用 pip install psutil 安裝上該 python module,剛剛跑一下可以運作。


--
Tags: Linux

All Comments

Olga avatar
By Olga
at 2018-04-15T17:23
真的太感謝您了!!!
Barb Cronin avatar
By Barb Cronin
at 2018-04-19T05:04
James avatar
By James
at 2018-04-22T14:09
比較意外的是改很快一直沒有人提交新的更新給作者蠻可
惜的,後續也來提交看看不過可能得順便處理py3的問題
Blanche avatar
By Blanche
at 2018-04-26T23:07
fork 版本可以請測試用看看,py3 也可以支援了:
Quintina avatar
By Quintina
at 2018-04-29T05:13
https://github.com/kenduest/speedometer

speedometer指令

Oscar avatar
By Oscar
at 2018-04-12T09:46
小弟剛步入linux系統沒多久而已,目前遇到一個問題 我想在macbook上使用speedometer這個網路監測軟體,想說都是以unix下去發展出來的OS, 執行上照理說可行的,但目前遇到下面問題 speedometer -rx eth0 時顯示以下 Traceback (most recent ca ...

Ubuntu 16.04 自動登出

Christine avatar
By Christine
at 2018-04-12T09:07
各位好,小弟最近使用Linux系統做些小實驗。 不過昨天ssh近自己ubuntu玩著玩著就發現成功登入後大概過不到幾秒就自動斷線。 原先以為是ssh設定問題,想說今天直接到Host來查看問題。 結果發現現在登入後完全不能下commad就會自動被登出。 不論是GUI介面還是tty都一樣有這問題,導致完全 ...

kali live on surface pro 失敗

Elvira avatar
By Elvira
at 2018-04-11T10:00
我使用 kali 2018.1 版本 再用rufus 做成支援 UEFI 的開機隨身碟 使用PC 測試是正常沒有問題的。pc 也是用UEFI 開機的。 但在surface pro 上面有出現grub 的選單, 但選了live 選項後就沒有任何動靜了。 surface 上已關掉security ,並且US ...

PCIe 重新啟動

Barb Cronin avatar
By Barb Cronin
at 2018-04-10T15:03
環境:ubuntu 14.04 小弟剛入門 Linux 環境 因工作需求,須插拔PCle的nvme ssd裝置,來重新辨識裝置 但無法像sata 可以直接熱拔,無須關電重開機,達到重新識別裝置。 目前pcie都是重開機後重新認裝置 想請問 有軟體方式 重新啟動pcie裝置,而不用關電重開機?? ※ ...

Linux Bonding免費課程

Tristan Cohan avatar
By Tristan Cohan
at 2018-04-09T19:14
請問有人知道Linux Bonding嗎? 剛剛逛到一個FB貼文,有免費課,感覺可以聽看看 2018年4月10日 星期二 20:00~21:00 『Linux Bonding的秘密』 https://goo.gl/ifFrQP 出處: https://m.facebook.com/story.ph ...