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
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).
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.