You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.5 KiB

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import scapy.all as scapy
import scapy.contrib.modbus as mb
def decode(pkt):
prt=0
addr,value = "",""
bigL = [ ]
global miniL
f=open("truc.txt","w")
if "ModbusADU" in pkt:
grPkt=pkt["ModbusADU"].show(dump=True)
lPkt=grPkt.splitlines()
for i in lPkt:
if "###" in i:
#if addr!="" and value!="":
#print("addr=",addr,"value=",value)
prt=0
typResp=""
if "Coil" in i and "Read" in i:
prt=1
miniL[0] = "cr"
if "Coil" in i and "Write" in i:
prt=1
miniL[0]="cw"
elif "Register" in i:
prt=2
miniL[0] = "r"
#if prt==1:
#print(i)
elif "startAddr" in i or "outputAddr" in i or "registerAddr" in i:
miniL[1] = i[i.find("=")+2:]
#print(addr)
elif "coilStatus" in i or "registerVal=" in i or ("outputsValue" in i and miniL[0]=="r"):
value = i[i.find("[")+1:i.rfind("]")]
value = value.split(", ")
miniL[2] = value
#print("value= ",value," addr= ",addr)
elif "outputValue" in i or "quantityOutput" in i or "registerValue" in i:
miniL[2] = i[i.find("=")+2:]
elif "outputsValue" in i and miniL[0]=="c":
valInter=i[i.find("=")+2:]
valInter2=valInter[1:-1]
#miniL[2] = int(valInter2,16)
miniL[2]=vaInter2
if miniL.count(0)==0:
if(type(miniL[2])==list):
if miniL[0]=="r":
for i in range(len(miniL[2])):
bigL.append([miniL[0],int(miniL[1],16)+i,int(miniL[2][i],16)])
if miniL[0]=="cr":
print("hého:",miniL)
for i in range(len(miniL[2])):
byte=8*[0]
print("regarde ici",miniL[2][i])
convert=bin(int(miniL[2][i]))
for j in range(len(convert)-2):
byte[j]=convert[-(j+1)]
for j in range(8):
bigL.append(["c",int(miniL[1],16)+i*8+j,byte[j]])
if miniL[0]=="cw":
print("salut:",miniL)
bigL.append(["c",int(miniL[1],16),0])
else:
bigL.append(miniL)
print(bigL)
miniL = [0,0,0]
miniL = [0,0,0]
scapy.sniff(iface="lo", prn=decode)