Skip to content

Improve nethogs #9

Description

@pdolinic
#!/bin/bash

# Create or clear the new_ips_log.txt
> new_ips_log.txt

while true; do
    # Run nethogs in batch mode and capture the output
    output1=$(nethogs -t -c 2)
    
    # Wait for 10 seconds
    sleep 10
    
    # Capture the output again
    output2=$(nethogs -t -c 2)

    # Extract the IPs from both outputs
    ips1=$(echo "$output1" | grep -oP '(\d{1,3}\.){3}\d{1,3}')
    ips2=$(echo "$output2" | grep -oP '(\d{1,3}\.){3}\d{1,3}')

    # Check each IP from the current set of IPs
    while IFS= read -r ip; do
        # If the IP exists in the log, increment its counter
        if grep -q "^$ip " new_ips_log.txt; then
            awk -v ip="$ip" '$1 == ip {$2++}1' new_ips_log.txt > temp.txt && mv temp.txt new_ips_log.txt
        else
            # If it's a new IP (not in the previous set and not in the log), add it with a count of 1
            echo "$ip 1" >> new_ips_log.txt
        fi
    done <<< "$ips2"
done

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions