在密码提示时立即向SSH发送关闭信号 [英] Send close signal to SSH immediately on password prompt

查看:55
本文介绍了在密码提示时立即向SSH发送关闭信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用以下命令运行服务器测试:

I am currently running a server test with the following command:

echo ^C |ssh -q root@${server}
if [ 0 -ne $? ]; then
  echo "==> Failed to connect..."; continue
fi

不幸的是,看起来我的 ^ C 似乎并没有在密码提示后立即关闭连接,这正是我所期望的.有没有更好的方式编写此代码以测试ssh连接而不挂提示?

Unfortunately it does not look like my ^C is closing the connection immediately after a password prompt which is what I was expecting. Is there a better way to write this to test for an ssh connection without hanging on a prompt?

推荐答案

在unix系统上, ^ C 会杀死一个进程,因为当用户键入^ C时,TTY接口会向该进程发送中断信号.标准输入(从文件读取或从另一个过程的输出打印到管道)上的文字^ C只是一个常规字符.

On unix systems, ^C kills a process because the TTY interface sends an interrupt signal to the process when the user types ^C. A literal ^C on standard input (read from a file or printed to a pipe from another process's output) is just a regular character.

您可以通过运行ssh来禁止密码验证:

You could suppress password authentication by running ssh like this:

ssh -o PasswordAuthentication=no -q root@${server}

如果

ssh无法使用密码以外的其他方式进行身份验证,则它将以失败代码退出.此故障代码与连接故障无法区分,因此可能不是您想要的.

ssh will exit with a failure code if it fails to authenticate using something other than a password. This failure code is indistinguishable from a failure to connect, so this may not be what you want.

如果您主要对与远程SSH服务器端口的连接感兴趣,则不必使用实际的ssh程序进行测试.例如,您可以使用 nc (netcat)进行此测试:

If you're mainly interested in connectivity to the remote SSH server port, you don't have to use the actual ssh program to test. You could do this test with nc (netcat) for example:

$ nc -z localhost 22
$ echo $?
0
$ nc -z localhost 11122
$ echo $?
1

-z 标志告诉netcat在不发送或接收任何数据的情况下测试到指定主机和端口的连接.您可以这样构建测试:

The -z flag tells netcat to test connectivity to the specified host and port without sending or receiving any data. You could structure your test like this:

if nc -z localhost 22
then
    : success
else
    echo Connection to ${server} failed
fi

这篇关于在密码提示时立即向SSH发送关闭信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆