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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With