期望脚本:需要一种方法来验证 SSH 连接不需要密码

Expect script: Need a means to validate an SSH connection does not need a password

我是一名系统测试员,我有一组特定的 DUT,可以 运行 在固件的预发布版本或候选发布版本上。在预发布版中,我可以通过使用特定用户帐户登录来访问 DUT 的 Linux 核心 OS。候选版本不允许这样做。

我正在编写的脚本的全部基础是能够远程执行驻留在 DUT 上的脚本。我想检查是否需要密码才能访问 Linux 核心 OS。如果我确实有访问权限,则继续,否则退出脚本。

我已经尝试了很多东西,但在针对发布候选版本进行测试时,每一个都失败了,因为需要密码。这是最新的尝试:

    set status [catch {exec ssh $user@$host ls} result]
    if { [regexp "Password:"  $result]} then {
        # A password was asked for. Fail
        puts "This is a Release Candidate Version\nAccess to the Linux OS is denied"
        exit
    } else {
        # no password needed. Success
        puts "This is a Pre-Release version"
    }

针对预发布版本执行此代码时有效。但是当需要密码时,它不会因为 SSH 会话提示输入密码并等待输入。

有没有人有解决方法来打破所需的密码场景?

谢谢

如果您遇到与远程系统的连接可能要求输入密码,但并非总是 ,你最好从 expect 中进行连接。这是因为可以告诉 expect 命令同时等待几个不同的事情。

set timeout 20; # 20 seconds; if things don't respond within that, we've got problems

# 'echo OK' because it is quick and produces known output
spawn ssh $user@$host echo OK

# Wait for the possibilities we know about; note the extra cases!
expect {
    "Password:" {
        # Password was asked for
        puts "This is a Release Candidate Version\nAccess to the Linux OS is denied"
        close
        exit
    }
    "OK" {
        # Password not asked for
        puts "This is a Pre-Release version"
    }
    timeout {
        puts "There was a network problem? Cannot continue test"
        close
        exit 1
    }
    eof {
        puts "Inferior ssh exited early? Cannot continue test"
        close
        exit 1
    }
}
close

# ... now you've checked the connection ...