0

I'm fairly new to Python but I need to write a python script to extract text blocks from a command output.

Let me explain step by step.

I first run a command giving me this output:

B8A64EF4290336BD175189B23B91C5B0574C7FD8A533ADD4C6C17E2C8AB6E7478619483BC9CF177A86B18D902B05DEFD801E309B8825E3DBB82F10AF42FC2C0B0BA9623A41B9981CEE5CDFF6D4E03F90AD32E50711CDF2A9768D70D7B259F001031D1B44C7E25337CD34D5DB0FB8A03F93A9FDA9F180B0180EB0C3E5A2A2D8B4A234A72EE1E709B97645FF18E4997EFD8CA429C02B3937513C390158C5DA7E27318B6AFFC3B52F4FB8879D22CFCEFE08ABA15A6C526DC595F20BE0EAB7B4FACCCFA435214D4B28AC4FCB62D5BFBCD86799E6850138F8289DA9A6F0836CBC6FDEF7F0DE74A4444D5F1A974E19551AC924577970A8E916A64BA62E90EEA2D54EEB

Next, I need to run another command which gives me following output:

Exponent: 01 00 01 Test01 creation_date=(00000000) Modulus: fa a4 60 8c a1 d2 6d 8f 4a d0 cc 07 8d 7f 47 43 d0 ac 35 eb b0 08 dd ff 5c f7 39 2d 65 c4 12 3b 0d 96 53 66 99 d1 2d 76 6f b9 23 f7 fb cb ce 22 2e 50 01 cf 32 50 10 5d 0e c1 50 fd 98 01 2d 92 00 3d 96 41 6e 6c 19 0e e2 e0 bc 7e a1 e1 4a 11 dc 39 eb cf 1c 0a ce a9 8e fb ae ea e9 7b f5 c6 41 67 3c a3 75 21 5b 49 d9 05 e5 14 8c f4 94 c8 b3 05 94 a9 da 98 59 64 1a 00 a1 6b 1a 6b 3d c9 84 31 3b 1d d3 46 6c 0b bd 98 8b 76 38 7c b0 f7 d1 f2 7b 22 bf 39 fa dd 1e 95 5c a3 29 f7 e2 58 f5 d2 c8 40 6e c3 8d f3 69 72 f5 f0 fe a9 92 6e d1 c7 54 c4 b3 84 96 dd de 53 37 ec 7e 8d 62 8c 2c 01 7c 0f 30 36 01 46 d9 b2 5f 36 ff 5d 12 db 29 ae f1 29 49 28 8b f5 92 8a 6b 27 1b f1 3c 76 0d d9 4a b3 e9 f5 82 89 8d c1 e6 51 7e fc 98 d7 44 cb 6d 7e 7e b2 b5 e1 81 90 f2 24 90 ff 26 a9 Exponent: 01 00 01 0Test1 creation_date=(00000000) Modulus: 94 9f 2c a3 5c 88 2d 6c 6a 79 f5 d5 a1 a0 c0 d5 b2 e0 03 9e 25 fc 7e 5b 67 7a a6 5f 1d b3 29 3d c7 85 b4 20 e8 89 34 03 e1 6a 86 e4 1e ac 2e 19 6d 9b 41 76 b2 52 1b 7d 08 1c ca 51 14 4b 08 63 3e d9 6b af 8d 4f 06 2f 39 c2 69 bc b8 5f fe de 96 27 66 ef 79 67 00 e1 72 ff e4 fd 59 47 9f 7e 05 a4 a6 3d c7 f6 93 28 16 77 2b fa 25 22 81 d5 88 2b 83 e0 7a 86 93 10 0f 4d 26 92 52 a0 d9 e5 Exponent: 01 00 01 03Test creation_date=(00000000) Modulus: 8e 22 19 02 70 29 34 0d 55 06 88 43 a1 d0 4d 72 f8 9e 65 95 e8 ad ce fe 57 22 ca 74 9e 28 12 66 5d a9 7f 72 c9 e0 5b da b6 c6 dd 29 26 10 9e 07 89 2f 48 60 bd d7 ff 38 75 58 f2 bc f4 ce 9f dd e0 b9 05 1f 42 f3 70 e1 20 f4 57 f0 9c b2 e3 72 b6 e7 9c 9e 85 62 c8 74 43 27 51 da da d2 85 9a 46 bb 35 37 18 93 a1 c7 63 2b 84 f1 58 a9 be 7c c6 51 4d 90 e6 7f 6e 6d 21 65 54 08 3d ce 7c 5b

You see there's a pattern. This is one block:

Exponent: 01 00 01 Test01 creation_date=(00000000) Modulus: fa a4 60 8c a1 d2 6d 8f 4a d0 cc 07 8d 7f 47 43 d0 ac 35 eb b0 08 dd ff 5c f7 39 2d 65 c4 12 3b 0d 96 53 66 99 d1 2d 76 6f b9 23 f7 fb cb ce 22 2e 50 01 cf 32 50 10 5d 0e c1 50 fd 98 01 2d 92 00 3d 96 41 6e 6c 19 0e e2 e0 bc 7e a1 e1 4a 11 dc 39 eb cf 1c 0a ce a9 8e fb ae ea e9 7b f5 c6 41 67 3c a3 75 21 5b 49 d9 05 e5 14 8c f4 94 c8 b3 05 94 a9 da 98 59 64 1a 00 a1 6b 1a 6b 3d c9 84 31 3b 1d d3 46 6c 0b bd 98 8b 76 38 7c b0 f7 d1 f2 7b 22 bf 39 fa dd 1e 95 5c a3 29 f7 e2 58 f5 d2 c8 40 6e c3 8d f3 69 72 f5 f0 fe a9 92 6e d1 c7 54 c4 b3 84 96 dd de 53 37 ec 7e 8d 62 8c 2c 01 7c 0f 30 36 01 46 d9 b2 5f 36 ff 5d 12 db 29 ae f1 29 49 28 8b f5 92 8a 6b 27 1b f1 3c 76 0d d9 4a b3 e9 f5 82 89 8d c1 e6 51 7e fc 98 d7 44 cb 6d 7e 7e b2 b5 e1 81 90 f2 24 90 ff 26 a9

In the actual output of the command, there's 135 blocks.

What I should achieve is compare the output of my first command, with the contents of my second command, and if I find an exact match, I should return the name, which appears before creation_date. "Test01", "0Test1", etc

I can loop through the second output by creating a for loop using the number of occurrences of the word "modulus" (hence the 135 times).

The part that I'm having a hard time with is, in each step of my for loop, extract the next "block", extract only the modulus info, remove the spaces and compare it.

I could probably achieve it by hard coding some stuff but this might not work every time so I prefer to find a clean solution.

Any tips for me?

This is the code I've written so far:


#!/usr/bin/env python
import subprocess
import sys
import os


cmd1= "/opt/myCommand1"


#This returns "Modulus=verylongmodulus

So I extract only the actual modulus, removing "Modulus="

myModulus = (os.popen(cmd1).read())[8:]



cmd2 = "/opt/myCommand2"


myCommand2Output = (os.popen(cmd2).read()

occurrences = lunaCertificates.count('Modulus')

for x in range(0, occurrences):
        #Here I need to extract each block and compare it to the output of the first command. if it matches, return the name

Thank you. Steve

  • Did you write any code yet ? – scharette Jun 26 '18 at 12:17
  • Are you trying to compare output of second command block with other block's? Because as per your question, what you will achieve by comparing first output with second output block's? – van neilsen Jun 26 '18 at 12:23
  • @vanneilsen I'm trying to find the output of my first command, anywhere in the output of my second command. If I find it, extract the name just before the creation date. – user2052570 Jun 26 '18 at 12:47
  • @scharette I added the code i've written so far – user2052570 Jun 26 '18 at 12:47
  • 1. Create a list by splitting second o/p from 'Exponent:'. You will get 135 entries in list. 2. Iterate through list and then you will get Name & Modulus info. 3. Store Name and Modules info in some variables. 4. Apply replace(" ", "") on Modulus info variable to normalize. 5. Now you can compare first o/p with Modulus info and if matched return name stored in variable. 6. Repeat the same process till end and you will get desired result. :) – van neilsen Jun 26 '18 at 15:19

1 Answers1

0

IMHO, you should create a class say SegmentedCommand and variables similar to sections names like 'Exponent', 'Name', 'Modulus' etc. This is how class can be created.

Parse you second output with string split or regex and build an array of SegmentedCommand.

Loop on this array and pass your first command's output to each SegmentedCommand and compare value based on your criteria. (Here one data will be self's data and the other one is first response)

Hope that helps!

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56