简体   繁体   English

在Python中通过Telnet运行脚本

[英]Running a script over Telnet in Python

so I have some normal Python scripts that you can access through your terminal normally. 所以我有一些普通的Python脚本,你可以通过你的终端正常访问。 Like the script works with your terminal (it is a game). 就像脚本适用于你的终端(它是一个游戏)。

Now I want to somehow make a script that creates a telnet server, and when a user telnet's to the ip, the script will run in their terminal. 现在我想以某种方式创建一个创建telnet服务器的脚本,当用户telnet到ip时,脚本将在他们的终端中运行。

How do I go about this? 我该怎么做?

You can use xinetd to add a service starting your python script. 您可以使用xinetd添加启动python脚本的服务。 The standard input and output will be transmitted over the network on desired port, so you do not need to modify your scripts ( input / raw_input and print methods will work fine). 标准输入和输出将通过网络在所需端口上传输,因此您无需修改​​脚本( input / raw_inputprint方法将正常工作)。

Brief tutorial: http://linuxpoison.blogspot.com/2010/01/how-to-add-custom-new-service-under.html 简要教程: http//linuxpoison.blogspot.com/2010/01/how-to-add-custom-new-service-under.html

I won't create whole code for this problem. 我不会为这个问题创建完整的代码。 I will just give you a brief example. 我将举一个简短的例子。

This is the part of the telnet server : 这是telnet server的一部分:

list_of_the_connections = []
for connection in list_of_the_connections:
    user_command = connection.read()
    if user_command = 'name_of_the_script':
        stdout = catch_stdout(os.system('python name_of_the_script'))
    connection.send(stdout)

Line stdout = catch_stdout(os.system('python name_of_the_script')) is using os.system to run the bash command and function catch_stdout catches the output of that command. stdout = catch_stdout(os.system('python name_of_the_script'))使用os.system运行bash命令,函数catch_stdout捕获该命令的输出。 Later on, all you have to do is to send this output to the user. 稍后,您所要做的就是将此输出发送给用户。

I had a similar problem that I solved by calling the program that I was trying to connect to over telnet in a pexpect spawn class and the telnet project miniboa . 我有一个类似的问题,我解决了通过调用我试图通过pexpect spawn类和telnet项目miniboa连接到telnet的程序 By wrapping the program in pexpect you can the read and write to stdin/stdout safely. 通过将程序包装在pexpect中,您可以安全地读取和写入stdin / stdout。

To use the input function, pexpect uses regular expressions to know when to stop reading and get input, so I added '>' to the end of my read from stdin function. 要使用输入函数,pexpect使用正则表达式来知道何时停止读取并获取输入,因此我在stdin函数的读取结尾处添加了“>”。

class Client(object):
    def __init__(self, client):
        self.client = client
        self.process = pexpect.spawnu(PROGRAM)

def on_connect(client):
    clients.append(Client(client))

def process_clients():
    """
    Check each client, if client.cmd_ready == True then there is a line of
    input available via client.get_command().
    """
    for client in clients:
        if client.active and client.cmd_ready:
            # If the client sends input echo it to the chat room
            interact(client)

def interact(client):
    choice = client.process.expect([re.compile('\n*/>$'), '\n'])
    if choice == 1: # not an input prompt
        if len(client.process.before) == 0:
            return
        client.client.send(client.process.before+'\n')
    else: #input
        msg = client.client.get_command().strip()
        client.process.sendline(msg)

if __name__ == "__main__":
    tn = miniboa.TelnetServer(
        port=7000, 
        address='127.0.0.1', 
        on_connect = on_connect
    )
    while True:
        tn.poll()
        if len(clients) > 0:
            process_clients()

PROGRAM is the program to be called. PROGRAM是要调用的程序。 This is mostly gleaned from the miniboa examples. 这主要是从miniboa的例子中收集到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM