I am experiencing some unexpected symptoms between Expect and SSH and was wondering if anyone could provide some insight. In a nutshell, I want to create a proc that flushes out the Expect buffer of residual characters from an SSH session. I thought of doing this by merely "expecting" a catch all regexp. However, this doesn't seem to work.
To simplify, I have provided an example below.
Given:
Prompt Format = [JohnDoe@1.2.3.4 /home]$
1) Spawn an SSH Session to server 1.2.3.4 as user JohnDoe
spawn ssh -l JohnDoe -X 1.2.3.4
2) Expect a Password prompt
expect -re {[Pp]assword:}
3) Send in JohnDoe's password
send "abc123\r"
4) Expect the User's Bash Prompt based on the given prompt format defined above. In this case, I use a regexp of a right square bracket followed by a literal dollar-sign (not an anchor)
expect -re {]\$}
5) Execute any Linux command
send "ls -al\r"
6) Execute the following two Expect commands
6a) expect -re {.*}
6b) expect -re {]\$}
My problem is that I would expect the regexp {.*} to catch everything that the spawned SSH session is returning. However, it is not. In other words, step #6a doesn't catch anything other than a single blank space (based on exp_internal debug). It's as though the SSH session does NOT want to let go and echo anything from step #5...no matter how long I wait. However, if I execute step #6b, I then see all of the output.
Can anyone tell me why the Expect command in step #6a is not catching everything? Any help would be much appreciated and thank you in advance.
To simplify, I have provided an example below.
Given:
Prompt Format = [JohnDoe@1.2.3.4 /home]$
1) Spawn an SSH Session to server 1.2.3.4 as user JohnDoe
spawn ssh -l JohnDoe -X 1.2.3.4
2) Expect a Password prompt
expect -re {[Pp]assword:}
3) Send in JohnDoe's password
send "abc123\r"
4) Expect the User's Bash Prompt based on the given prompt format defined above. In this case, I use a regexp of a right square bracket followed by a literal dollar-sign (not an anchor)
expect -re {]\$}
5) Execute any Linux command
send "ls -al\r"
6) Execute the following two Expect commands
6a) expect -re {.*}
6b) expect -re {]\$}
My problem is that I would expect the regexp {.*} to catch everything that the spawned SSH session is returning. However, it is not. In other words, step #6a doesn't catch anything other than a single blank space (based on exp_internal debug). It's as though the SSH session does NOT want to let go and echo anything from step #5...no matter how long I wait. However, if I execute step #6b, I then see all of the output.
Can anyone tell me why the Expect command in step #6a is not catching everything? Any help would be much appreciated and thank you in advance.