Handle multiple statement in expect script

痞子三分冷 提交于 2019-12-04 11:44:17

You can use exp_continue in such a case:

expect {
      "Are you sure you want to continue connecting (yes/no)" {
            send "yes\r"
            exp_continue
      }
      "root@$LinuxMachine's password:" {
            send "root123\r"
            expect "[root@Client_FC12_172_85 ~]#"
            send "ls\r"
            interact
      }
}

In the above, the expect block waits for either the yes/no question OR the prompt for password. If the latter, it moves on with providing password, expecting prompt, sending ls command and giving control back. If the former, it will answer 'yes' and repeat the expect block, ready to find the prompt for a password (or even the yes/no question again, for that matter - not that you will need that).

I would also include some timeouts with meaningful messages in case some expect option does not match as expected, but the above should work.

As a side comment, you don't want to set a root password in a script... I recommend using ssh key authentication.

Let me know how it goes.

We like to call it "long log in", there are ssh options that don't check the host keys:

send -- "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no username@host\n"
expect {
        "Password" {
                send -- "$passwd\n"
        }

Part of the bash script that calls on the expect sets the password:

echo -n "Enter LDAP password: " 
read -s passwd
echo
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!