#!/bin/bash
 
NETWORK="192.168.1.0/24"
USER="user"
 
# Don'n scan the host
MY_IP=$(hostname -I | awk '{print $1}')
 
echo "[*] Scanning $NETWORK..."
 
# -oG (Grepable) Format, so the IP addr is at $2 steadily
IP_LIST=$(sudo nmap -sn $NETWORK -oG - | awk '/Up$/ {print $2}')
 
for IP in $IP_LIST; do
    if [[ "$IP" == "192.168.1.1" ]] || [[ "$IP" == "$MY_IP" ]]; then
        continue
    fi
 
    echo "[+] Trying to connect to : $IP"
	
    # -o BatchMode=yes: If need password to login, skip it (good for automation)
    # -o StrictHostKeyChecking=no: Accept Host Key
    ssh -o ConnectTimeout=7 \
        -o BatchMode=yes \
        -o StrictHostKeyChecking=no \
        "$USER@$IP" \
        "hostname; uptime" # If success, execute `hostname` command
 
    if [ $? -eq 0 ]; then # If the command executed
        echo "[!] Login Success $IP"
    else
        echo "[-] Can't login $IP"
    fi
    echo "--------------------------"
done