| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
"""Telnet-based shell.""" |
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
from twisted.protocols import telnet |
|---|
| 9 |
from twisted.internet import protocol |
|---|
| 10 |
from twisted.python import log, failure |
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
import string, copy, sys |
|---|
| 14 |
from cStringIO import StringIO |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
class Shell(telnet.Telnet): |
|---|
| 18 |
"""A Python command-line shell.""" |
|---|
| 19 |
|
|---|
| 20 |
def connectionMade(self): |
|---|
| 21 |
telnet.Telnet.connectionMade(self) |
|---|
| 22 |
self.lineBuffer = [] |
|---|
| 23 |
|
|---|
| 24 |
def loggedIn(self): |
|---|
| 25 |
self.transport.write(">>> ") |
|---|
| 26 |
|
|---|
| 27 |
def checkUserAndPass(self, username, password): |
|---|
| 28 |
return ((self.factory.username == username) and (password == self.factory.password)) |
|---|
| 29 |
|
|---|
| 30 |
def write(self, data): |
|---|
| 31 |
"""Write some data to the transport. |
|---|
| 32 |
""" |
|---|
| 33 |
self.transport.write(data) |
|---|
| 34 |
|
|---|
| 35 |
def telnet_Command(self, cmd): |
|---|
| 36 |
if self.lineBuffer: |
|---|
| 37 |
if not cmd: |
|---|
| 38 |
cmd = string.join(self.lineBuffer, '\n') + '\n\n\n' |
|---|
| 39 |
self.doCommand(cmd) |
|---|
| 40 |
self.lineBuffer = [] |
|---|
| 41 |
return "Command" |
|---|
| 42 |
else: |
|---|
| 43 |
self.lineBuffer.append(cmd) |
|---|
| 44 |
self.transport.write("... ") |
|---|
| 45 |
return "Command" |
|---|
| 46 |
else: |
|---|
| 47 |
self.doCommand(cmd) |
|---|
| 48 |
return "Command" |
|---|
| 49 |
|
|---|
| 50 |
def doCommand(self, cmd): |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
fn = '$telnet$' |
|---|
| 55 |
result = None |
|---|
| 56 |
try: |
|---|
| 57 |
out = sys.stdout |
|---|
| 58 |
sys.stdout = self |
|---|
| 59 |
try: |
|---|
| 60 |
code = compile(cmd,fn,'eval') |
|---|
| 61 |
result = eval(code, self.factory.namespace) |
|---|
| 62 |
except: |
|---|
| 63 |
try: |
|---|
| 64 |
code = compile(cmd, fn, 'exec') |
|---|
| 65 |
exec code in self.factory.namespace |
|---|
| 66 |
except SyntaxError, e: |
|---|
| 67 |
if not self.lineBuffer and str(e)[:14] == "unexpected EOF": |
|---|
| 68 |
self.lineBuffer.append(cmd) |
|---|
| 69 |
self.transport.write("... ") |
|---|
| 70 |
return |
|---|
| 71 |
else: |
|---|
| 72 |
failure.Failure().printTraceback(file=self) |
|---|
| 73 |
log.deferr() |
|---|
| 74 |
self.write('\r\n>>> ') |
|---|
| 75 |
return |
|---|
| 76 |
except: |
|---|
| 77 |
io = StringIO() |
|---|
| 78 |
failure.Failure().printTraceback(file=self) |
|---|
| 79 |
log.deferr() |
|---|
| 80 |
self.write('\r\n>>> ') |
|---|
| 81 |
return |
|---|
| 82 |
finally: |
|---|
| 83 |
sys.stdout = out |
|---|
| 84 |
|
|---|
| 85 |
self.factory.namespace['_'] = result |
|---|
| 86 |
if result is not None: |
|---|
| 87 |
self.transport.write(repr(result)) |
|---|
| 88 |
self.transport.write('\r\n') |
|---|
| 89 |
self.transport.write(">>> ") |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
class ShellFactory(protocol.Factory): |
|---|
| 94 |
username = "admin" |
|---|
| 95 |
password = "admin" |
|---|
| 96 |
protocol = Shell |
|---|
| 97 |
service = None |
|---|
| 98 |
|
|---|
| 99 |
def __init__(self): |
|---|
| 100 |
self.namespace = { |
|---|
| 101 |
'factory': self, |
|---|
| 102 |
'service': None, |
|---|
| 103 |
'_': None |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
def setService(self, service): |
|---|
| 107 |
self.namespace['service'] = self.service = service |
|---|
| 108 |
|
|---|
| 109 |
def __getstate__(self): |
|---|
| 110 |
"""This returns the persistent state of this shell factory. |
|---|
| 111 |
""" |
|---|
| 112 |
dict = self.__dict__ |
|---|
| 113 |
ns = copy.copy(dict['namespace']) |
|---|
| 114 |
dict['namespace'] = ns |
|---|
| 115 |
if ns.has_key('__builtins__'): |
|---|
| 116 |
del ns['__builtins__'] |
|---|
| 117 |
return dict |
|---|