0

I am writing a bash script that goes through a number of hosts defined in hosts.list and returns if they are online. I would like to do the same but with ssh. How to I return a value from ssh like boolean if the connection and login was successful.

#!/bin/bash

File="hosts.list"
Hosts=$(cat $File)
declare -i deadhosts
$deadhosts = 0
declare -i counter
$counter = 0
for Host in $Hosts
do
        if ! ping -c 1 -s 1 -W 1 "$Host" 1>/dev/null 2>&1; then
                deadhosts=$((deadhosts+1))
        else
                echo $Host
        fi
        counter=$((counter+1))
done

echo "Mission success"
echo "Scanned Hosts: $counter Dead Hosts: $deadhosts"
Bchru
  • 1
  • 3
    `$deadhosts = 0` `$counter = 0` will result in `=: command not found`. That's not how you assign a variable in bash. – KamilCuk Oct 18 '21 at 20:55
  • Does https://stackoverflow.com/questions/1405324/how-to-create-a-bash-script-to-check-the-ssh-connection answer your question? – KamilCuk Oct 18 '21 at 20:56
  • 2
    have you considered using a tool like `nmap` instead of writing your own script? – Barmar Oct 18 '21 at 21:01

1 Answers1

-1

There is a question that was ask for the same thing:

How to create a bash script to check the SSH connection?

And you can also find information there:

https://www.golinuxcloud.com/test-ssh-connection/

Maximilien
  • 49
  • 1
  • 8