Enhance launch-cluster script with improved node detection and SSH scanning using netcat and Python

This commit is contained in:
Eugene Rakhmatulin
2025-12-18 14:52:23 -08:00
parent 6c04ebfca1
commit db5c443905

View File

@@ -137,50 +137,60 @@ fi
if [[ -z "$NODES_ARG" ]]; then if [[ -z "$NODES_ARG" ]]; then
echo "Auto-detecting nodes..." echo "Auto-detecting nodes..."
if ! command -v avahi-browse &> /dev/null; then if ! command -v nc &> /dev/null; then
echo "Error: avahi-browse not found. Please install avahi-utils." echo "Error: nc (netcat) not found. Please install netcat."
exit 1 exit 1
fi fi
# Get local IP of the selected ETH_IF if ! command -v python3 &> /dev/null; then
LOCAL_IP=$(ip -4 addr show "$ETH_IF" | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | head -n 1) echo "Error: python3 not found. Please install python3."
if [[ -z "$LOCAL_IP" ]]; then
echo "Error: Could not determine IP for interface $ETH_IF"
exit 1 exit 1
fi fi
echo " Detected Local IP: $LOCAL_IP" # Get CIDR of the selected ETH_IF
CIDR=$(ip -o -f inet addr show "$ETH_IF" | awk '{print $4}' | head -n 1)
if [[ -z "$CIDR" ]]; then
echo "Error: Could not determine IP/CIDR for interface $ETH_IF"
exit 1
fi
LOCAL_IP=${CIDR%/*}
echo " Detected Local IP: $LOCAL_IP ($CIDR)"
DETECTED_IPS=("$LOCAL_IP") DETECTED_IPS=("$LOCAL_IP")
# Scan for other nodes echo " Scanning for SSH peers on $CIDR..."
echo " Scanning for peers via avahi..."
# Run avahi-browse, filter for _ssh._tcp, and look for our interface
# Note: avahi-browse output format varies, we use -p (parsable)
# Format: =;interface;IPv4;name;type;domain;hostname;ip;port;txt
# We only care about services on our selected ETH_IF or related interfaces? # Generate list of IPs using python
# The reference script scans ALL interfaces found by ibdev2netdev. ALL_IPS=$(python3 -c "import ipaddress, sys; [print(ip) for ip in ipaddress.ip_network(sys.argv[1], strict=False).hosts()]" "$CIDR")
# Let's stick to the reference logic: scan on all IB-associated interfaces.
TEMP_FILE=$(mktemp) TEMP_IPS_FILE=$(mktemp)
trap 'rm -f "$TEMP_FILE"' EXIT
avahi_output=$(avahi-browse -p -r -f -t _ssh._tcp 2>/dev/null) # Scan in parallel
for ip in $ALL_IPS; do
# Skip own IP
if [[ "$ip" == "$LOCAL_IP" ]]; then continue; fi
# Filter by the selected management interface (ETH_IF) (
echo "$avahi_output" | grep ";$ETH_IF;" > "$TEMP_FILE" # Check port 22 with 1 second timeout
if nc -z -w 1 "$ip" 22 &>/dev/null; then
echo "$ip" >> "$TEMP_IPS_FILE"
fi
) &
done
# Extract IPs # Wait for all background scans to complete
while IFS=';' read -r prefix iface protocol name type domain hostname ip port txt; do wait
if [[ "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
# Avoid duplicates # Read found IPs
if [[ ! " ${DETECTED_IPS[@]} " =~ " ${ip} " ]]; then if [[ -f "$TEMP_IPS_FILE" ]]; then
while read -r ip; do
DETECTED_IPS+=("$ip") DETECTED_IPS+=("$ip")
echo " Found peer: $ip ($hostname)" echo " Found peer: $ip"
done < "$TEMP_IPS_FILE"
rm -f "$TEMP_IPS_FILE"
fi fi
fi
done < <(grep "^=" "$TEMP_FILE" | grep "IPv4")
# Sort IPs # Sort IPs
IFS=$'\n' SORTED_IPS=($(sort <<<"${DETECTED_IPS[*]}")) IFS=$'\n' SORTED_IPS=($(sort <<<"${DETECTED_IPS[*]}"))