wip shlex

This commit is contained in:
Michel Oosterhof
2015-10-17 08:00:08 +00:00
parent 8b376b86c7
commit 09185e63cf
2 changed files with 21 additions and 10 deletions

View File

@ -124,22 +124,31 @@ class HoneyPotShell(object):
self.interactive = interactive self.interactive = interactive
self.cmdpending = [] self.cmdpending = []
self.environ = protocol.environ self.environ = protocol.environ
#self.lexer.debug = 1
self.showPrompt() self.showPrompt()
def lineReceived(self, line): def lineReceived(self, line):
""" """
""" """
log.msg('CMD: %s' % (line,)) log.msg('CMD: %s' % (line,))
line = line[:500] self.lexer = shlex.shlex(punctuation_chars=True);
comment = re.compile('^\s*#') self.lexer.push_source(line)
for i in [x.strip() for x in re.split(';|&&|\n', line.strip())[:10]]: tokens = []
if not len(i): while True:
tok = self.lexer.get_token()
log.msg( "tok: %s" % (repr(tok)) )
# for now, execute all after &&
if tok == ';' or tok == self.lexer.eof or tok == '&&':
self.cmdpending.append((tokens))
tokens = []
if tok == ';':
continue continue
if comment.match(i): if tok == '&&':
continue continue
self.cmdpending.append(i) if tok == self.lexer.eof:
break
tokens.append(tok)
if len(self.cmdpending): if len(self.cmdpending):
self.runCommand() self.runCommand()
else: else:

View File

@ -13,7 +13,7 @@ import re
import sys import sys
from collections import deque from collections import deque
from io import StringIO from io import StringIO, BytesIO
__all__ = ["shlex", "split", "quote"] __all__ = ["shlex", "split", "quote"]
@ -22,7 +22,8 @@ class shlex:
def __init__(self, instream=None, infile=None, posix=False, def __init__(self, instream=None, infile=None, posix=False,
punctuation_chars=False): punctuation_chars=False):
if isinstance(instream, str): if isinstance(instream, str):
instream = StringIO(instream) #instream = StringIO(instream)
instream = BytesIO(instream)
if instream is not None: if instream is not None:
self.instream = instream self.instream = instream
self.infile = infile self.infile = infile
@ -81,7 +82,8 @@ class shlex:
def push_source(self, newstream, newfile=None): def push_source(self, newstream, newfile=None):
"Push an input source onto the lexer's input source stack." "Push an input source onto the lexer's input source stack."
if isinstance(newstream, str): if isinstance(newstream, str):
newstream = StringIO(newstream) #newstream = StringIO(newstream)
newstream = BytesIO(newstream)
self.filestack.appendleft((self.infile, self.instream, self.lineno)) self.filestack.appendleft((self.infile, self.instream, self.lineno))
self.infile = newfile self.infile = newfile
self.instream = newstream self.instream = newstream