# Network Interface Statistics Script

**URL:** <https://community.netbeez.net/t/network-interface-statistics-script/297>\
**Category:** Custom Commands\
**Created:** [August 13, 2025, 8:44am UTC](https://community.netbeez.net/t/network-interface-statistics-script/297 "2025-08-13T08:44:24Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![stefano](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@stefano](https://community.netbeez.net/u/stefano)\
**Post date:** [August 13, 2025, 8:44am UTC](https://community.netbeez.net/t/network-interface-statistics-script/297/1 "2025-08-13T08:44:24Z")

</div>

## 1. Introduction

Monitoring network interface statistics is a quick way to spot potential issues like packet drops, high error counts, or abnormal throughput. This script lets you check key counters — packets, bytes, and errors — for any interface directly from a NetBeez agent.

**Example Use Cases:**

- Quickly verify if an interface is transmitting/receiving data
- Detect unusual traffic spikes
- Identify packet error trends during troubleshooting

* * *

## 2. Overview of the Script

This Bash script:

1. Accepts a network interface name as an argument (defaults to `eth0` if none is provided)
2. Validates that the interface exists
3. Reads counters from `/sys/class/net/<interface>/statistics/`
4. Outputs RX/TX packet counts, byte counts, and error counts in a clean format

**Prerequisites:**

- Linux-based NetBeez network agent
- Read access to `/sys/class/net/`
- Bash shell

**Expected Output:**

- Key metrics like packets sent/received, bytes sent/received, and error counts

* * *

## 3. Script Code

```bash
#!/usr/bin/env bash

# Default interface
INTERFACE="eth0" # Use eth0 or replace with desired interface name

# Check if interface exists
if [! -d "/sys/class/net/$INTERFACE"]; then
    echo "Interface $INTERFACE does not exist."
    exit 1
fi

# Read statistics
RX_BYTES=$(cat /sys/class/net/$INTERFACE/statistics/rx_bytes)
TX_BYTES=$(cat /sys/class/net/$INTERFACE/statistics/tx_bytes)
RX_PACKETS=$(cat /sys/class/net/$INTERFACE/statistics/rx_packets)
TX_PACKETS=$(cat /sys/class/net/$INTERFACE/statistics/tx_packets)
RX_ERRORS=$(cat /sys/class/net/$INTERFACE/statistics/rx_errors)
TX_ERRORS=$(cat /sys/class/net/$INTERFACE/statistics/tx_errors)

# Print results
echo "rx_pkts=$RX_PACKETS"
echo "tx_pkts=$TX_PACKETS"
echo "rx_bytes=$RX_BYTES"
echo "tx_bytes=$TX_BYTES"
echo "rx_err=$RX_ERRORS"
echo "tx_err=$TX_ERRORS"

```

## 4. Sample Output

```auto
rx_pkts=104523
tx_pkts=98762
rx_bytes=12876432
tx_bytes=11324567
rx_err=0tx_err=0 

```

## 5. Closing Remarks

This script provides a quick, no-frills way to inspect interface-level traffic counters from a NetBeez agent. It’s especially handy during incident response or when validating changes in real-time.

Have ideas to improve it? Share your modifications in the comments so others can benefit.

---

_[View the full topic](https://community.netbeez.net/t/network-interface-statistics-script/297)._
