2010年01月23日 linux小贴士*3

1. 监控本机网络流量的shell脚本

#!/bin/bash

if [ -n "$1" ]; then
eth_name=$1
else
eth_name="eth0"
fi

i=0

send_o=`ifconfig $eth_name | grep bytes | awk '{print $6}' | awk -F : '{print $2}'`
recv_o=`ifconfig $eth_name | grep bytes | awk '{print $2}' | awk -F : '{print $2}'`
send_n=$send_o
recv_n=$recv_o

while [ $i -le 100000 ]; do
send_l=$send_n
recv_l=$recv_n
sleep 1
send_n=`ifconfig $eth_name | grep bytes | awk '{print $6}' | awk -F : '{print $2}'`
recv_n=`ifconfig $eth_name | grep bytes | awk '{print $2}' | awk -F : '{print $2}'`
i=`expr $i + 1`
send_r=`expr $send_n - $send_l`
recv_r=`expr $recv_n - $recv_l`
total_r=`expr $send_r + $recv_r`
send_ra=`expr \( $send_n - $send_o \) / $i`
recv_ra=`expr \( $recv_n - $recv_o \) / $i`
total_ra=`expr $send_ra + $recv_ra`
sendn=`ifconfig $eth_name | grep bytes | awk -F \( '{print $3}' | awk -F \) '{print $1}'`
recvn=`ifconfig $eth_name | grep bytes | awk -F \( '{print $2}' | awk -F \) '{print $1}'`
clear
echo "Last second : Send rate: $send_r Bytes/sec Recv rate: $recv_r Bytes/sec Total rate: $total_r Bytes/sec"
echo "Average value: Send rate: $send_ra Bytes/sec Recv rate: $recv_ra Bytes/sec Total rate: $total_ra Bytes/sec"
echo "Total traffic after startup: Send traffic: $sendn Recv traffic: $recvn"
done

此脚本可实时监测网络流量信息并动态刷新(需具备ifconfig的执行权限),输出范例如下

Last second : Send rate: 17270 Bytes/sec Recv rate: 3175 Bytes/sec Total rate: 20445 Bytes/sec
Average value: Send rate: 91654 Bytes/sec Recv rate: 12443 Bytes/sec Total rate: 104097 Bytes/sec
Total traffic after startup: Send traffic: 3.9 GiB Recv traffic: 420.2 MiB

2. 更改linux主机名

登陆到root用户,执行下列命令更改当前系统的主机名

[root@old ~]$ hostname new.server

修改/etc/sysconfig/network中的条目,使其重启后能继续生效

[root@old ~]$ vi /etc/sysconfig/network
hostname=new.server

修改/etc/hosts中的条目并重启

[root@old ~]$ vi /etc/hosts
127.0.0.1 new.server new localhost.localdomain localhost

3. 在top中查看多核cpu的利用率信息(有灌水嫌疑,勿拍砖)

执行top命令后按数字键1。。。- -||

Rand Posts:



我们在天上的父
愿人都尊你的名为圣
愿你的国降临
愿你的旨意行在地上如同行在天上
我们将顺着你的指引前行
直至重归你的梯下


评论

0条评论

发表评论

文明上网,共建和谐社会。