ssh 如何从 tty 接收密码? [英] How does ssh receive password from tty?

查看:50
本文介绍了ssh 如何从 tty 接收密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 openssh 如何在登录时获取密码,因为我陷入了将密码自动输入到 linux 中类似工具的问题,这需要像 ssh 一样从 tty 获取密码.

I was wondering how openssh gets the password when login, cause I got stuck in automating entering passwords to the similar tools in linux which requires getting password from tty like ssh.

尝试了解sshpass,发现sshpass fork了一个pid相同的子进程,然后在子进程下输入密码.

Tried to understand sshpass and found that sshpass forks a child process with the same pid then enters the password under the child process.

不知道我的猜测是否正确,ssh 需要检查正确的 pid,因为我无法使用另一个进程输入 ssh 密码来标准输入到当前的 tty.

Don't know if my guess was right that ssh needs to check the right pid since I cannot stdin to the current tty using another process to enter the ssh password.

推荐答案

出于安全原因,许多程序需要用户交互输入密码.相当多的程序在从 stdin 读取密码之前使用以下类型的检查:

For security reasons, many programs requires a password interactively from users. Quite many programs uses the following kind of check before reading a password from stdin:

if (isatty(STDIN_FILENO) == 0)
{
    exit(EXIT_FAILURE); 
}

所以程序只允许从终端输入密码.这样它会尽量防止非交互式密码输入.

So the program allows password only from a terminal. That way it try to prevent non-interactive password entering.

sshpass 只是一个工具:

sshpass is just a tool for:

欺骗 ssh 以为它是从交互式用户那里获取密码.[来自 sshpass 的手册页]

fooling ssh into thinking it is getting the password from an interactive user. [from man page of sshpass]

为了欺骗 sshsshpass 创建并打开一个伪终端,并为 ssh 的 stdin 提供它.需要 fork() 因为 sshpass 必须通过伪终端向 ssh 写入密码.

For fooling ssh, sshpass creates and open a pseudo terminal, and gives that for stdin of ssh. fork() is needed because sshpass must write a password to ssh via the pseudo terminal.

这样ssh进程的stdin就是一个终端,isatty测试会通过.

This way stdin of ssh process is a terminal, and isatty test will be passed.

这篇关于ssh 如何从 tty 接收密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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