Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if sshd runs on a remote machine [closed]

Tags:

linux

bash

shell

ssh -q -o "BatchMode=yes" user@host "echo 2>&1" && echo "OK" || echo "NOK"

this method is suitable but it returns true when the pub.key is copied to host. I need to see if an ssh is connectiable between two devices without keys.

Simply wants to check if the sshd is running remotely.

like image 242
barp Avatar asked Aug 15 '12 12:08

barp


People also ask

How can I tell if SSH is running on remote host?

You can try ssh localhost to test if it is running; if it respons with something like Connection refused , then it is not running. These commands must be run as root. If the server does not start automatically, try using the service sshd start command, or just reboot the computer.

How can I tell if SSH is open?

Step 1: Check if SSH is enabled To check if SSH is enabled on your system, open a command prompt and end the command ssh . If it provides you with help for using SSH, it is already enabled! You should be able to follow the Linux instructions using the ssh-keygen command from the command prompt.

Does SSH keep running after disconnect?

SSH creates a pseudo terminal and attaches it to your login session. When you end your session, the SSH terminal is also closed and all the processes running on it are terminated, unless you have configured them to keep running, even after session disconnection.

Does remote desktop use SSH?

A Major Difference between RDP and SSH RDP and SSH are designed to provide two distinct solutions for connecting to remote computer systems. RDP furnishes users with a tool for managing remote connections via a GUI. SSH offers a Secure Shell and is used for text-based management of remote machines.


1 Answers

If you just want to check if you can connect to a host via ssh, you could simply check if port 22 is open. There are various ways to to this.

Using nmap (replace localhost with your target host):

$ nmap -p22 localhost

Starting Nmap 5.21 ( http://nmap.org ) at 2012-08-15 13:18 BST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000044s latency).
PORT   STATE SERVICE
22/tcp open  ssh

Nmap done: 1 IP address (1 host up) scanned in 0.04 seconds

To use this in a script:

if nmap -p22 localhost -oG - | grep -q 22/open; then 
    echo "OK"
else 
    echo "NOK"
fi

You can also use netcat:

$ nc -zv localhost 22
Connection to localhost 22 port [tcp/ssh] succeeded!

To use this in a script:

if nc -zv localhost 80 2>&1 | grep -q succeeded; then 
    echo "OK"
else 
    echo "NOK"
fi

This is a quick check which is sufficient in most situations, however it is not fool-proof. There is no guarantee that the service listening on the remote port is actually an SSH server.

You could attempt a dummy connection and inspect the returned header, e.g:

$ echo "dummy" | nc localhost 22
SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1
Protocol mismatch.

however such an approach is undesirable for various reasons. The only guaranteed way would be to establish an actual connection as you've shown in your question.

like image 165
Shawn Chin Avatar answered Oct 23 '22 14:10

Shawn Chin