Expect within a Bash script

后端 未结 3 988
無奈伤痛
無奈伤痛 2020-12-31 13:04

I wrote a Bash script with Expect within, to connect to a terminal server and clear lines.

I am unable to figure out the error I am getting, as in I have given all th

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-31 13:49

    @Chris: I incorporated the changes you suggested and my code is working now.

    However, I had to make two more changes stated below:

    1. The single quote which you mentioned prevents parameter substitution. For example, I cannot write $IP in place of 1.1.1.1. Hence, to get around this, I removed the single quotes and replaced with double quotes. As you mentioned nested doubles quotes are not interpreted by Bash which is true. Hence I rewrote the inside double quotes as

       send \"password1\r\"
      

      That is adding backslashes before the double quotes inside. This corrects the problem of parameter substitution.

    2. Even after I put two/three actions within a single expect command, as they run in parallel I still faced issues. So taking your suggestion, I put each of the action in a separate Expect command. Something like:

       expect {
           Prompt>          { send "en\r"; exp_continue }
       }
       expect  {
           Password:        { send "password2\r" }
       }
      

提交回复
热议问题