如何从perl脚本中注销ssh会话?

时间:2014-06-01 14:38:13

标签: linux perl ssh debian

我有一个perl脚本在打开ssh shell后进行第二层身份验证。它要求输入密码,并且在n次无效尝试之后,脚本应该注销用户。这是在Debian系统上运行的。

现在,问题是通常使用命令exit以交互方式关闭ssh shell。

从反引号或perl脚本中的system()运行时,exit不会被识别为有效命令。那么如何从perl脚本中的ssh会话中注销用户呢?该脚本不对ssh会话负责。它在遥控器上运行,并从.bashrc开始。

这是相关的代码段:

while ($actualpass ne $password) {
    ++$attempts;
    if ( $attempts > $maxattempts ) {
        `/bin/bash /root/ascii_breach`;
        `/bin/bash exit`;
    }

/bin/bash exit显然不起作用。

2 个答案:

答案 0 :(得分:1)

听起来你有点倒退了。我认为你正在做的是ssh到运行shell的远程主机,shell运行perl然后perl想要退出shell。更好的方法是ssh到主机直接运行perl脚本。只有在身份验证通过时,perl脚本才会启动shell。

您可以配置sshd以在authorized_keys文件中运行您的身份验证脚本,假设用户是如何进入的。

答案 1 :(得分:0)

您可以启动身份验证脚本,检查退出状态并在失败时注销shell:

perl auth.pl; if [ $? -ne 0 ]; then exit; fi

然后在Perl脚本中:

if ( $attempts > $maxattempts ) {
    die 'Authentication failed';
}

您还需要停止用户使用ctrl + c或ctrl + z跳过身份验证:

trap "echo no" SIGINT SIGTSTP
perl auth.pl; if [ $? -ne 0 ]; then exit; fi