fixing bug with replace mode

This commit is contained in:
brent s 2017-11-08 23:12:36 -05:00
parent 405bf79d56
commit 731609f689

View File

@ -101,6 +101,9 @@ class hostscanner(object):
return() return()


def write(self): def write(self):
if self.args['writemode'] == 'replace':
if os.path.isfile(self.args['output']) and self.args['output'] != sys.stdout:
os.move(self.args['output'], os.path.join(self.args['output'], '.bak'))
for h in self.keys.keys(): for h in self.keys.keys():
for i in self.keys[h]: for i in self.keys[h]:
_s = '# Automatically added via hostscan.py\n{0} {1} {2}\n'.format(i['host'], _s = '# Automatically added via hostscan.py\n{0} {1} {2}\n'.format(i['host'],
@ -109,11 +112,7 @@ class hostscanner(object):
if self.args['output'] == sys.stdout: if self.args['output'] == sys.stdout:
print(_s, end = '') print(_s, end = '')
else: else:
if self.args['writemode'] == 'append': with open(self.args['output'], 'a') as f:
_wm = 'a'
else:
_wm = 'w'
with open(self.args['output'], _wm) as f:
f.write(_s) f.write(_s)
os.chmod(self.args['output'], 0o644) os.chmod(self.args['output'], 0o644)
os.chown(self.args['output'], os.chown(self.args['output'],