Expect pattern matching in the command output

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect pattern matching in the command output
# 1  
Old 03-03-2018
Expect pattern matching in the command output

This is the command output need to be matched:
[snr_core.serconsole.con info] Telnet console listening to port 42365.
(the port number changes every time)

Code to test it:
exp_internal 1

set timeout 10

spawn bash
set bashId $spawn_id

send_user -i $bashId "\n"
send_user -i $bashId "\[snr_core.serconsole.con info\] Telnet console listening to port 42365.\n"
expect -i $bashId -re "\[snr_core.serconsole.con info\] Telnet console listening to port \[0-9]*\." {send_user "The output is $expect_out(0,string)\n"}

It send_user a line, the same as the expected output, then catch it and display $expect_out(0,string).

This is the debug output:
spawn bash
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {115854}

[snr_core.serconsole.con info] Telnet console listening to port 42365.

expect: does "" (spawn_id exp4) match regular expression "[snr_core.serconsole.con info] Telnet console listening to port [0-9]*."? no
expect: does "\u001b]0;ecuser@antxec10:~/user_workspace/master/tools/AcpTestTool\u0007\u001b[?1034hantxec10:/user_workspace/master/tools/AcpTestTool:" (spawn_id exp4) match regular expression "[snr_core.serconsole.con info] Telnet console listening to port [0-9]*."? no
expect: timed out

I also tried different escape sequences, brace, double/triple backslash etc., no luck.

Appreciate for any help

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-03-2018 at 06:13 AM.. Reason: Added CODE tags.
# 2  
Old 03-03-2018
isn't it possible an invisible character produced ?
expect -i $bashId -re "\[snr_core.serconsole.con\s+info\]\s+Telnet\s+console\s+listening\s+to\s+port\s+\[0-9]+\."  {send_user "The output is $expect_out(0,string)\n"}

# 3  
Old 03-03-2018
This code to match the send_user line, there is no invisible character.
I try
expect -i $bashId -re "\[snr_core.serconsole.con\s+info\]\s+Telnet\s+console\s+listening\s+to\s+port\s+\[0-9]+\."  {send_user "The output is $expect_out(0,string)\n"}

It does not work.
# 4  
Old 03-04-2018
send_user just logs text to stdout it doesn't send it to the bash command as input, so you cant use expect to test for it.
# 5  
Old 03-22-2018
This the real output:
>>> run_test_analysis()
[Info] Running DataCapture and Validation for Test(1).
[Info] Test(1) passed.

command run_test_analysis() will output Test(1) passed or failed. I want to catch the line:
[Info] Test(1) xxxxx.

exp_send -i $bashId "run_test_analysis()\r"
expect -i $bashId -re "\[Info\] Test\(1\) .*\."    
set testResult [lindex [split $expect_out(0,string) " ."] end-1]
send_user "\nTest(1): $testResult\n"

I use different sequences in expect:
expect -i $bashId -re "\\\[Info\\\] Test\(1\) .*\."
expect -i $bashId -re "\\[Info\\] Test\(1\) .*\."
expect -i $bashId -re "\[Info\] Test\(1\) .*"
expect -i $bashId -re ".*Test(1).*"
expect -i $bashId -re ".* Test\(1\) .*"
expect -i $bashId -re ".*Test(1) \[a-z]*\."

Also tried global pattern match, no one work.
Can someone shed some light on it?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX command to get the latest file and also matching pattern

we want to fetch the latest file in a given directory and also the file name should match the below pattern Example file name ->hrdata-2015-10-13-16-45-26.xml(2015-10-13-16-45-26- it is not current timestamp, we just need to check for the pattern) We expect the file will have the... (3 Replies)
Discussion started by: vishwanath001
3 Replies

2. Shell Programming and Scripting

Pattern Matching and creating output

HI Unix Forum, My requirement I have two set of Patterns UBA and CIE for which different Phases are there which will have Start and End time. They are not in same order. I want the o/p in the below mentioned format. Eg: Mangolia Alien 03:04:56 Phase 0 started (10... (5 Replies)
Discussion started by: TechGyaann
5 Replies

3. Shell Programming and Scripting

Expect - assigning UNIX command output to a variable

Hi, I'm writing a script that connects through ssh (using "expect") and then is supposed to find whether a process on that remote machine is running or not. Here's my code (user, host and password are obviously replaced with real values in actual script): #!/usr/bin/expect set timeout 1... (3 Replies)
Discussion started by: oseri
3 Replies

4. Shell Programming and Scripting

Expect Script - Pattern Matching Trouble

I am still learning expect, For the below script I always get This is True as the answer. Tried to debug and does not make sense to me. Not sure, where I am doing the mistake. Need Help! - Thanks #!/usr/bin/expect -f set timeout 10 spawn -noecho bash expect { *$* } send "test -d... (3 Replies)
Discussion started by: rmsagar
3 Replies

5. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

6. Shell Programming and Scripting

Understanding pattern matching used in a grep command

I have the following code. I want to remove the --sort=num/num/... and am using grep to exclude it as shown below: I have a bit of problem figuring out the use of - at the front echo "--sort=4/5/6" | grep -ivE '-((sort|group)=+/+(/+)*)$' Now suppose I want to remove --quiet I can... (7 Replies)
Discussion started by: kristinu
7 Replies

7. HP-UX

pgrep doesn't perform full command line pattern matching

Hi! I need to get PID of some particular process and I wonder if I can use pgrep tool for this purpose. The problem is that pgrep doesn't perform pattern matching on the whole command line, even if I use -f key. Parsing output of ps command is not quite convenient... Also deamon, which PID I need... (2 Replies)
Discussion started by: Sapfeer
2 Replies

8. AIX

matching pattern in 'ps' command

Hi all, I have two instance of jboss (jboss and jboss2). In a shell script that I am writing, I need to grep for each instance of jboss. If i use ps -ef|grep 'jboss', it will also return the process for my second instance of jboss, which is jboss2. Can someone tell me how can i specify the... (1 Reply)
Discussion started by: haroon_a
1 Replies

9. UNIX for Dummies Questions & Answers

how to save the output of command in tcl/expect

hi, everyone: I just wonder how to save the output of command, I mean everything, save as a string into a variable. another question is I try to ls the details of a directory, but it works in the shell, not in the script. for example code: ls -ltr *se100* | grep ^- | tail -1 | awk '... (1 Reply)
Discussion started by: allenxiao7
1 Replies

10. Shell Programming and Scripting

using command line arguments as columns for pattern matching using awk

Hi, I wish to use a column, as inputted by a user from command line, for pattern matching. awk file: { if($1 ~ /^8/) { print $0> "temp2.csv" } } something like this, but i want '$1' to be any column as selected by the user from command line. ... (1 Reply)
Discussion started by: invinclible0009
1 Replies
Login or Register to Ask a Question

Featured Tech Videos