Playbooks

Python Script to Monitor Network Traffic

Here is a simple Python script to monitor network traffic.

import psutil

def monitor_network():
    # Get the network I/O statistics for all interfaces
    net_io_counters = psutil.net_io_counters(pernic=True)

    # Loop through each interface
    for interface, info in net_io_counters.items():
        print(f"Interface: {interface}")
        print(f"Bytes sent: {info.bytes_sent}")
        print(f"Bytes received: {info.bytes_recv}")
        print("\n")

# Continuously monitor network traffic every second
while True:
    monitor_network()
    time.sleep(1)

This script uses the psutil library to get the network I/O statistics for all network interfaces. It then loops through each interface and prints the number of bytes sent and received. The script continuously monitors the network traffic every second by using an infinite loop and the time.sleep function.

PyJoy

Recent Posts

Python Script for Web Scraping

Here's a basic example of web scraping in Python using the requests and BeautifulSoup libraries:…

2 years ago

Python Script to SSH and Run Commands

Here's an example of how you can use Python to SSH into a remote server…

2 years ago

Python Code Example with Classes

Here's an example of a Python class that represents a rectangle: class Rectangle: def __init__(self,…

2 years ago

Python Script Example with Main Method in Active Directory

Here is a simple Python script that interacts with Active Directory (AD) using the pyad…

2 years ago

Python Script Example for System Administrators

Here's a Python script to monitor disk usage on a system and send an email…

2 years ago

How to Run a Python Script on Mac

Running a Python script on a Mac is a relatively straightforward process that can be…

2 years ago