开发者

How do I drop a bash shell from within Python?

开发者 https://www.devze.com 2022-12-21 15:07 出处:网络
i\'m working on a python tcp shell; I\'d like to be able to telnet to a port, and have it prompt me with a shell: ex.

i'm working on a python tcp shell; I'd like to be able to telnet to a port, and have it prompt me with a shell: ex.

$ telnet localhost 5555
Connected to localhost.
Escape character is '^]'.
$

The answer below was quite helpful in getting me on my way. Here's the working code.

import SocketServer, os, subprocess
class EchoRequestHandler(SocketServer.BaseRequestHandler ):
    def setup(self):
        print self.client_address, 'connected!'
        self.request.send(str(self.client_address) + '\n')
        self.request.send(os.system('sh'))
    def handle(self):
        data = 'dummy'
        while data:
            data = self.request.recv(1024)
            self.request.send(os.popen(data.strip()).read())
            if data.strip() == 'bye':
                return

    def finish(self):
        print self.client_address, 'disconnected!'
        self.request.send('bye ' + str(self.client_add开发者_高级运维ress) + '\n')

server = SocketServer.ThreadingTCPServer(('', 7784), EchoRequestHandler)
server.serve_forever()


import os
os.system('sh')  # or bash, tcsh, zsh, whatever. :-P
0

精彩评论

暂无评论...
验证码 换一张
取 消