SSH端口转发(“ssh -fNL”)无法通过expect spawn自动提供密码。

3

我知道做端口转发的命令是ssh -L。我还使用其他选项来修饰它。因此,例如,最终的完整命令可能看起来像这样:ssh -fCNL *:10000:127.0.0.1:10001 127.0.0.1。在输入密码后一切正常。

然后,因为不止一个端口需要转发,我决定将工作留给Shell脚本,并使用expect(tcl)提供密码(全部相同)。

虽然没有深入了解expect,但在互联网的帮助下,我成功编写了代码。脚本成功生成ssh并提供正确的密码。但是,当我尝试使用ps -ef | grep sshnetstat -anp | grep 10000进行检查时,我发现没有这样的进程。

我给ssh添加了-v选项,输出似乎没问题。

那么问题出在哪里呢?我在互联网上搜索过,但大多数问题与端口转发无关。我不确定是否适合只想让脚本自动提供密码时使用expect。

以下是脚本。

#!/bin/sh

# Port Forwarding

# set -x

## function definition
connection ()
{
    ps -ef | grep -v grep | grep ssh | grep $1 | grep $2 > /dev/null
    if [ $? -eq 0 ] ; then
        echo "forward $1 -> $2 done"
        exit 0
    fi

    # ssh-keygen -f "$HOME/.ssh/known_hosts" -R "127.0.0.1"

    /usr/bin/expect << EOF
set timeout 30
spawn /usr/bin/ssh -v -fCNL *:$1:127.0.0.1:$2 127.0.0.1
expect {
"yes/no" {send "yes\r" ; exp_continue}
"password:" {send "1234567\r" ; exp_continue}
eof
}
catch wait result
exit [lindex \$result 3]
EOF
    echo "expect ssh return $?"
    echo "forward $1 -> $2 done"
}

## check expect available
which expect > /dev/null
if [ $? -ne 0 ] ; then
    echo "command expect not available"
    exit 1
fi

login_port="10000"
forward_port="10001"

## check whether the number of elements is equal
login_port_num=$(echo ${login_port} | wc -w)
forward_port_num=$(echo ${forward_port} | wc -w)
if [ ${login_port_num} -ne ${forward_port_num} ] ; then
    echo "The numbers of login ports and forward ports are not equal"
    exit 1
fi
port_num=${login_port_num}

## provide pair of arguments to ssh main function
index=1
while [ ${index} -le ${port_num} ] ; do
    login_p=$(echo ${login_port} | awk '{print $'$index'}')
    forward_p=$(echo ${forward_port} | awk '{print $'$index'}')
    connection ${login_p} ${forward_p}
    index=$((index + 1))
done

以下是脚本的输出结果

spawn /usr/bin/ssh -v -fCNL *:10000:127.0.0.1:10001 127.0.0.1
OpenSSH_7.2p2 Ubuntu-4ubuntu2.10, OpenSSL 1.0.2g  1 Mar 2016
...
debug1: Next authentication method: password
wang@127.0.0.1's password: 
debug1: Enabling compression at level 6.
debug1: Authentication succeeded (password).
Authenticated to 127.0.0.1 ([127.0.0.1]:22).
debug1: Local connections to *:10000 forwarded to remote address 127.0.0.1:10001
debug1: Local forwarding listening on 0.0.0.0 port 10000.
debug1: channel 0: new [port listener]
debug1: Local forwarding listening on :: port 10000.
debug1: channel 1: new [port listener]
debug1: Requesting no-more-sessions@openssh.com
debug1: forking to background
expect ssh return 0
forward 10000 -> 10001 done

不要使用密码,使用密钥。 - ceving
1个回答

0
这对您应该有效:
spawn -ignore SIGHUP ssh -f ...

更新:

另一个解决方法是:

spawn bash -c "ssh -f ...; sleep 1"

更新2(稍微解释一下):

ssh -f 调用 daemon() 来将自己变成守护进程。请参阅源代码中的 ssh.c

/* Do fork() after authentication. Used by "ssh -f" */
static void
fork_postauth(void)
{
        if (need_controlpersist_detach)
                control_persist_detach();
        debug("forking to background");
        fork_after_authentication_flag = 0;
        if (daemon(1, 1) == -1)
                fatal("daemon() failed: %.200s", strerror(errno));
}

daemon() 的实现方式类似于 this

int
daemon(int nochdir, int noclose)
{
        int fd;

        switch (fork()) {
        case -1:
                return (-1);
        case 0:
                break;
        default:
                _exit(0);
        }

        if (setsid() == -1)
                return (-1);

        if (!nochdir)
                (void)chdir("/");

        if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
                (void)dup2(fd, STDIN_FILENO);
                (void)dup2(fd, STDOUT_FILENO);
                (void)dup2(fd, STDERR_FILENO);
                if (fd > 2)
                        (void)close (fd);
        }
        return (0);
}

在父进程的_exit()和子进程的setsid()之间存在竞争条件(不确定这里是否是正确的术语)。在这里,_exit()总是会先完成,因为“函数_exit()立即终止调用进程”,而setsid()则更加耗费资源。因此,当父进程退出时,setsid()还没有生效,子进程仍然处于与父进程相同的会话中。根据apue book(我参考的是2005年版第10章:信号),如果“会话领导终止”,则会生成SIGHUP。在这种情况下,信号将发送到“前台进程组中的每个进程”。

简而言之:

  • Expect 分配一个伪终端并在该伪终端上运行 ssh。在这里,ssh 将在新会话中运行,并且成为该会话的领导者。
  • ssh -f 调用 daemon()。父进程(会话领导者)调用 _exit()。此时,子进程仍然处于会话中,因此它将收到 SIGHUP,其默认行为是终止该进程。

解决方法的工作原理:

  • nohup 方式 (spawn -ignore SIGHUP) 是显式地要求进程忽略 SIGHUP 信号,因此它不会被终止。
  • 对于 bash -c 'sshh -f ...; sleep 1'bash 将成为会话领导者,并且最后的 sleep 1 防止会话领导者过早退出。因此,在 sleep 1 之后,子进程的 setsid() 已经完成,子进程 ssh 已经处于一个新的进程会话中

更新 3:

您可以使用以下修改(在ssh.c中)编译ssh并进行验证:

static int
my_daemon(int nochdir, int noclose)
{
    int fd;

    switch (fork()) {
    case -1:
        return (-1);
    case 0:
        break;
    default:
        // wait a while for child's setsid() to complete
        sleep(1);
//      ^^^^^^^^
        _exit(0);
    }

    if (setsid() == -1)
        return (-1);

    if (!nochdir)
        (void)chdir("/");

    if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
        (void)dup2(fd, STDIN_FILENO);
        (void)dup2(fd, STDOUT_FILENO);
        (void)dup2(fd, STDERR_FILENO);
        if (fd > 2)
            (void)close (fd);
    }
    return (0);
}

/* Do fork() after authentication. Used by "ssh -f" */
static void
fork_postauth(void)
{
    if (need_controlpersist_detach)
        control_persist_detach();
    debug("forking to background");
    fork_after_authentication_flag = 0;
    if (my_daemon(1, 1) == -1)
//      ^^^^^^^^^
        fatal("my_daemon() failed: %.200s", strerror(errno));
}

哇,运行得非常好!让我猜猜,ssh进程是由expect拥有的。当expect退出时,SIGHUP也会被发送到ssh进程,因此它被关闭了。如果这是正确的,你是怎么知道的呢?如果在expect手册中提到了,请原谅我,但我进行了快速搜索,没有结果。 - HQW.ang
1
非常感谢!显然我需要更多的学习才能完全理解这些东西,哈哈。但是我很好奇,一个人如何深入挖掘源代码并理解机制。无论如何,这是巨大的鼓励,让我更加努力学习! - HQW.ang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接