Using conditional statements inside 'expect'

前端 未结 3 971
感情败类
感情败类 2020-12-01 05:27

I need to automate logging into a TELNET session using expect, but I need to take care of multiple passwords for the same username.

Here\'s the flow

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 05:54

    With a bit of bashing I found a solution. Turns out that expect uses a TCL syntax that I'm not at all familiar with:

    #!/usr/bin/expect
    set pass(0) "squarepants"
    set pass(1) "rhombuspants"
    set pass(2) "trapezoidpants"
    set count 0
    set prompt "> "
    spawn telnet 192.168.40.100
    expect {
      "$prompt" {
        send_user "successfully logged in!\r"
      }
      "password:" {
        send "$pass($count)\r"
        exp_continue
      }
      "login incorrect" {
        incr count
        exp_continue
      }
      "username:" {
        send "spongebob\r"
        exp_continue
      }
    }
    send "command1\r"
    expect "$prompt"
    send "command2\r"
    expect "$prompt"
    send "exit\r"
    expect eof
    exit
    

    Hopefully this will be useful to others.

提交回复
热议问题