如何创建 bash 脚本来检查 SSH 连接?

我正在创建一个 bash 脚本,该脚本将登录到远程计算机并创建私钥和公钥。

我的问题是远程计算机不是很可靠,而且它们并不总是运行正常。我需要一个 bash 脚本来检查 SSH 连接是否正常。在实际创建将来使用的键之前。

203922 次浏览

您可以使用 ssh 给出的返回值来检查这一点:

$ ssh -q user@downhost exit
$ echo $?
255


$ ssh -q user@uphost exit
$ echo $?
0

编辑: 另一种方法是使用 nmap (您不需要有密钥或登录内容) :

$ a=`nmap uphost -PN -p ssh | grep open`
$ b=`nmap downhost -PN -p ssh | grep open`


$ echo $a
22/tcp open ssh
$ echo $b
(empty string)

But you'll have to grep the message (nmap does not use the return-value to show if a port was filtered, closed or open).

编辑2:

如果您对 ssh-port 的实际状态感兴趣,可以用 egrep 'open|closed|filtered'替换 grep open:

$ nmap host -PN -p ssh | egrep 'open|closed|filtered'

只是为了完整。

我觉得你在试图解决错误的问题。您不应该试图使 ssh 守护进程更稳定吗?尝试运行类似于 monit的程序,它将检查守护进程是否正在运行,如果没有,则重新启动它(这样您就有时间找到关闭 sshd 背后的根问题)。还是网络服务很麻烦?看看 man ifup。整件事就是想让你闭嘴吗?嗯,这是一个更大的问题... ... 试着查看您的日志(从 syslog 开始) ,找出正在关闭您的盒子的硬件故障或服务(也许是一个温度监视器?).

使您的脚本具有容错性非常好,但是您可能也希望使您的盒子具有容错性。

试试:

echo quit | telnet IP 22 2>/dev/null | grep Connected
ssh -q -o "BatchMode=yes" -i /home/sicmapp/.ssh/id_rsa <ID>@<Servername>.<domain> "echo 2>&1" && echo $host SSH_OK || echo $host SSH_NOK

You can use something like this

$(ssh -o BatchMode=yes -o ConnectTimeout=5 user@host echo ok 2>&1)

如果 ssh 连接正常,这将输出“ ok”

作为对 @Adrià Cidre回应的补充,你可以做到:

status=$(ssh -o BatchMode=yes -o ConnectTimeout=5 user@host echo ok 2>&1)


if [[ $status == ok ]] ; then
echo auth ok, do something
elif [[ $status == "Permission denied"* ]] ; then
echo no_auth
else
echo other_error
fi

为了防止有人只想检查远程机器上的端口22是否打开,这个简单的 netcat 命令非常有用。我使用它是因为 nmap 和 telnet 对我来说不可用。此外,我的 ssh 配置使用键盘密码 auth。

它是 GUESSWHOz 提出的解决方案的一个变体。

nc -q 0 -w 1 "${remote_ip}" 22 < /dev/null &> /dev/null && echo "Port is reachable" || echo "Port is unreachable"

如果您想检查远程文件夹是否存在,或者其他文件测试:

if [ -n "$(ssh "${user}@${server}" [ -d "$folder" ] && echo 1; exit)" ]; then
# exists
else
# doesn't exist
fi

不要忘记在 "$(ssh ...)"的报价。

连接到具有多个接口的服务器

ssh -o ConnectTimeout=1 -q Necktwi@192.168.1.61;[ $? = 1 ] || ssh -o ConnectTimeout=1 -q Necktwi@192.168.1.51

使用 BASH 4 + 脚本示例:

# -- ip/host and res which is result of nmap (note must have nmap installed)
ip="192.168.0.1"
res=$(nmap ${ip} -PN -p ssh | grep open)


# -- if result contains open, we can reach ssh else assume failure) --
if [[ "${res}" =~ "open" ]] ;then
echo "It's Open! Let's SSH to it.."
else
echo "The host ${ip} is not accessible!"
fi

Https://onpyth.blogspot.com/2019/08/check-ping-connectivity-to-multiple-host.html

上面的链接是创建用于检查连接性的 Python 脚本。 你可以使用类似的方法:

ping -w 1 -c 1 "IP Address"

Command to create bash script.

Below ssh command should have an exit code of 0 on a successful connection and a non-zero value otherwise.

ssh -q -o BatchMode=yes user@remote.com exit


if [ $? != "0" ]; then
echo "Connection failed"
fi

在@user156676之后,检查一系列的 ip:

#!/bin/sh
IP='192.168.0.'
PWD='your_password'
USR='your_usr'


for i in $(seq 229 255);do
sshpass -p $PWD ssh -q -o ConnectTimeout=3 ${USR}@${IP}${i} exit
let ret=$?
if [ $ret -eq 5 ]; then
echo $IP$i "Refused!"  $ret
elif [ $ret -eq 255 ] ; then
echo $IP$i "Server Down!" $ret
elif [ $ret -eq 0 ] ; then
echo $IP$i "Connnected!" $ret
else
echo $IP$i "Unknown return code!" $ret
fi
done

我编写这个脚本是为了检查 netcat 和 SSH 对服务器/etc/hosts 中所有主机的连接

逐行读取/etc/hosts,然后尝试使用 netcat 端口22,然后使用 ssh 作为“ sutter”用户

检查网络健全性的快速方法

script uses a "sshuttle" user, this is an account that has pub/priv keys on all my hosts and can ssh anywhere (non root account), we use this acct to spin up sshuttle VPN tunnels, but you can add any account that has SSH access to servers

https://gist.github.com/perfecto25/8687d563716ba4923c77162be724beda

输出,

./conncheck.sh




netcat is installed, proceeding..
--------------------------------------
tm-us1 (127.0.0.1): ssh OK | nc OK
--------------------------------------
localhost (127.0.0.1): ssh OK | nc OK
--------------------------------------
atlas (192.168.142.21): ssh ERROR | nc OK
--------------------------------------
hydra (192.168.142.22): ssh OK | nc OK
--------------------------------------
nemesis (192.168.140.23): ssh OK | nc OK
--------------------------------------
vulcan (192.168.140.24): ssh OK | nc OK
--------------------------------------
athena (192.168.140.27): ssh OK | nc OK
--------------------------------------
nas1 (192.168.100.101): ssh ERROR | nc OK
--------------------------------------
tm-dev (192.10.23.71): ssh ERROR | nc ERROR
--------------------------------------
WARNING: Your password has expired.
Password change required but no TTY available.
infra01 (192.10.23.186): ssh ERROR | nc OK
--------------------------------------
ns-us1 (192.10.23.252): ssh ERROR | nc OK
--------------------------------------
ns-us2 (192.10.23.182): ssh ERROR | nc OK
--------------------------------------
proxy-us1 (192.10.23.120): ssh OK | nc OK
--------------------------------------
simtm-us1 (192.10.23.236): ssh OK | nc OK
--------------------------------------
tm-us1 (192.10.23.104): ssh OK | nc OK
--------------------------------------
tm-us2 (192.10.23.215): ssh OK | nc OK
--------------------------------------
tm-dev (192.10.23.77): ssh OK | nc OK
--------------------------------------
WARNING: Your password has expired.
Password change required but no TTY available.
tm-uat (192.10.23.225): ssh ERROR | nc OK
--------------------------------------
vpn-us1 (192.10.23.193): ssh OK | nc OK
--------------------------------------