image od network

7 Useful Linux Scripts for Network Monitoring and Troubleshooting

Network monitoring and troubleshooting are critical tasks for network administrators. In this blog post, we’ll explore 7 useful Linux scripts that can help you monitor your network, diagnose issues, and maintain optimal performance. These scripts are designed to save you time and make network administration tasks more efficient.

1. Ping Multiple Hosts

Check the reachability of multiple hosts at once with this simple script:

#!/bin/bash
HOSTS="host1.com host2.com host3.com"
for HOST in ${HOSTS}; do
    ping -c 1 "${HOST}" >/dev/null 2>&1
    if [ $? -eq 0 ]; then
        echo "${HOST} is up"
    else
        echo "${HOST} is down"
    fi
done
    

2. Monitor Network Bandwidth

Monitor your network bandwidth usage in real-time with this script using the ifstat tool:

#!/bin/bash
INTERFACE="eth0"
DURATION="1"
ifstat -i "${INTERFACE}" -t "${DURATION}"
    

3. Check Open Ports

Identify open ports on your server using this script with the netstat command:

#!/bin/bash
netstat -tuln | grep "LISTEN"
    

4. Monitor Packet Loss

Monitor packet loss on your network using this script with the mtr command:

#!/bin/bash
HOST="your-target-host.com"
INTERVAL="1"
mtr -c "${INTERVAL}" -rw "${HOST}"
    

5. DNS Lookup

Perform DNS lookups for multiple domains simultaneously with this script:

#!/bin/bash
DOMAINS="domain1.com domain2.com domain3.com"
for DOMAIN in ${DOMAINS}; do
    IP=$(dig +short "${DOMAIN}")
    echo "${DOMAIN} resolves to ${IP}"
done
    

6. Scan Network for Devices

Scan your local network for connected devices using this script with the nmap command:

#!/bin/bash
NETWORK="192.168.1.0/24"
nmap -sn "${NETWORK}"
    

7. Monitor Network Latency

Monitor network latency to a specific host with this script using the ping command:

#!/bin/bash
HOST="your-target-host.com"
INTERVAL="1"
ping -i "${INTERVAL}" "${HOST}"

Conclusion

These 7 useful Linux scripts can help network administrators monitor and troubleshoot their networks more efficiently. By incorporating these scripts into your workflow, you’ll be better equipped to maintain optimal network performance and quickly diagnose any issues that arise.