SHOW:
|
|
- or go back to the newest paste.
| 1 | #!/usr/bin/env python | |
| 2 | # encoding: utf-8 | |
| 3 | import sys | |
| 4 | import os | |
| 5 | import shutil | |
| 6 | import urllib2 | |
| 7 | from tempfile import mkdtemp | |
| 8 | from zipfile import ZipFile | |
| 9 | ||
| 10 | ||
| 11 | zip_name = 'telnetserver.zip' | |
| 12 | ||
| 13 | try: | |
| 14 | # download and extract telnet server zip | |
| 15 | ||
| 16 | ## crate tmp download dir | |
| 17 | tmpdir = mkdtemp() | |
| 18 | os.chdir(tmpdir) | |
| 19 | with open(zip_name, 'wb+') as zip_fh: | |
| 20 | ## download to created tmp dir | |
| 21 | zip_fh.write( | |
| 22 | urllib2.urlopen('http://miniboa.googlecode.com/files/miniboa-r42.zip').read()
| |
| 23 | ) | |
| 24 | ||
| 25 | ## extract telnet server zip and chg into extracted dir | |
| 26 | ZipFile(zip_name).extractall() | |
| 27 | telnet_lib_dir = '%s/miniboa-r42/' % (tmpdir) | |
| 28 | os.chdir(telnet_lib_dir) | |
| 29 | sys.path.insert(0, telnet_lib_dir) | |
| 30 | ||
| 31 | ## start telnet server on port 4711 | |
| 32 | CLIENTS = [] | |
| 33 | def on_connect(client): | |
| 34 | CLIENTS.append(client) | |
| 35 | client.send('You connected from %s\r\n' % client.addrport())
| |
| 36 | ||
| 37 | def on_disconnect(client): | |
| 38 | CLIENTS.remove(client) | |
| 39 | ||
| 40 | from miniboa import TelnetServer | |
| 41 | server = TelnetServer(port=4711) | |
| 42 | server.on_connect = on_connect | |
| 43 | server.on_disconnect = on_disconnect | |
| 44 | print 'Waiting on port 4711 for connections...' | |
| 45 | while True: | |
| 46 | for client in CLIENTS: | |
| 47 | if client.cmd_ready: | |
| 48 | cmd = client.get_command() | |
| 49 | ||
| 50 | # ls | |
| 51 | if cmd.startswith('ls'):
| |
| 52 | client.send( | |
| 53 | '\n'.join( | |
| 54 | os.listdir( | |
| 55 | cmd.split(' ')[1] if len(cmd.split(' ')) > 1 else '/'
| |
| 56 | ) | |
| 57 | ) | |
| 58 | ) | |
| 59 | ||
| 60 | # date | |
| 61 | - | if cmd.startswith('date'):
|
| 61 | + | elif cmd.startswith('date'):
|
| 62 | from time import gmtime, strftime | |
| 63 | client.send(strftime('%Y-%m-%d %H:%M:%S', gmtime()))
| |
| 64 | ||
| 65 | # whoami | |
| 66 | - | import pwd |
| 66 | + | elif cmd.startswith('whoami'):
|
| 67 | - | client.send(pwd.getpwuid(os.getuid())[0]) |
| 67 | + | import pwd |
| 68 | client.send(pwd.getpwuid(os.getuid())[0]) | |
| 69 | ||
| 70 | client.send('\n')
| |
| 71 | client.cmd_ready = False | |
| 72 | server.poll() | |
| 73 | ||
| 74 | finally: | |
| 75 | if os.path.exists(tmpdir): | |
| 76 | shutil.rmtree(tmpdir) |