Reputation: 532
I have an expect script in which I am currently looking for multiple prompt types and sending commands in response. I am aware of regular expression matching using "-re" but I'd like to know of the correct way to achieve this.
For example, I have these prompt types:
[user@hostname ~]#
user@hostname --->
/ >
-bash-3.00$
cli>
Is this the correct/sufficient expression to detect all the above?
set multiPrompt "(%|#|cli\>|\$|\-\-\-\>)"
expect -re $multiPrompt
send "$someCommand\r"
Also, I have a list of commands, some of them cause the prompt to change after they are executed on the remote system. Due to the change in prompt, the remaining commands are not getting sent because my expect script is unable to detect the change and perform the send action.
What I'm trying to do is create a pool of possible prompts, so that my expect script sends the commands across without missing any of them. Is my approach correct?
Upvotes: 4
Views: 24287
Reputation: 21
I know it's an old thread, but for anyone searching on this I hope this helps. I am a UNIX sysadmin and have a handful of expect scripts my program calls on for various admin functions. This is the one solution I found that works in all my use cases:
set prompt "(%|#|>|\\$ )"
set prompt [string trim $prompt]
The [string trim $prompt]
handles the scenarios where some prompts have a space before the input and some don't by trimming off the space when it looks at the prompt. Example: "password:" vs. "password: "
Upvotes: 1
Reputation: 137557
While using a regular expression to detect the prompts is the right thing, choosing a good one is tricky when you've got such a wide range of possibilities. For example, I bet that this RE would work:
set multiPrompt {[#>$] }
(It just detects the end of the prompt, and ignores all the stuff before it. There's virtually always a space at the end of the prompt, used to visually separate what users type from the prompt.)
However, the problem is that this RE is fairly likely to match other things. You might instead be better off changing the prompt to a known-and-unique value (typically by setting the PS1
environment variable on the remote device) so that you get reliable detection. Mind you, that's only suitable when you're not exposing the prompts back to users, which is true for some uses of expect and not others…
Upvotes: 4