I am making some sort of script which would return lines from log file. Having issue with printing out lines which does not suite requested conditions.
Leaving example of log file:
2021-05-14 04:16:22,164 instanceA 10.0.0.1 <request>requestID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:22,215 instanceA 10.0.0.1 <response>responseID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,311 instanceA 10.0.0.1 <request>requestID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,312 instanceA 10.0.0.1 <response>responseID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,620 instanceA 10.0.0.1 <request>requestID = 'r#qwsrdq'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,750 instanceA 10.0.0.1 <response>responseID = 'r#qwsrdq'<identifier>ID_valueA</identifier><objectError>Error_Data</objectError>
&QADQ;requestID = &Qot;'r#qwsrdq'/version=.../object=.../...
END OF QUERY <response><info><error></error></info></response>
2021-05-14 05:23:11,140 instanceA 10.0.0.1 <request>requestID = '1234xz987'<identifier>ID_valueB</identifier><object>Data</object>
2021-05-14 05:23:11,431 instanceA 10.0.0.1 <response>responseID = '1234xz987'<identifier>ID_valueB</identifier><object>Data</object>
2021-05-14 06:34:12,266 instanceA - WEB_GUI 10.0.0.1 <request>requestID = '1234xz987'
<identifier>ID_valueA</identifier>
<object>Data</object>
<object>Data</object>
<object>Data</object>
<value>Data</value>
2021-05-14 06:34:12,315 instanceA - WEB_GUI 10.0.0.1 <response>responseID = '1234xz987'
So basic idea is to get lines with ID_valueA, but also I need all the other data blocks which are connected with ID_valueA. So expected result would be:
2021-05-14 04:16:22,164 instanceA 10.0.0.1 <request>requestID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:22,215 instanceA 10.0.0.1 <response>responseID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,311 instanceA 10.0.0.1 <request>requestID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,312 instanceA 10.0.0.1 <response>responseID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,620 instanceA 10.0.0.1 <request>requestID = 'r#qwsrdq'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,750 instanceA 10.0.0.1 <response>responseID = 'r#qwsrdq'<identifier>ID_valueA</identifier><objectError>Error_Data</objectError>
&QADQ;requestID = &Qot;'r#qwsrdq'/version=.../object=.../...
END OF QUERY <response><info><error></error></info></response>
2021-05-14 06:34:12,266 instanceA - WEB_GUI 10.0.0.1 <request>requestID = '12355557'
<identifier>ID_valueA</identifier>
<object>Data</object>
<object>Data</object>
<object>Data</object>
<value>Data</value>
2021-05-14 06:34:12,315 instanceA - WEB_GUI 10.0.0.1 <response>responseID = '12355557'
For now I have managed to result:
2021-05-14 04:16:22,164 instanceA 10.0.0.1 <request>requestID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:22,215 instanceA 10.0.0.1 <response>responseID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,311 instanceA 10.0.0.1 <request>requestID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,312 instanceA 10.0.0.1 <response>responseID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,620 instanceA 10.0.0.1 <request>requestID = 'r#qwsrdq'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,750 instanceA 10.0.0.1 <response>responseID = 'r#qwsrdq'<identifier>ID_valueA</identifier><objectError>Error_Data</objectError>
2021-05-14 06:34:12,266 instanceA - WEB_GUI 10.0.0.1 <request>requestID = '12355557'
<identifier>ID_valueA</identifier>
<object>Data</object>
<object>Data</object>
<object>Data</object>
<value>Data</value>
2021-05-14 06:34:12,315 instanceA - WEB_GUI 10.0.0.1 <response>responseID = '12355557'
**
&QADQ;requestID = &Qot;'r#qwsrdq'/version=.../object=.../...
END OF QUERY <response><info><error></error></info></response>
**
^
MISSING
I have used below code:
from file_manager import open_log_file
request_id = re.compile(r"requestID=\"[0-9a-zA-Z:-]+\"")
list_of_input_values = []
list_of_output_values = []
filename = []
id_list= []
case1="typeofRequest1"
case2="typeofResponse1"
case3="typeofRequest2"
case4="typeofResponse2"
case5="typeofRequest3"
case6="typeofResponse3"
status = True
while status == True:
used = input('>>> ')
if used.lower() == 'stop':
break
if used.lower() == 'ID_parameter1':
ID_parameter1 = input('Please input ID_parameter1: ')
list_of_input_values.insert(0,ID_parameter1)
continue
if used.lower() == 'ID_parameter2':
ID_parameter2 = input('Please input ID_parameter2: ')
list_of_input_values.insert(1,ID_parameter2)
continue
if used.lower() == 'ID_parameter3':
ID_parameter3 = input('Please input ID_parameter3: ')
list_of_input_values.insert(2,ID_parameter3)
continue
if used.lower() == '':
continue
if used.lower() == 'open':
filename = open_log_file()
for element in filename:
with open(element) as log:
for line in log:
for val in list_of_input_values:
if val in line:
result = request_id.findall(line)
for i in result:
id_list.append(i)
for element in filename:
with open(element) as log:
for line in log:
for i in id_list:
if i in line:
list_of_output_values.append(line)
for line in log:
if i in line:
list_of_output_values.append(line)
else:
if line.__contains__(case1 or case2 or case3 or case4 or case5 or case6 or case7 or case8):
break
else:
list_of_output_values.append(line)
print(list_of_output_values, file=open(r'output\output.txt', "a"))
from file_manager import open_log_file :
def open_log_file():
import tkinter as tk
from tkinter import filedialog
import fileinput
unused_value = ">>> Unexpected input value: None. "
root = tk.Tk()
root.withdraw()
root.attributes('-topmost', True)
root.update()
root.wm_iconbitmap(r'resources\myicon.ico')
filename = filedialog.askopenfilename(multiple=True)
filename = root.tk.splitlist(filename)
filePath = []
for f in filename:
filePath.append(f)
if filename == ():
print(unused_value)
return filePath
Can you please look around to understand what is wrong. I am expecting current loop to work well, but it seems not working that good. Searching is based on request ID parameter, because not every line contains ID_value specific parameter. Thanks in advice.
Morning guys.
Can not explain why, maybe after taking a look of solution someone would give some decisions.
I have used:
print(list_of_output_values, file=open(r'output\output.txt', "a"))
At the end of code. Which gave me following output:
2021-05-14 04:16:22,164 instanceA 10.0.0.1 <request>requestID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:22,215 instanceA 10.0.0.1 <response>responseID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,311 instanceA 10.0.0.1 <request>requestID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,312 instanceA 10.0.0.1 <response>responseID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,620 instanceA 10.0.0.1 <request>requestID = 'r#qwsrdq'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,750 instanceA 10.0.0.1 <response>responseID = 'r#qwsrdq'<identifier>ID_valueA</identifier><objectError>Error_Data</objectError>
2021-05-14 06:34:12,266 instanceA - WEB_GUI 10.0.0.1 <request>requestID = '12355557'
<identifier>ID_valueA</identifier>
<object>Data</object>
<object>Data</object>
<object>Data</object>
<value>Data</value>
2021-05-14 06:34:12,315 instanceA - WEB_GUI 10.0.0.1 <response>responseID = '12355557'
Now scrolling down the code, I have decided to change print() function to write() block for clearer output. and using:
with open(r'output\output.txt', "a") as file:
for i in list_of_output_values:
file.write(i)
And now I am getting:
2021-05-14 04:16:22,164 instanceA 10.0.0.1 <request>requestID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:22,215 instanceA 10.0.0.1 <response>responseID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,311 instanceA 10.0.0.1 <request>requestID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,312 instanceA 10.0.0.1 <response>responseID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,620 instanceA 10.0.0.1 <request>requestID = 'r#qwsrdq'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,750 instanceA 10.0.0.1 <response>responseID = 'r#qwsrdq'<identifier>ID_valueA</identifier><objectError>Error_Data</objectError>
&QADQ;requestID = &Qot;'r#qwsrdq'/version=.../object=.../...
END OF QUERY <response><info><error></error></info></response>
2021-05-14 06:34:12,266 instanceA - WEB_GUI 10.0.0.1 <request>requestID = '12355557'
<identifier>ID_valueA</identifier>
<object>Data</object>
<object>Data</object>
<object>Data</object>
<value>Data</value>
2021-05-14 06:34:12,315 instanceA - WEB_GUI 10.0.0.1 <response>responseID = '12355557'
Related
This question already has answers here:
Pretty printing XML in Python
(27 answers)
Closed 1 year ago.
I am trying to format output file using XML structure. Tried xml and lxml but no luck for now.
Currently output file look like:
<root><time>2021-07-28 10:27:49,869 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_request id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest>
<time>2021-07-28 10:27:49,881 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_response id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest>
<time>2021-07-28 10:27:51,834 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_request id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest>
<time>2021-07-28 10:27:51,854 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_response id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest>
<time>2021-07-28 10:27:52,182 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_request id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest>
<time>2021-07-28 10:27:52,201 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_response id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest></root>
Using this code right here:
import lxml.etree as etree
xml_input = etree.parse(r'output\output.txt')
print(etree.tostring(xml_input, pretty_print=True),file = open(r'output\output.txt','a'))
file.close()
How can I improve code for this kind of input? What is done wrong? Thanks everyone for any advice.
not long ago have asked question and found the solution.
I have taken code from How to pretty print XML from the command line?
Where xml.dom.minidom is used.
import xml.dom.minidom
with open(r'output\output.txt', 'r+') as file:
s = file.read()
pretty = xml.dom.minidom.parseString(s).toprettyxml()
with open(r'output\output.txt', 'w') as writer:
writer.write(pretty)
Now output looks like expected.
Trying to visualize the result of various sensor data in python
I can see it on raspberry pi terminal like a linux command python Sensortag2650.py -n 5 -t 1 -T -A -H -M -B -G -K -L -P 54:6C:0E:52:F8:FC
now i want to see the same result on python shell with same code
def main():
import time
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('host', action='store',help='MAC of BT device')
parser.add_argument('-n', action='store', dest='count', default=0,
type=int, help="Number of times to loop data")
parser.add_argument('-t',action='store',type=float, default=5.0, help='time between polling')
parser.add_argument('-T','--temperature', action="store_true",default=False)
parser.add_argument('-A','--accelerometer', action='store_true',
default=False)
parser.add_argument('-H','--humidity', action='store_true', default=False)
parser.add_argument('-M','--magnetometer', action='store_true',
default=False)
parser.add_argument('-B','--barometer', action='store_true', default=False)
parser.add_argument('-G','--gyroscope', action='store_true', default=False)
parser.add_argument('-K','--keypress', action='store_true', default=False)
parser.add_argument('-L','--light', action='store_true', default=False)
parser.add_argument('-P','--battery', action='store_true', default=False)
parser.add_argument('--all', action='store_true', default=False)
arg = parser.parse_args(sys.argv[1:])
print('Connecting to ' + arg.host)
tag = SensorTag(arg.host)
Output :
Sensortag2650.py [-h] [-n COUNT] [-t T] [-T] [-A] [-H] [-M] [-B] [-G]
[-K] [-L] [-P] [--all]
host
Sensortag2650.py: `error: the following arguments are required: host`
the below while loop stops when runs the except IOError
SO i get as output:
IP: 192.168.1.1 ,PORT: 80 ,Connection established
IP: 192.168.1.1 ,PORT: 22 ,Connection established
IP: 192.168.1.1 ,PORT: 22 ,Connection established
IP: 192.168.1.1 ,PORT: 22 ,Connection established
IP: 192.168.1.97 ,PORT: 22 ,Cannot Connect (stops here)
Any help would be great. I can't understand why it runs the except IOError: and then stops
Thank you
while True:
f = input('Type the File Name/Path:')
if f == '': break
try:
with open(f, 'r', encoding='utf-8') as f:
for check in f:
check = check.split("\t")
HOSTNAME = check[0]
IP = check[1]
PORT = int(check[2])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(TIMEOUT)
s.connect((IP, PORT))
f = open("output.txt", "a")
print('IP:', IP, ',' 'PORT:', PORT, ',' "Connection established", file=f)
time.sleep(1)
f.close()
s.close()
except IOError:
f = open("output.txt", "a")
print('IP:', IP,',' 'PORT:', PORT,',' "Cannot Connect", file=f)
f.close()
except FileNotFoundError:
print("\n")
print('The file {} does not exist'.format(f))
input('Press ENTER to continue...')
print("\n")
break
if the IO Error is caused by the socket call, I think your try/except statement has to be changed a bit. You could try:
while True:
file = input('Type the File Name/Path:')
if file == '':
break
try:
with open(file, 'r', encoding='utf-8') as f:
for check in f:
check = check.split("\t")
HOSTNAME = check[0]
IP = check[1]
PORT = int(check[2])
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(TIMEOUT)
s.connect((IP, PORT))
s.close()
except IOError:
log = open("output.txt", "a")
print('IP:', IP,',' 'PORT:', PORT,',' "Cannot Connect", file=log)
log.close()
continue # will go back to the beginning of the while loop
log = open("output.txt", "a")
print('IP:', IP, ',' 'PORT:', PORT, ',' "Connection established", file=log)
log.close()
time.sleep(1)
except FileNotFoundError:
print('\nThe file {} does not exist'.format(file))
input('Press any key to continue...\n')
break
I am trying to run client serevr socket but ia m getting the below error
client code
enter code here
#!/usr/bin/env python3
import socket
HOST = '127.0.0.1' # The server's hostname or IP address
PORT = 65432 # The port used by the server
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
d=input("enter numbers")
f=input("enter numbers")
val = str(d) # sep = ' ' or sep = `\n`
s.send(val)
val1 = str(f) # sep = ' ' or sep = `\n`
s.send(val1)
#s.send(d)
#s.send(f)
data = s.recv(1024)
data5=int(data)
print('Received', repr(data5))
s.close()
server
#!/usr/bin/env python3
import socket
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(PORT)
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
w1 = conn.recv(1024)
w2= conn.recv(1024)
#w1=int(w[0])
#w2=int(w[1])
data3=w1-w2
data3=str(data3)
if not data:
break
conn.sendall(data3)
conn.close()
error-socket.error: [Errno 32] Broken pipe
I am facing this error,can we resolve this , i guess here we have an issue with tcp connection closed too soon.
There were packets captured by Wireshark sent by a fuzzing tool, and I'd like to craft to resend the packets in tls by scapy.
Here is the malformed packet captured. The pattern is \x18\x03\x01\x00\x01\x7f.
The structure of the packet disassembled by scapy is as follows:
>>> rdpcap("659726.pcap")[146].show()
###[ Ethernet ]###
dst= aa:aa:aa:aa:aa:aa
src= 00:50:56:bb:64:01
type= 0x800
###[ IP ]###
version= 4
ihl= 5
tos= 0x0
len= 46
id= 4211
flags= DF
frag= 0
ttl= 128
proto= tcp
chksum= 0x0
src= 192.168.40.214
dst= 192.168.40.32
\options\
###[ TCP ]###
sport= 61385
dport= https
seq= 351671141
ack= 2538369473
dataofs= 5
reserved= 0
flags= PA
window= 508
chksum= 0xd267
urgptr= 0
options= []
###[ Raw ]###
load= '\x18\x03\x01\x00\x01\x7f'
The following script, which I modified from /examples/full_rsa_connection_with_application_data.py is to resend the packets. It contains 2 steps: SSL Handshake and a malformed packet.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import with_statement
from __future__ import print_function
from scapy.all import *
try:
# This import works from the project directory
from scapy_ssl_tls.ssl_tls import *
except ImportError:
# If you installed this package via pip, you just need to execute this
from scapy.layers.ssl_tls import *
tls_version = TLSVersion.TLS_1_0
ciphers = [TLSCipherSuite.ECDHE_RSA_WITH_AES_128_GCM_SHA256]
# ciphers = [TLSCipherSuite.ECDHE_RSA_WITH_AES_256_CBC_SHA384]
# ciphers = [TLSCipherSuite.RSA_WITH_AES_128_CBC_SHA]
ciphers = [TLSCipherSuite.RSA_WITH_RC4_128_SHA]
# ciphers = [TLSCipherSuite.DHE_RSA_WITH_AES_128_CBC_SHA]
# ciphers = [TLSCipherSuite.DHE_DSS_WITH_AES_128_CBC_SHA]
extensions = [TLSExtension() / TLSExtECPointsFormat(),
TLSExtension() / TLSExtSupportedGroups()]
def tls_client(ip):
with TLSSocket(client=True) as tls_socket:
try:
tls_socket.connect(ip)
print("Connected to server: %s" % (ip,))
except socket.timeout:
print("Failed to open connection to server: %s" % (ip,), file=sys.stderr)
else:
try:
server_hello, server_kex = tls_socket.do_handshake(tls_version, ciphers, extensions)
server_hello.show()
malformedPacket = Raw('\x18\x03\x01\x00\x01\x7f') #or malformedPacket = IP(dst="192.168.40.32")/TCP(dport=443)/raw('\x18\x03\x01\x00\x01\x7f')
respMalformedPacket = tls_socket.do_round_trip(malformedPacket)
print("response from malformed packet: ==========================================")
respMalformedPacket.show()
print("end of response from malformed packet: ==========================================")
except TLSProtocolError as tpe:
print("Got TLS error: %s" % tpe, file=sys.stderr)
tpe.response.show()
else:
resp = tls_socket.do_round_trip(TLSPlaintext(data="GET / HTTP/1.1\r\nHOST: localhost\r\n\r\n"))
print("Got response from server")
resp.show()
finally:
print(tls_socket.tls_ctx)
if __name__ == "__main__":
if len(sys.argv) > 2:
server = (sys.argv[1], int(sys.argv[2]))
else:
server = ("127.0.0.1", 8443)
tls_client(server)
I just want to send the pattern'\x18\x03\x01\x00\x01\x7f' to the target after SSL Handshake. SSL Handshake is done by tls_socket.do_handshake(). Then I want to send the malformed packet I crafted by adding tls_socket.do_round_trip(malformedPacket) after ChangeCipherSpec as the original packet captured. However, it failed to send successfully, and here is the result. I have no idea of how to send the raw data in tls correctly. I need some suggestion. Thanks.