Last active
May 22, 2024 18:22
-
-
Save AlexAtkinson/d74df51311c27fb787de0c78ca46aa76 to your computer and use it in GitHub Desktop.
Generate a simple TCP Connection Report
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# constat.sh | |
# ------------------------------------------------------------------------------ | |
# DESCRIPTION: | |
# Generate a TCP Connection Report | |
# | |
# USAGE: | |
# This script can be run directly, setup as a .bashrc function, or called remotely. | |
# Remote Use Example: (Tip: Get url by hitting the [RAW] button for this gist) | |
# source <(curl -s <url>) | |
# bash <(curl -s <url>) | |
# ------------------------------------------------------------------------------ | |
function constat() { | |
tmpfile='/tmp/connections.out'; | |
netstat -antu > $tmpfile; | |
printf "CONNECTION COUNTS (UDP,TCP):\n"; | |
printf -- "-----------------\n"; | |
for i in CLOSE_WAIT CLOSED ESTABLISHED FIN_WAIT_1 FIN_WAIT_2 LISTEN SYN_RECEIVED SYN_SEND TIME_WAIT UDP; | |
do | |
echo -e "${i}: $(grep -ci ${i} ${tmpfile})"; | |
done; | |
total_local_ports=$(( $(cat /proc/sys/net/ipv4/ip_local_port_range | awk '{print $2}') - $(cat /proc/sys/net/ipv4/ip_local_port_range | awk '{print $1}') )); | |
echo -e "\n\e[01;39mLOCAL PORT RANGE:\e[0m $(cat /proc/sys/net/ipv4/ip_local_port_range | tr '\t' '-')"; | |
echo -e "\e[01;39mTOTAL:\e[0m $total_local_ports"; | |
echo -e "\e[01;39mIN USE:\e[0m $(grep -c ^ ${tmpfile})"; | |
echo -e "\e[01;39mAVAILABLE:\e[0m $(( $total_local_ports - $(grep -c ^ ${tmpfile}) ))" | |
} | |
constat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Remote Use:
source <(curl -s https://gist.githubusercontent.com/AlexAtkinson/d74df51311c27fb787de0c78ca46aa76/raw/bd24b572dbdfe635b826b54433db4e56f8c8851a/constat.sh)