2016-05-21 16 views
1

だから、通常の端末からアクセスできる通常のPythonスクリプトがあります。スクリプトはあなたの端末で動作します(それはゲームです)。PythonのTelnetでスクリプトを実行する

私は何とかtelnetサーバーを作成するスクリプトを作成したいと思います。ユーザーtelnetがipに接続されている場合、スクリプトはそのターミナルで実行されます。

どうすればよいですか?

+0

https://pypi.python.org/pypi/miniboaをご覧ください。これは、Python用の小型で軽いtelnetサーバーです。しかし、私はすべてを理解しているか分からない。 Telnet経由でサーバからクライアントにスクリプトをコピーしたいのですか? – Laszlowaty

+0

@Laszlowaty:すぐにそれを見ていきます。私は基本的にtelnetサーバーを作成するスクリプトを必要とし、ユーザーがそのtelnetサーバーに接続すると、Telnetサーバーと同じマシン上にホストされている別のpythonスクリプトが実行されます。基本的に、私がtelnet経由で使っているスクリプトにアクセスできるようにします。 – prestotron

+0

まあ、それは妖精です。サーバで単にユーザ入力を読み込み、 'if'入力が正しい場合は、スクリプトを実行してstdoutを捕捉してユーザに送信するためにos.system( 'command')を使用してください。しかし、これは少しのセキュリティ上の問題です(コマンドを書くことはできません)。 – Laszlowaty

答えて

0

xinetdを使用すると、pythonスクリプトを開始するサービスを追加できます。標準入力と出力はネットワーク上の目的のポートで送信されるため、スクリプトを変更する必要はありません(input/raw_inputprintメソッドは正常に動作します)。

簡単なチュートリアル: http://linuxpoison.blogspot.com/2010/01/how-to-add-custom-new-service-under.html

0

私は、この問題のために全体のコードを作成しません。私はあなたに簡単な例を与えます。

これは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) 

ラインstdout = catch_stdout(os.system('python name_of_the_script'))は、そのコマンドの出力をキャッチcatch_stdout bashコマンドや機能を実行するためにos.systemを使用しています。後で、この出力をユーザーに送信するだけです。

0

私はpexpectスポーンクラスとtelnetプロジェクトminiboaでtelnet経由で接続しようとしていたプログラムを呼び出すことで解決したのと同様の問題がありました。プログラムをpexpectでラップすることで、stdin/stdoutを安全に読み書きすることができます。

入力機能を使用するために、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は呼び出すプログラムです。これは、ほとんどの場合、miniboaの例から集められています。

関連する問題