如何创建一个 bash 脚本来检查 SSH 连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1405324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a bash script to check the SSH connection?
提问by chutsu
I am in the process of creating a bash script that would log into the remote machines and create private and public keys.
我正在创建一个 bash 脚本,该脚本将登录到远程机器并创建私钥和公钥。
My problem is that the remote machines are not very reliable, and they are not always up. I need a bash script that would check if the SSH connection is up. Before actually creating the keys for future use.
我的问题是远程机器不是很可靠,而且它们并不总是正常运行。我需要一个 bash 脚本来检查 SSH 连接是否已启动。在实际创建密钥以供将来使用之前。
回答by
You can check this with the return-value ssh gives you:
您可以使用 ssh 为您提供的返回值进行检查:
$ ssh -q user@downhost exit
$ echo $?
255
$ ssh -q user@uphost exit
$ echo $?
0
EDIT: Another approach would be to use nmap (you won't need to have keys or login-stuff):
编辑:另一种方法是使用 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).
但是您必须 grep 消息(nmap 不使用返回值来显示端口是否被过滤、关闭或打开)。
EDIT2:
编辑2:
If you're interested in the actual state of the ssh-port, you can substitute grep open
with egrep 'open|closed|filtered'
:
如果您对 ssh 端口的实际状态感兴趣,可以替换grep open
为egrep 'open|closed|filtered'
:
$ nmap host -PN -p ssh | egrep 'open|closed|filtered'
Just to be complete.
只是为了完整。
回答by Brian Ott
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
回答by Adrià Cidre
You can use something like this
你可以使用这样的东西
$(ssh -o BatchMode=yes -o ConnectTimeout=5 user@host echo ok 2>&1)
This will output "ok" if ssh connection is ok
如果 ssh 连接正常,这将输出“ok”
回答by iarroyo
Complementing the response of @Adrià Cidre
you can do:
补充@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
回答by GUESSWHOz
Try:
尝试:
echo quit | telnet IP 22 2>/dev/null | grep Connected
回答by Mathieu C.
Just in case someone only wishes to check if port 22 is open on a remote machine, this simple netcat command is useful. I used it because nmap and telnet were not available for me. Moreover, my ssh configuration uses keyboard password auth.
万一有人只想检查远程机器上的端口 22 是否打开,这个简单的 netcat 命令很有用。我使用它是因为 nmap 和 telnet 对我不可用。此外,我的 ssh 配置使用键盘密码身份验证。
It is a variant of the solution proposed by GUESSWHOz.
它是 GUESSWHOz 提出的解决方案的变体。
nc -q 0 -w 1 "${remote_ip}" 22 < /dev/null &> /dev/null && echo "Port is reachable" || echo "Port is unreachable"
回答by Jonathan H
If you would like to check a remote folder exists, or any other file-test really:
如果您想检查远程文件夹是否存在,或者任何其他文件测试真的:
if [ -n "$(ssh "${user}@${server}" [ -d "$folder" ] && echo 1; exit)" ]; then
# exists
else
# doesn't exist
fi
Do not forget the quotes in "$(ssh ...)"
.
不要忘记中的引号"$(ssh ...)"
。
回答by Necktwi
To connect to a server with multiple interfaces
连接到具有多个接口的服务器
ssh -o ConnectTimeout=1 -q [email protected];[ $? = 1 ] || ssh -o ConnectTimeout=1 -q [email protected]
回答by Mike Q
Example Using BASH 4+ script:
使用 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
回答by Dheeraj Kumar
https://onpyth.blogspot.com/2019/08/check-ping-connectivity-to-multiple-host.html
https://onpyth.blogspot.com/2019/08/check-ping-connectivity-to-multiple-host.html
Above link is to create Python script for checking connectivity. You can use similar method and use:
上面的链接是创建用于检查连通性的 Python 脚本。您可以使用类似的方法并使用:
ping -w 1 -c 1 "IP Address"
Command to create bash script.
创建 bash 脚本的命令。