遍历主机名列表,以查看对它们使用ssh是否成功

问题描述 投票:0回答:1

我的任务已经让我头疼了很长时间。基本上,我会得到一个主机名列表以及它们的用户名和密码(每个主机名都有一个用户名和密码)。我要编写一个脚本,以便可以自动从提供的登录详细信息列表中使用ssh。我正在使用Expect脚本来做到这一点。这是我到目前为止提出的。

#!/usr/bin/expect

# Values to retrieve elements from the list based on the index
set x 0
set y 0

set HSTNMLIST [list 192.168.6.99 192.168.0.32 192.168.3.77]
set USRNMLIST [list user1 user2 user3]
set PSSWRDLIST [list asfsa bvgtrnh bdfbdfg]

foreach n $HSTNMLIST {
    spawn ssh $USRNMLIST[$x]@$n 
    expect "password"
    send "$PSSWRDLIST[$y]\r" 
    if { expect "continue connecting" } {
        send "No\r"
        incr $x
    } else {
        send "No\r"
        expect "Permission denied"
        send "No\r"
        incr $y
    }
}
interact

现在,我一直在研究如何基于循环函数中的索引来检索元素。谢谢您对我耐心等待。

expect
1个回答
0
投票

您可以做:

for {set idx 0} {$idx < [llength $HSTNMLIST]} {incr i} {
    set host [lindex $HSTNMLIST $idx]
    set user [lindex $USRNMLIST $idx]
    set pswd [lindex $PSSWRDLIST $idx]

    spawn ssh $user@$host
    ...
    send "$pswd\r"

或者您想为每个主机尝试用户名/密码的所有组合吗?

© www.soinside.com 2019 - 2024. All rights reserved.