Created
February 8, 2013 16:07
-
-
Save sporsh/4739983 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from twisted.internet.protocol import Protocol | |
import re | |
class ShellProtocol(Protocol): | |
"""Define a generic shell-like protocol: | |
[login-prompt [input-echo]] | |
[password-prompt] [input-echo]] | |
[greeting] | |
[prompt] | |
[input-echo] | |
<[output]...> | |
<prompt> | |
""" | |
prompts = {'(?:[L|l]ogin|[U|u]ser):\s+$': 'username', | |
'[P|p]assword:\s+$': 'password', | |
} | |
def __init__(self): | |
self._buffer = '' | |
def dataReceived(self, data): | |
self._buffer += data | |
re.search(self._re_prompt, self._buffer) | |
mo = self.re_prompt.search(self._buffer) | |
if mo: | |
response, self._buffer = self._buffer[:mo.start()], self._buffer[mo.end():] | |
output = mo.groups('output') | |
prompt = mo.groups('prompt') | |
self._buffer = self._buffer[mo.end():] | |
self.responseReceived(response) | |
self.dispatchPrompt(prompt, output) | |
def responseReceived(self, response): | |
pass | |
def dispatchPrompt(self, prompt, output): | |
if | |
def shell_USER(self, message): | |
pass | |
def shell_PASSWORD(self, message): | |
pass | |
def shell_DEFAULT(self, message): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment