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"
3
  • 3
    $deadhosts = 0 $counter = 0 will result in =: command not found. That's not how you assign a variable in bash. Commented Oct 18, 2021 at 20:55
  • Does stackoverflow.com/questions/1405324/… answer your question? Commented Oct 18, 2021 at 20:56
  • 2
    have you considered using a tool like nmap instead of writing your own script? Commented Oct 18, 2021 at 21:01

1 Answer 1

-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/

Sign up to request clarification or add additional context in comments.

1 Comment

This should be posted as comments and close votes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.