Use the "with" keyword when dealing with file objects
It is good practice to use the "with" keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally blocks
This commit is contained in:
@@ -45,9 +45,8 @@ class Wifite(object):
|
|||||||
if not os.path.exists(name):
|
if not os.path.exists(name):
|
||||||
Color.pl('{!} {O}file {C}%s{O} not found{W}' % name)
|
Color.pl('{!} {O}file {C}%s{O} not found{W}' % name)
|
||||||
return
|
return
|
||||||
f = open(name, 'r')
|
with open(name, 'r') as fid:
|
||||||
json = loads(f.read())
|
json = loads(fid.read())
|
||||||
f.close()
|
|
||||||
for (index, item) in enumerate(json):
|
for (index, item) in enumerate(json):
|
||||||
Color.pl('\n{+} Cracked target #%d:' % (index + 1))
|
Color.pl('\n{+} Cracked target #%d:' % (index + 1))
|
||||||
cr = CrackResult.load(item)
|
cr = CrackResult.load(item)
|
||||||
|
|||||||
@@ -39,10 +39,8 @@ class Aircrack(object):
|
|||||||
def get_key_hex_ascii(self):
|
def get_key_hex_ascii(self):
|
||||||
if not self.is_cracked():
|
if not self.is_cracked():
|
||||||
raise Exception('Cracked file not found')
|
raise Exception('Cracked file not found')
|
||||||
f = open(self.cracked_file, 'r')
|
with open(self.cracked_file, 'r') as fid:
|
||||||
hex_raw = f.read()
|
hex_raw = fid.read()
|
||||||
f.close()
|
|
||||||
|
|
||||||
hex_key = ''
|
hex_key = ''
|
||||||
ascii_key = ''
|
ascii_key = ''
|
||||||
while len(hex_raw) > 0:
|
while len(hex_raw) > 0:
|
||||||
|
|||||||
@@ -96,14 +96,11 @@ class Aireplay(Thread):
|
|||||||
while self.pid.poll() is None:
|
while self.pid.poll() is None:
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
if not os.path.exists(self.output_file): continue
|
if not os.path.exists(self.output_file): continue
|
||||||
# Read output file
|
# Read output file & clear output file
|
||||||
f = open(self.output_file, "r")
|
with open(self.output_file, "r+") as fid:
|
||||||
lines = f.read()
|
lines = fid.read()
|
||||||
f.close()
|
fid.seek(0)
|
||||||
# Clear output file
|
fid.truncate()
|
||||||
f = open(self.output_file, "w")
|
|
||||||
f.write("")
|
|
||||||
f.close()
|
|
||||||
for line in lines.split("\n"):
|
for line in lines.split("\n"):
|
||||||
line = line.replace("\r", "").strip()
|
line = line.replace("\r", "").strip()
|
||||||
if line == "": continue
|
if line == "": continue
|
||||||
|
|||||||
@@ -189,9 +189,8 @@ class AttackWPA(Attack):
|
|||||||
Color.pl("")
|
Color.pl("")
|
||||||
# Check crack result
|
# Check crack result
|
||||||
if os.path.exists(key_file):
|
if os.path.exists(key_file):
|
||||||
f = open(key_file, "r")
|
with open(key_file, "r") as fid:
|
||||||
key = f.read().strip()
|
key = fid.read().strip()
|
||||||
f.close()
|
|
||||||
os.remove(key_file)
|
os.remove(key_file)
|
||||||
|
|
||||||
Color.pl("{+} {G}Cracked WPA Handshake{W} PSK: {G}%s{W}\n" % key)
|
Color.pl("{+} {G}Cracked WPA Handshake{W} PSK: {G}%s{W}\n" % key)
|
||||||
|
|||||||
@@ -27,17 +27,15 @@ class CrackResult(object):
|
|||||||
name = CrackResult.cracked_file
|
name = CrackResult.cracked_file
|
||||||
json = []
|
json = []
|
||||||
if os.path.exists(name):
|
if os.path.exists(name):
|
||||||
f = open(name, 'r')
|
with open(name, 'r') as fid:
|
||||||
text = f.read()
|
text = fid.read()
|
||||||
f.close()
|
|
||||||
try:
|
try:
|
||||||
json = loads(text)
|
json = loads(text)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
Color.pl('{!} error while loading %s: %s' % (name, str(e)))
|
Color.pl('{!} error while loading %s: %s' % (name, str(e)))
|
||||||
json.append(self.to_dict())
|
json.append(self.to_dict())
|
||||||
f = open(name, 'w')
|
with open(name, 'w') as fid:
|
||||||
f.write(dumps(json, indent=2))
|
fid.write(dumps(json, indent=2))
|
||||||
f.close()
|
|
||||||
Color.pl('{+} saved crack result to {C}%s{W} ({G}%d total{W})'
|
Color.pl('{+} saved crack result to {C}%s{W} ({G}%d total{W})'
|
||||||
% (name, len(json)))
|
% (name, len(json)))
|
||||||
|
|
||||||
|
|||||||
11
py/Reaver.py
11
py/Reaver.py
@@ -240,10 +240,8 @@ class Reaver(Attack):
|
|||||||
out = self.get_stdout()
|
out = self.get_stdout()
|
||||||
|
|
||||||
# Clear output file
|
# Clear output file
|
||||||
f = open(self.stdout_file, 'w')
|
with open(self.stdout_file, 'w'):
|
||||||
f.write('')
|
pass
|
||||||
f.close()
|
|
||||||
|
|
||||||
# CHECK FOR CRACK
|
# CHECK FOR CRACK
|
||||||
|
|
||||||
(pin, psk, ssid) = Reaver.get_pin_psk_ssid(out)
|
(pin, psk, ssid) = Reaver.get_pin_psk_ssid(out)
|
||||||
@@ -394,9 +392,8 @@ class Reaver(Attack):
|
|||||||
''' Gets output from stdout_file '''
|
''' Gets output from stdout_file '''
|
||||||
if not self.stdout_file:
|
if not self.stdout_file:
|
||||||
return ''
|
return ''
|
||||||
f = open(self.stdout_file, 'r')
|
with open(self.stdout_file, 'r') as fid:
|
||||||
stdout = f.read()
|
stdout = fid.read()
|
||||||
f.close()
|
|
||||||
return stdout.strip()
|
return stdout.strip()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user