I have a mytext.txt
file and its contents as follows:
...
I 07/06/2022 10:26:19:301 y2031211 doing| deleting data for now with 0: function: 122, uname: <hidden>, upass: <hidden>
I 07/06/2022 10:26:19:301 y2031211 doing| Reg req: M = [P] URI = [https://myURIxxxxx.com/mb/nr/action] BODY = [parameters=bl64lI2hMI8/u OUQfYmUw83/0lJ/2s2sGaEl2ycy/mtiIf6nC9G5I2OwDHw
lh/Ds22ScsXdugLeLL0sqq5KSZyRaScv/Nk3/sWUSPEzDDibmXEW3t/qGJ39
k5qIBypH0KS8EcSnPg/CPHgH44Uzi1VBZZwai4TUj26IOWOYKD7HpasZXf1k
5hz8WQfxeC2iz9RFV27GEabyzgu9 OK3c9ffVjplO5QFo10nCefarXcLlbiT
sBKpeyk9jQ9/3rh UM83ZVzAegzsmgQ4tLfkmV2ou6Kl7gNm9w28DGp8Mnf
ZXFTw4REddcZA2jMxQOIHE92wYSy5bDVfoIADnUqSQ==
]
T 07/06/2022 10:26:19:302 y2031211 doing| still have active session but no data ready at this time; waiting...
T 07/06/2022 10:26:19:548 y2031211 done| creation
T 07/06/2022 10:26:19:549 y2031211 done| connect
T 07/06/2022 10:26:19:549 y2031211 done| connection succeeded to myURIxxxxx.como:443
...
the above is an excerpt of a txt
logfile and I wanted just to read out the base64
contents of the parameters
. the file was too large and contained so long lines but I presented that as sample.
so far I did this:
with open('mytext.txt', 'r') as handle:
lines = handle.readlines()
for line in lines:
if "parameters=" in line:
print("\n")
print(line[line.index('parameters=') 11:])
print("\n")
but this only outputs
bl64lI2hMI8/u OUQfYmUw83/0lJ/2s2sGaEl2ycy/mtiIf6nC9G5I2OwDHw
but what I'm trying to do is that to get the entire base64
contents as an output as like this:
bl64lI2hMI8/u OUQfYmUw83/0lJ/2s2sGaEl2ycy/mtiIf6nC9G5I2OwDHw
lh/Ds22ScsXdugLeLL0sqq5KSZyRaScv/Nk3/sWUSPEzDDibmXEW3t/qGJ39
k5qIBypH0KS8EcSnPg/CPHgH44Uzi1VBZZwai4TUj26IOWOYKD7HpasZXf1k
5hz8WQfxeC2iz9RFV27GEabyzgu9 OK3c9ffVjplO5QFo10nCefarXcLlbiT
sBKpeyk9jQ9/3rh UM83ZVzAegzsmgQ4tLfkmV2ou6Kl7gNm9w28DGp8Mnf
ZXFTw4REddcZA2jMxQOIHE92wYSy5bDVfoIADnUqSQ==
CodePudding user response:
I would use regex to split by the actual log lines, and clean up the text.
import re
import base64
with open('content', 'r') as f:
content = f.read()
lines = re.split(r'I [0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]:[0-9][0-9][0-9]', content)
for line in lines:
if 'BODY = ' in line:
print('body detected')
tmp_split = line.split('BODY = ')
tmp_split = tmp_split[-1] # select last element
tmp_split = tmp_split.replace('\n', '').replace('\r\n', '') # delete new lines
tmp_split = tmp_split[1:-1] # remove starting and ending bracket
base64_str = tmp_split.replace('parameters=', '')
print(f'BASE64:\n{base64_str}\n')
decoded_bytes = base64.b64decode(base64_str)