shell脚本网卡流量
时间: 2023-11-17 10:03:42 浏览: 56
以下是一个获取网卡流量的Shell脚本例子:
```bash
#!/bin/bash
eth_in_old=$(ifconfig eth0|grep RX bytes|sed 's/RX bytes://'|awk '{print $1}')
eth_out_old=$(ifconfig eth0|grep RX bytes|sed 's/.*TX bytes://'|awk '{print $1}')
sleep 1
eth_in_new=$(ifconfig eth0|grep RX bytes|sed 's/RX bytes://'|awk '{print $1}')
eth_out_new=$(ifconfig eth0|grep RX bytes|sed 's/.*TX bytes://'|awk '{print $1}')
eth_in=$((($eth_in_new-$eth_in_old)/1024))
eth_out=$((($eth_out_new-$eth_out_old)/1024))
echo "Input traffic is: $eth_in KB/s"
echo "Output traffic is: $eth_out KB/s"
```
该脚本通过获取网卡的输入和输出流量,计算出1秒钟内的流量,并输出结果。
相关问题
shell脚本监控线路流量
您好!为了监控线路流量,您可以编写一个Shell脚本来完成。以下是一个简单的示例:
```shell
#!/bin/bash
# 定义要监控的线路接口
INTERFACE="eth0"
# 定义监控间隔时间(秒)
INTERVAL=5
while true; do
# 获取当前时间
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
# 获取接口的流入流量和流出流量
RX=$(cat /sys/class/net/$INTERFACE/statistics/rx_bytes)
TX=$(cat /sys/class/net/$INTERFACE/statistics/tx_bytes)
# 将字节转换为可读格式
RX=$(numfmt --to=iec-i --suffix=B --padding=7 $RX)
TX=$(numfmt --to=iec-i --suffix=B --padding=7 $TX)
# 输出监控结果
echo "${TIMESTAMP} | RX: ${RX} | TX: ${TX}"
# 等待指定的时间间隔
sleep $INTERVAL
done
```
在脚本中,您需要根据您要监控的线路接口进行相应的配置,将`INTERFACE`变量设置为对应的接口名称。然后,使用`cat`命令读取该接口的流入(rx_bytes)和流出(tx_bytes)字节数。通过`numfmt`命令将字节转换为可读格式,并使用`echo`命令输出监控结果。最后,使用`sleep`命令等待指定的时间间隔。
您可以将上述脚本保存为一个文件(例如`monitor.sh`),然后在Shell中运行该脚本(例如`./monitor.sh`)来监控指定线路的流量。
写配置网卡shell脚本
以下是一个配置网卡的shell脚本,你可以根据自己的需求进行修改:
```shell
#!/bin/bash
# 获取网卡名称
echo "Please enter the name of the network card:"
read NIC
# 获取IP地址
echo "Please enter the IP address:"
read IPADDR
# 获取子网掩码
echo "Please enter the subnet mask:"
read NETMASK
# 获取网关
echo "Please enter the gateway:"
read GATEWAY
# 获取DNS
echo "Please enter the DNS server:"
read DNS
# 配置网卡
cat > /etc/sysconfig/network-scripts/ifcfg-$NIC << EOF
DEVICE=$NIC
BOOTPROTO=static
ONBOOT=yes
IPADDR=$IPADDR
NETMASK=$NETMASK
GATEWAY=$GATEWAY
DNS1=$DNS
EOF
# 重启网络服务
systemctl restart network
```