利用watch命令和byzanz工具制作Gif图

Linux下都有一个名为watch的命令,这个命令可以帮我们监测另外一个命令的运行结果,这在我们需要一直查看某个命令的输出结果时非常有用。比如,我们知道查看网口eth0上的接收数据包流量的命令如下:
[root@localhost ~]# ethtool -S eth0 | grep rx_packets
rx_packets: 595
[root@localhost ~]#

如果要一直查看网口eth0上的接收数据包流量,那么我们也许会这样做:
[root@localhost ~]# while :; do ethtool -S eth0 | grep rx_packets; sleep 1; done
rx_packets: 1945
rx_packets: 1946
rx_packets: 1947
rx_packets: 1948
rx_packets: 1949
rx_packets: 1950
rx_packets: 1951
rx_packets: 1952
rx_packets: 1953
^C
[root@localhost ~]#

但更方便的方法也许是如下这样,“-n 1”指定每隔1s刷新一次,而“-d”则高亮显示变化的区域,这样能够一目了然:
[root@localhost ~]# watch -n 1 -d “ethtool -S eth0 | grep rx_packets;”

然后,你将获得这样一个动态的字符界面:
Every 1.0s: ethtool -S eth0 | grep rx_packets; Tue Nov 29 18:57:54 2011

rx_packets: 2379

关于watch命令的更多内容请直接查看man手册,下面介绍byzanz:
byzanz在ubuntu下很好安装,直接如下即可:
sudo apt-get install byzanz

我这里从源码开始安装,源码可从官网:http://blogs.gnome.org/otte/2009/08/30/byzanz-0-2-0/下载到。byzanz的手动编译有点麻烦,它需要的cairo版本有点高,我的CentOS-6.0的系统默认的cairo 1.8.8也不够,所以configure配置时报错如下:
[root@localhost byzanz-0.2.0]# ./configure

checking for GTK… configure: error: Package requirements (cairo >= 1.9.3 gtk±2.0 >= 2.17.10 x11 gio-2.0) were not met:

Requested ‘cairo >= 1.9.3’ but version of cairo is 1.8.8

下载高版本的cairo:http://cairographics.org/releases/cairo-1.10.0.tar.gz以及pixman:http://cairographics.org/releases/pixman-0.24.0.tar.gz并进行编译安装:
[root@localhost lenky]# tar xzf pixman-0.24.0.tar.gz
[root@localhost pixman-0.24.0]# cd pixman-0.24.0
[root@localhost pixman-0.24.0]# ./configure
[root@localhost pixman-0.24.0]# make
[root@localhost pixman-0.24.0]# make install
[root@localhost pixman-0.24.0]# cd …
[root@localhost lenky]# tar xzf xzf cairo-1.10.0.tar.gz
[root@localhost lenky]# cd cairo-1.10.0
[root@localhost cairo-1.10.0]# ./configure
[root@localhost cairo-1.10.0]# make -j2
[root@localhost cairo-1.10.0]# make install

接着编译byzanz:
[root@localhost cairo-1.10.0]# cd
[root@localhost ~]# cd lenky/
[root@localhost lenky]# cd byzanz-0.2.0
[root@localhost byzanz-0.2.0]# ./configure

提示出错差某某库,也直接给它装上:
[root@localhost byzanz-0.2.0]# yum install libpanelappletmm*
[root@localhost byzanz-0.2.0]# yum install gstreamer*

再来,好,这下一切ok,byzanz终于被我安装上了:
[root@localhost byzanz-0.2.0]# ./configure
[root@localhost byzanz-0.2.0]# make
[root@localhost byzanz-0.2.0]# make install

试试效果:
[root@localhost ~]# cd Desktop/
[root@localhost Desktop]# cd
[root@localhost ~]# byzanz-record first.gif
[root@localhost ~]#

初步感觉不错,byzanz提供的主要参数如下:
-d, --duration=SECS:录像持续时间,默认是10秒
–delay=SECS:设置几秒种后开始录像,默认是1秒
-l, --loop:设置录像循环播放
-c, --cursor:记录鼠标指针
-x, --x=PIXEL:录像区域的起始横坐标,默认是0(左上角)
-y, --y=PIXEL:录像区域的起始纵坐标,默认是0(左上角)
-w, --width=PIXEL:录像区域宽度
-h, --height=PIXEL:录像区域高度
–display=DISPLAY:指定显示设备

那么结合watch和byzanz,我们可以生成这样一张网口收包流量图:
rx_packets.gif

转自:http://lenky.info/?p=429

“while :; do ethtool -S eth0 | grep rx_packets; sleep 1; done” 终端还可以用循环哪,第一次见:5_116:
byzanz不知道可不可以作为屏幕录像软件使用。