A blog about digital independence and autonomy

Warning, this article has been created more than 6 month ago. The information it contains may not be up to date.

Check if it is possible to establish a SSH connection with Bash

Written by Mirabellette / / no comments

Hello everyone

I am continuing to write an article each month. I add some little title and subtitle to improve the ease of reading. Today, I want to share to you a little trick I use to check if it is possible to establish a SSH connection with a remote host.


Use case

  • You want to transfer some files with SSH protocol. You want to be sure it is possible to establish a connection and be notified if it is not possible.
  • You want to check periodically if it is possible to connect to a remote host with SSH.

What I do

I created a bash script to open a connection with the remote host in SSH and check if it works well. If it doesn't, I send an email to a specific address.

The main part


Both scripts are available in Github following MIT LICENCE. You can find them here.

Script to check ssh connection




Requirement and advice

  • You need to have a bash prompt after you connected to ssh.
  • You need to have a mail transfer agent properly configured.
  • You need to check when you established a SSH connection that you don't receive any warning message from SSH. In this case, the status variable will got another value than "ok" and the script will considered you aren't be able to establish a connection.



I know that it must be a better way to test that but it fit well for my use cases.

Social media

If you find this article interesting, feel free to subscribe to my RSS flux and to follow me on Mastodon. Don't hesitate to share it if you think he could interest someone else.

Classified in : Tips / Tags : none

Write a comment

What is the fifth letter of the word gnwayq?