Skip to content

Instantly share code, notes, and snippets.

@AlexAtkinson
Last active May 22, 2024 18:22
Show Gist options
  • Save AlexAtkinson/d74df51311c27fb787de0c78ca46aa76 to your computer and use it in GitHub Desktop.
Save AlexAtkinson/d74df51311c27fb787de0c78ca46aa76 to your computer and use it in GitHub Desktop.
Generate a simple TCP Connection Report
#!/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
@AlexAtkinson
Copy link
Author

AlexAtkinson commented Jun 14, 2023

Remote Use:
source <(curl -s https://gist.githubusercontent.com/AlexAtkinson/d74df51311c27fb787de0c78ca46aa76/raw/bd24b572dbdfe635b826b54433db4e56f8c8851a/constat.sh)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment