Last active
December 18, 2015 17:08
-
-
Save mitchellrj/5815942 to your computer and use it in GitHub Desktop.
Basic remote Python interpreter. Connects to a remote host and presents the Python interpreter.
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
""" | |
Basic remote Python interpreter | |
=============================== | |
Connects to a remote host and presents the Python interpreter to it. | |
Example usage | |
------------- | |
On Machine A (the local host):: | |
machine-a $ nc -lv 1337 | |
On Machine B (the remote host, running the interpreter):: | |
machine-b $ python interact.py machine-a 1337 | |
Expected output on Machine A: | |
machine-a $ nc -lv 1337 | |
Remote Python interpreter running on machine-b:65024 | |
Python 3.3.1 (default, Apr 25 2013, 12:36:23) | |
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.27)] on darwin | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> _ | |
""" | |
from code import InteractiveConsole | |
import socket | |
import sys | |
banner_fmt = \ | |
"""Remote Python interpreter running on {}:{} | |
Python {} on {} | |
Type "help", "copyright", "credits" or "license" for more information. | |
""" | |
encoding = 'utf8' | |
def remote_console(host, port): | |
sock = socket.create_connection((remote_host, remote_port)) | |
hostname = socket.getfqdn() | |
local_port = sock.getsockname()[1] | |
def sock_write(data): | |
sock.send(data.encode(encoding)) | |
def sock_input(prompt): | |
sock_write(prompt) | |
data = char = b'' | |
while char != b'\n': | |
char = sock.recv(1) | |
data += char | |
return data.decode(encoding) | |
i = InteractiveConsole(None) | |
i.raw_input = sock_input | |
i.write = sock_write | |
banner = banner_fmt.format(hostname, local_port, sys.version, sys.platform) | |
i.interact(banner) | |
sockfile.close() | |
sock.close() | |
if __name__ == '__main__': | |
remote_host = sys.argv[1] | |
remote_port = int(sys.argv[2]) | |
remote_console(remote_host, remote_port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment