How can I make an expect script prompt for a password?

后端 未结 3 1762
半阙折子戏
半阙折子戏 2020-11-29 04:57

I have an expect script that connects to a few routers through ssh. All these routers have the same password (I know, it\'s wrong), and the script needs to know that passwo

3条回答
  •  独厮守ぢ
    2020-11-29 05:14

    Use expect's stty command like this:

    # grab the password
    stty -echo
    send_user -- "Password for $user@$host: "
    expect_user -re "(.*)\n"
    send_user "\n"
    stty echo
    set pass $expect_out(1,string)
    
    #... later
    send -- "$pass\r"
    

    Note that it's important to call stty -echo before calling send_user -- I'm not sure exactly why: I think it's a timing issue.

    expect programmers should all read the book: Exploring Expect by Don Libes

提交回复
热议问题