我如何获取实时值以在 BASH 中制作自己的脚本,特别如附件所示

发布于 2024-12-14 14:51:27 字数 2984 浏览 3 评论 0原文

我正在尝试在 BASH 中编写我自己的跟踪,如附件中所示。

在此处输入图像描述

特别是我需要跟踪接收和发送数据的网络历史记录。我在哪里可以获得这些网络发送/接收值,它是在文件中还是来自 Linux 中的某些命令?

使用 BASH 我试图实现类似如下的东西:

例如:

/* My 10 seconds timer */ 
t = new javax.swing.Timer(10000, new ActionListener() 
{
    public void actionPerformed(ActionEvent ae) 
    {
    }
}); 

跟进:(好网站:http://www.debianhelp.co.uk/networktools1.htm)

$ sudo apt-get install bwm-ng; 
yum -y install bwm; 

# Show me only plain mode 
$ bwm-ng -o plain

 bwm-ng v0.6 (delay 0.500s); press 'ctrl-c' to end this
 /proc/net/dev
 |         iface                    Rx                   Tx               Total
 ==============================================================================
              lo:       88803.53 KB/s        88803.53 KB/s        88803.53 KB/s
            eth0:           0.13 KB/s            0.13 KB/s            0.13 KB/s
 ------------------------------------------------------------------------------
           total:       88803.66 KB/s        88803.66 KB/s        88803.66 KB/s

# Show only the interface that i need to see
$ bwm-ng -o plain -I eth0

bwm-ng v0.6 (delay 0.500s); press 'ctrl-c' to end this
 /proc/net/dev
 |         iface                    Rx                   Tx               Total
 ==============================================================================
            eth0:           0.13 KB/s            0.13 KB/s            0.13 KB/s
 ------------------------------------------------------------------------------
           total:           0.13 KB/s            0.13 KB/s            0.13 KB/s

# Show only in MB format or KB format
# by skiping -d will default show as KB
$ bwm-ng -o plain -d

bwm-ng v0.6 (delay 0.500s); press 'ctrl-c' to end this
 /proc/net/dev
 /         iface                    Rx                   Tx               Total
 ==============================================================================
              lo:          85.79 MB/s           85.79 MB/s           85.79 MB/s
            eth0:         246.58  B/s          246.58  B/s          246.58  B/s
 ------------------------------------------------------------------------------
           total:          85.79 MB/s           85.79 MB/s           85.79 MB/s

 $ bwm-ng -o plain -N -d | grep total:
      total:           0.00  B/s            0.00  B/s            0.00  B/s
      total:           1.28 MB/s            1.28 MB/s            1.28 MB/s
      total:           1.19 MB/s            1.19 MB/s            1.19 MB/s
      total:           1.19 MB/s            1.19 MB/s            1.19 MB/s


 # another tool i used apt-get install vnstat
 # bwm-ng was doing wrong strange on other interfaces but this one 
 # now showing correct
 $ vnstat -u -i lo 
 $ vnstat -u -i eth0
 $ vnstat 
 $ iftop -i eth0

I am trying to write in BASH my own tracking such as shown in the attachment.

enter image description here

Specially i need to track the Network History Recieving and Sending data. Where i can get those Network send/receive values, is it in a file or is that comes out from some commands in Linux?

Using BASH i am trying to implement something similar as below:

ex:

/* My 10 seconds timer */ 
t = new javax.swing.Timer(10000, new ActionListener() 
{
    public void actionPerformed(ActionEvent ae) 
    {
    }
}); 

Follow up: (good site: http://www.debianhelp.co.uk/networktools1.htm)

$ sudo apt-get install bwm-ng; 
yum -y install bwm; 

# Show me only plain mode 
$ bwm-ng -o plain

 bwm-ng v0.6 (delay 0.500s); press 'ctrl-c' to end this
 /proc/net/dev
 |         iface                    Rx                   Tx               Total
 ==============================================================================
              lo:       88803.53 KB/s        88803.53 KB/s        88803.53 KB/s
            eth0:           0.13 KB/s            0.13 KB/s            0.13 KB/s
 ------------------------------------------------------------------------------
           total:       88803.66 KB/s        88803.66 KB/s        88803.66 KB/s

# Show only the interface that i need to see
$ bwm-ng -o plain -I eth0

bwm-ng v0.6 (delay 0.500s); press 'ctrl-c' to end this
 /proc/net/dev
 |         iface                    Rx                   Tx               Total
 ==============================================================================
            eth0:           0.13 KB/s            0.13 KB/s            0.13 KB/s
 ------------------------------------------------------------------------------
           total:           0.13 KB/s            0.13 KB/s            0.13 KB/s

# Show only in MB format or KB format
# by skiping -d will default show as KB
$ bwm-ng -o plain -d

bwm-ng v0.6 (delay 0.500s); press 'ctrl-c' to end this
 /proc/net/dev
 /         iface                    Rx                   Tx               Total
 ==============================================================================
              lo:          85.79 MB/s           85.79 MB/s           85.79 MB/s
            eth0:         246.58  B/s          246.58  B/s          246.58  B/s
 ------------------------------------------------------------------------------
           total:          85.79 MB/s           85.79 MB/s           85.79 MB/s

 $ bwm-ng -o plain -N -d | grep total:
      total:           0.00  B/s            0.00  B/s            0.00  B/s
      total:           1.28 MB/s            1.28 MB/s            1.28 MB/s
      total:           1.19 MB/s            1.19 MB/s            1.19 MB/s
      total:           1.19 MB/s            1.19 MB/s            1.19 MB/s


 # another tool i used apt-get install vnstat
 # bwm-ng was doing wrong strange on other interfaces but this one 
 # now showing correct
 $ vnstat -u -i lo 
 $ vnstat -u -i eth0
 $ vnstat 
 $ iftop -i eth0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过期情话 2024-12-21 14:51:27

编辑:简单的方法:您始终可以使用 mrtgcacti 而不是自己做。

否则...
每 2 秒获取字节输入和字节输出(按 ctrl+c 使其停止):

default_device=$(awk ' { if ($2 == '00000000') print $1 ; } ' < /proc/net/route)
while true
do
    bytesin=$(grep $default_device /proc/net/dev | cut -d':' -f2 | awk ' { print $1; } ')
    bytesout=$(grep $default_device /proc/net/dev | cut -d':' -f2 | awk ' { print $9; } ')
    echo bytesin=$bytesin bytesout=$bytesout
    sleep 2
done

您将得到如下输出:

bytesin=622734605 bytesout=1249429296
bytesin=622735091 bytesout=1249429620
bytesin=622735523 bytesout=1249430120
bytesin=622736268 bytesout=1249430481
bytesin=622736874 bytesout=1249430535

这些值是为安装默认路由的设备获取的。

EDIT: the easy way: you could always use mrtg or cacti instead of doing it yourself.

otherwise...
getting the bytes in and the bytes out, each 2 seconds (hit ctrl+c to make it stop):

default_device=$(awk ' { if ($2 == '00000000') print $1 ; } ' < /proc/net/route)
while true
do
    bytesin=$(grep $default_device /proc/net/dev | cut -d':' -f2 | awk ' { print $1; } ')
    bytesout=$(grep $default_device /proc/net/dev | cut -d':' -f2 | awk ' { print $9; } ')
    echo bytesin=$bytesin bytesout=$bytesout
    sleep 2
done

you'll get output like this:

bytesin=622734605 bytesout=1249429296
bytesin=622735091 bytesout=1249429620
bytesin=622735523 bytesout=1249430120
bytesin=622736268 bytesout=1249430481
bytesin=622736874 bytesout=1249430535

the values are obtained for the device where the default route is installed.

拥抱没勇气 2024-12-21 14:51:27

您可以编写自己的脚本来解析:

  • top -b -n1 CPU 输出
  • free 内存/交换
  • ifconfig eth0 网络# 将 eth0 替换为您的设备

Yon can write your own script to parse :

  • top -b -n1 output for the CPU
  • free for the memory/swap
  • ifconfig eth0 for the network # replace eth0 by your device
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文