Help us understand the problem. What is going on with this article?

新人育成に便利?Telnet練習用サーバを書いてみた

More than 5 years have passed since last update.

新人にTelnetでサーバやらNW機器の操作を経験させたいが、VMでLinuxを動かすだけならともかく、ロードバランサなどを用意するのは大変である。そんなわけでPythonのsocketserverを使ってTelnet接続をシミュレートする練習用サーバを書いてみた。

適当なコマンドを入力したら、それに応じた出力をするようif文で書いておき、TeraTermなどのターミナルで接続する。

if command == "exit":
  sys.exit()

elif command == "ls":
  self.write("bin     etc     proc    usr     dev     home\r\n")

ちゃんとTelnetプロトコルを喋るので、現場で使ってるターミナルで使えるし、マクロなどの実験台としてもいい。

server.py
import sys
import socketserver

class MyTCPHandler(socketserver.BaseRequestHandler):

    prompt = "$ "

    def command(self,command):
        print(command)
        if command == "exit":
            sys.exit()

        elif command == "ls":
            self.write("bin     etc     proc    usr     dev     home\r\n")

        elif command == "ls -l":
            self.write("drwxr-xr-x+ 1 t.uehara Domain Users      0 3月 19 11:14 bin\r\n")
            self.write("drwxr-xr-x+ 1 t.uehara Domain Users      0 3月 19 11:14 etc\r\n")
            self.write("drwxr-xr-x+ 1 t.uehara Domain Users      0 3月 19 11:14 proc\r\n")
            self.write("drwxr-xr-x+ 1 t.uehara Domain Users      0 3月 19 11:14 usr\r\n")
            self.write("drwxr-xr-x+ 1 t.uehara Domain Users      0 3月 19 11:14 dev\r\n")
            self.write("drwxr-xr-x+ 1 t.uehara Domain Users      0 3月 19 11:14 home\r\n")

    def write(self,str):
        self.request.sendall(bytes(str,"UTF-8"))

    def handle(self):

        buff = []

        while True:
            self.data = self.request.recv(1024)

            if self.data == bytes("\r\x00","UTF-8"): # 改行
                self.write("\r\n")
                self.command("".join(buff))
                del buff[:]
                self.write(self.prompt)
            elif self.data == bytes("\x03","UTF-8"): # Ctrl+C
                sys.exit()
            else:
                self.request.sendall(self.data)
                try:
                    buff.append(self.data.decode("UTF-8"))
                except UnicodeDecodeError:
                    print(self.data)

if __name__ == "__main__":

    HOST, PORT = "localhost", 23

    server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)
    server.serve_forever()

ちなみに本来であれば、Telnet接続する際のコマンドなんかも解釈させたいところだが、今のところオウム返しさせている。独自に拡張させたい場合は、下記のサイトが参考になる。

http://www5e.biglobe.ne.jp/aji/3min/55.html

コマンドヒストリーとかTab補完も無理。うちの職場はZshなんでぇ~という方にも、この練習台は向かない。でも、SSH版は密かに作ってみたかったり。

usop4
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした