Last active
December 17, 2024 08:50
-
-
Save jmkim/f735a85ae3f5b4a2b5ec8a531b363d02 to your computer and use it in GitHub Desktop.
SSH port prober (parallel shell execution)
This file contains hidden or 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
#!/bin/bash | |
TITLE="SSH Port Prober" | |
DESCRIPTION="SSH port prober (parallel shell execution)" | |
VERSION="2.0" | |
AUTHOR="Jongmin Kim <[email protected]>" | |
LICENSE="Expat" | |
EXECUTABLE="$0" | |
HOST='' # Write a target host here | |
PORT_START=1 | |
PORT_INCREMENT=2048 | |
PORT_STOP=65535 | |
RUNNER_FILE_PREFIX='runner-' | |
RUNNER_FILE_SUFFIX='.sh' | |
OUTPUT_DIR='output' | |
OUTPUT_COMBINED_FILE='output.log' | |
ssh_prober_runner_generation() { | |
for i in $(seq $PORT_START $PORT_INCREMENT $PORT_STOP); do | |
local R_FILE="$RUNNER_FILE_PREFIX$i$RUNNER_FILE_SUFFIX" | |
local P_START=$i | |
local P_STOP=$(expr $P_START + $PORT_INCREMENT - 1) | |
if (( $P_STOP > $PORT_STOP )); then | |
P_STOP=$PORT_STOP | |
fi | |
local O_FILE="$OUTPUT_DIR/output-$P_START.log" | |
cat > "$R_FILE" << EOM | |
#!/bin/bash | |
for i in \$(seq $P_START $P_STOP) | |
do | |
ssh -T -o ConnectTimeout=2 -p \$i '$HOST' 1>> '$O_FILE' 2>&1 | |
done | |
EOM | |
chmod +x "$R_FILE" | |
done | |
echo "Runner files created" | |
} | |
ssh_prober_runner_execution() { | |
for i in $(seq $PORT_START $PORT_INCREMENT $PORT_STOP); do | |
local R_FILE="$RUNNER_FILE_PREFIX$i$RUNNER_FILE_SUFFIX" | |
mkdir -p "$OUTPUT_DIR" | |
echo "Running ./$R_FILE ..." | |
"./$R_FILE" & | |
done | |
echo "Runners executed" | |
} | |
ssh_prober_runner_kill() { | |
pkill -9 "$RUNNER_FILE_PREFIX" | |
if (( $? == 0 )); then | |
echo "Runners killed" | |
else | |
echo "No runners running" | |
fi | |
} | |
ssh_prober_runner_removal() { | |
rm "$RUNNER_FILE_PREFIX"*"$RUNNER_FILE_SUFFIX" | |
echo "Runner files removed" | |
} | |
ssh_prober_output_combination() { | |
for i in $(seq $PORT_START $PORT_INCREMENT $PORT_STOP); do | |
local P_START=$i | |
local O_FILE="$OUTPUT_DIR/output-$P_START.log" | |
local C_FILE="$OUTPUT_COMBINED_FILE" | |
cat "$O_FILE" >> "$C_FILE" | |
done | |
echo "Output files combined" | |
} | |
ssh_prober_output_removal() { | |
rm -r "$OUTPUT_DIR" | |
echo "Output directory removed" | |
} | |
if [[ "$1" == "create" ]]; then | |
ssh_prober_runner_generation | |
elif [[ "$1" == "start" ]]; then | |
ssh_prober_runner_execution | |
elif [[ "$1" == "stop" ]]; then | |
ssh_prober_runner_kill | |
elif [[ "$1" == "remove" ]]; then | |
ssh_prober_runner_removal | |
elif [[ "$1" == "combine-output" ]]; then | |
ssh_prober_output_combination | |
elif [[ "$1" == "remove-output" ]]; then | |
ssh_prober_output_removal | |
elif [[ "$1" == "version" ]]; then | |
echo "$TITLE" | |
echo " Description: $DESCRIPTION" | |
echo " Version: $VERSION" | |
echo " Author: $AUTHOR" | |
echo " License: $LICENSE" | |
echo " Executable: $EXECUTABLE" | |
elif [[ "$1" == "help" ]]; then | |
echo "$TITLE ($VERSION)" | |
echo "Usage: $EXECUTABLE (create|start|stop|remove|combine-output|remove-output|version|help)" | |
else | |
echo "$TITLE ($VERSION)" | |
echo "Usage: $EXECUTABLE (create|start|stop|remove|combine-output|remove-output|version|help)" 2>&1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment