Binary cat (#1382)

* output cat as bytes
* make grep bytes compatible
This commit is contained in:
Michel Oosterhof 2020-07-18 22:01:18 +08:00 committed by GitHub
parent 5c51342c03
commit 4886aa443c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 17 deletions

View File

@ -62,26 +62,33 @@ class command_cat(HoneyPotCommand):
raise FileNotFound
except FileNotFound:
self.errorWrite('cat: {}: No such file or directory\n'.format(arg))
self.exit()
elif self.input_data is not None:
self.output(self.input_data)
self.exit()
def output(self, input):
"""
This is the cat output, with optional line numbering
"""
if 'decode' in dir(input):
input = input.decode('UTF-8')
if not isinstance(input, str):
pass
if input is None:
return
lines = input.split('\n')
if lines[-1] == "":
if isinstance(input, str):
input = input.encode('utf8')
elif isinstance(input, bytes):
pass
else:
log.msg("unusual cat input {}".format(repr(input)))
lines = input.split(b'\n')
if lines[-1] == b'':
lines.pop()
for line in lines:
if self.number:
self.write("{:>6} ".format(self.linenumber))
self.write('{:>6} '.format(self.linenumber))
self.linenumber = self.linenumber + 1
self.write(line + '\n')
self.writeBytes(line + b'\n')
def lineReceived(self, line):
"""

View File

@ -34,12 +34,11 @@ class command_grep(HoneyPotCommand):
self.errorWrite("grep: {}: No such file or directory\n".format(filename))
def grep_application(self, contents, match):
match = os.path.basename(match)
match = match.replace('\"', '')
match = os.path.basename(match).replace('\"', '').encode('utf8')
matches = re.compile(match)
contentsplit = contents.split(b'\n')
matches = re.compile('.*' + match + '.*')
for line in contentsplit:
if matches.match(line.decode()):
if matches.search(line):
self.writeBytes(line + b'\n')
def help(self):

View File

@ -47,12 +47,12 @@ class ShellCatCommandTests(unittest.TestCase):
def test_cat_command_003(self):
"""
test ignore stdin when called without '-'
test without arguments, read stdin only and quit
"""
self.proto.lineReceived(b'echo whatever | cat\n')
self.proto.lineReceived(b'test\n')
self.proto.lineReceived(b'echo 1 | cat\n')
self.proto.lineReceived(b'echo 2\n')
self.proto.handle_CTRL_D()
self.assertEquals(self.tr.value(), b'test\n' + PROMPT)
self.assertEquals(self.tr.value(), b'1\n' + PROMPT + b'2\n' + PROMPT)
def test_cat_command_004(self):
"""