期待脚本--密码输入错误,再去抓取密码。

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

我将在第一段抓取密码,脚本将移动到登录段,在那里,系统将检查密码,如果密码输入错误,我想显示密码错误,然后再次回到抓取密码段。

我怎么做呢?

#Grabbing Password to be used in script further
stty -echo
send_user -- "Enter the Password: "
expect_user -re "(.*)\n"
send_user "\n"
stty echo
set pass $expect_out(1,string)

#Loggin into the Gateway as Normal user
spawn ssh -o StrictHostKeyChecking=No $USER@$IP
expect "$USER@$IP's password:"
send "$pass\n"
expect
1个回答
0
投票

这就是使用procs帮助代码重用的地方。你会想要这样的东西。

proc passwd {} {
    stty -echo
    send_user -- "Enter the Password: "
    expect_user -re "(.*)\n"
    send_user "\n"
    stty echo
    return $expect_out(1,string)
}

set pass [passwd]
spawn ssh -o StrictHostKeyChecking=No $USER@$IP
expect {
    "$USER@$IP's password:" {
        send "$pass\n"
        exp_continue
    }
    "please try again." {
        # you may need to adjust the "incorrect password" pattern there
        set pass [passwd]
        exp_continue
    }
    eof {
        send_user "you have not entered the correct password. login failed.\n"
        exit 1
    }
    -re $prompt
}

# now you're logged in
© www.soinside.com 2019 - 2024. All rights reserved.