Last active
March 26, 2023 08:02
-
-
Save L1Cafe/2acadb26d12529f2d0e5c7cdc6ad1cb1 to your computer and use it in GitHub Desktop.
Scan for SSH hosts, then try to connect to them using your keyfile
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 | |
default_network_range="192.168.0.0/24" | |
default_username="root" | |
default_port=22 | |
# Prompt user for input and set default values if not provided | |
echo "Enter network range (default: $default_network_range): " | |
read network_range | |
network_range=${network_range:-$default_network_range} | |
echo "Enter username (default: $default_username): " | |
read username | |
username=${username:-$default_username} | |
echo "Enter port number (default: $default_port): " | |
read port | |
port=${port:-$default_port} | |
# Print the final values | |
echo "Network range: $network_range" | |
echo "Username: $username" | |
echo "Port: $port" | |
if ! command -v nmap &> /dev/null; then | |
echo "nmap could not be found. Please install nmap and try again." | |
exit 1 | |
fi | |
echo "Scanning network for open SSH ports..." | |
hosts_with_ssh_port=$(nmap -p $port --open -oG - $network_range | awk '/open/{print $2}' | tail -n +2) | |
if [[ -z $hosts_with_ssh_port ]]; then | |
echo "No hosts with open SSH ports found." | |
else | |
echo "Found hosts with open SSH ports:" | |
echo "$hosts_with_ssh_port" | |
echo "Attempting to connect using specified SSH key..." | |
for host in $hosts_with_ssh_port; do | |
echo "Trying to connect to $host..." | |
ssh -p $port -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -o "LogLevel QUIET" -t "$username@$host" true | |
if [[ $? -eq 0 ]]; then | |
echo "Successfully connected to $host" | |
else | |
echo "Failed to connect to $host" | |
fi | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment