-1
  1. Firstly in the code, i would like to know How can i add a for loop for CH (1-11) instead of writing for every number
  2. Also how to extract SUCCESS and FAILED message from the output (reference) For example i want the output as

CH1 : Failed

CH2: SUCCESS

CH3: Failed

: so on

I want to use regular expression and not json for this.

import pexpect
  def quick_test():   
    ch = pexpect.spawn('ssh to server')
    ch.logfile = sys.stdout
    ch.expect("Select channels")
    print ("\n########################################\n")
    ch.sendline("1")
    ch.expect("Enter ch to run:")
    ch.sendline("CH1,0")
    var1=ch.after
    print(var1)
    ch.expect("Enter Test:")
    var2=ch.before
    print(var2)
    ch.sendline("CH2,0")
    ch.expect("Enter Test:")
    var3=ch.before
    print(var3)
    ch.sendline("CH3,0")
    ch.expect("Enter Test:")
    var4=ch.before
    print(var4)
    ch.sendline("CH4,0")
    ch.expect("Enter Test:")
    var5=ch.before
    print(var5)
    ch.sendline("CH5,0")
    ch.expect("Enter Test:")
    var6=ch.before
    print(var6)
    ch.sendline("CH6,0")
    ch.expect("Enter Test:")
    var7=ch.before
    print(var7)
    ch.sendline("CH7,0")
    ch.expect("Enter Test:")
    var8=ch.before
    print(var8)
    ch.sendline("CH8,0")
    ch.expect("Enter Test:")
    var9=ch.before
    print(var9)
    ch.sendline("CH9,0")
    ch.expect("Enter Test:")
    var10=ch.before
    print(var10)
    ch.sendline("CH10,0")
    ch.expect("Enter Test:")
    var11=ch.before
    print(var11)
    ch.sendline("CH11,0")

if __name__ == '__main__':
    quick_test()

output:

    output
    ###########################################
    There are plenty of output displayed in which these below lines are included and 
    not in the given order and are displayed randomly.

CH1,0 Result: FAILED
CH2,0 Result: SUCCESS
CH3,0 Result: FAILED
CH4,0 Result: SUCCESS
CH5,0 Result: SUCCESS
CH6,0 Result: SUCCESS
CH7,0 Result: FAILED
CH8,0 Result: SUCCESS
CH9,0 Result: FAILED
CH10,0 Result: SUCCESS
CH11,0 Result: FAILED
Shishira
  • 1
  • 3

1 Answers1

0

1. Firstly in the code, i would like to know How can i add a for loop for CH (1-11) instead of writing for every number

Your basic repeatable unit, starting on "CH2", is:

ch.sendline("CH2,0")
ch.expect("Enter Test:")
var3=ch.before
print(var3)

Instead of using named variables, we will use a single variable holding a list data structure to hold n values:

vars = []

We use a for loop to iterate up to 11:

for i in range(11):
    ch.sendline("CH{},0".format(i+1))
    ch.expect("Enter Test:")
    vars[i]=ch.before
    print(vars[i])

The first variable is handled differently, so we deal with it outside the loop:

vars = []
ch.expect("Enter ch to run:")
ch.sendline("CH1,0")
var1s[0]=ch.after
print(var1)
for i in range(1, 11):
    ch.sendline("CH{},0".format(i+1))
    ch.expect("Enter Test:")
    vars[i]=ch.before
    print(vars[i])

This should print the same text to the display, and your values should still be stored in the vars list. For example, what used to be in var8 will now be in vars[7] (since arrays are zero-indexed).

2. Also how to extract SUCCESS and FAILED message from the output (reference)

Use a RegEx, such as this pattern:

^(CH\d{1,}),0 Result: (SUCCESS|FAILED)$

You will get the desired strings in two positional capture groups.

You can match against each line in the output (assuming this output is stored somewhere, such as read in from a file, and not simply printed to the display) by again using a for loop.

Make use of Python's re module:

pattern = r"^(CH\d{1,}),0 Result: (SUCCESS|FAILED)$" # r-string
sampleOutputLine = "CH1,0 Result: FAILED"
m = re.match(pattern, sampleOutputLine)

print(m.groups())

Output:

('CH1', 'FAILED')

You can then format the groups as desired, for example as:

formattedOutputLine = "{}: ".format(m.groups[0])
if m.groups[1] === "SUCCESS":
    formattedOutputLine += m.groups[1]
else:
    formattedOutputLine += m.groups[1].lower()

Assuming the output lines are stored as a list of strings in the variable output, where each string is a line:

pattern = r"^(CH\d{1,}),0 Result: (SUCCESS|FAILED)$" # r-string

formattedOutput = []
for line in output:
    m = re.match(pattern, line) # consider compiling the pattern beforehand if your output is large, for performance
    formattedOutputLine = "{}: ".format(m.groups[0])
if m.groups[1] === "SUCCESS":
    formattedOutputLine += m.groups[1]
else:
    formattedOutputLine += m.groups[1].lower()
    
    formattedOutput.append(formattedOutputLine)
zr0gravity7
  • 2,917
  • 1
  • 12
  • 33
  • Thank you so much for the solution. This expression is for re.findall? can you write inside the code as i did not understand. – Shishira Aug 23 '21 at 14:03
  • There is no need to use `re.findall()`, simply use `re.match()` as I have above. Also I am not sure how to use it in code because your problem is not well-defined. Where are the output lines stored? – zr0gravity7 Aug 23 '21 at 14:17
  • outputlines are displayed on the console. so i need to extract that line from the console output. For eg., stored in the variable "output = ch.before()". – Shishira Aug 23 '21 at 14:45
  • As i am new to stackoverflow, i need 15 reputation to upvote. So am not able to right now :) That pattern gives me a output as None. – Shishira Aug 23 '21 at 14:52
  • Does the edit satisfy your problem then? Only work would be to get ch into the `output` list, or simply make it iterable. Also, you should be able to accept answers (checkmark under upvote buttons). Thanks. – zr0gravity7 Aug 23 '21 at 15:07