ファイル内のいくつかの文字列を見つけようとしていますが、ファイル内のどの文字列に対しても line.find() が true を返しません。見つかったすべての文字列の値。次の文字列の検索は、そのオフセットから開始する必要があります。
def CheckFile(*argv):
import os
Filename = argv[0]
Search = argv[1]
Flag = False
FileFlag = False
offset1 = 0
offset2 = 0
if os.path.exists(Filename) == 0:
return "File Doesn't exist", 1
else:
fh = open(Filename,"r")
for line in fh:
if Search in line:
print "Success"
print (line)
Flag = True
offset1 = fh.tell()
#offset1 = int(offset1)
break
else:
fh.close()
return "Could not find String %s"%(Search), 1
#fh.close()
if Flag:
fh = open(Filename,"r")
print(offset1)
fh.seek(offset1)
for line in fh:
if "TestDir1\TestFile1.txt" in line:
print "Success"
print (line)
FileFlag = True
offset2 = fh.tell()
#offset2 = int(offset2)
break
else:
fh.close()
return "Couldn't Find File TestDir1\TestFile1.txt", 1
#fh.close()
if Flag and FileFlag:
fh = open(Filename,"r")
print(offset2)
fh.seek(offset2)
for line in fh:
if "Persistent Handle: True" in line:
print "Success"
return "Success -- Found the strings", 0
else:
fh.close()
return "Failur -- Failed to find 'Persistent Handle: True'", 1
出力:
>>> CheckFile("D:\wireshark.txt","NetBIOS")
('Could not find String NetBIOS', 1)
サンプルファイルは次のとおりです。
> [SEQ/ACK analysis]
> [This is an ACK to the segment in frame: 104]
> [The RTT to ACK the segment was: 0.043579000 seconds]
> [Bytes in flight: 252]
>NetBIOS Session Service
> Message Type: Session message (0x00)
> Length: 248
>SMB2 (Server Message Block Protocol version 2)
> SMB2 Header
> Server Component: SMB2
> Header Length: 64
> Credit Charge: 1
> Channel Sequence: 0
Reserved: 0000
Command: Create (5)
Credits requested: 1
Flags: 0x00000000