├── .gitignore ├── Command2Api.py ├── Command2Api_Py3.py ├── README.md └── images └── 0.png /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea 3 | .gradle -------------------------------------------------------------------------------- /Command2Api.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import BaseHTTPServer 3 | import SimpleHTTPServer 4 | import cgi 5 | import threading 6 | import sys 7 | import uuid 8 | 9 | screen_list = [] 10 | 11 | uri = '/' + str(uuid.uuid4()).replace("-", "") 12 | 13 | class thread(threading.Thread): 14 | def __init__(self, threadname, command): 15 | threading.Thread.__init__(self, name='Thread_' + threadname) 16 | self.threadname = int(threadname) 17 | self.command = command 18 | 19 | def run(self): 20 | global screen_list 21 | ret = subprocess.Popen( 22 | self.command, 23 | shell=True, 24 | stdin=subprocess.PIPE, 25 | stdout=subprocess.PIPE, 26 | stderr=subprocess.PIPE 27 | ) 28 | for i in iter(ret.stdout.readline, b""): 29 | res = i.decode().strip() 30 | print(res) 31 | screen_list.append(res) 32 | 33 | class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): 34 | def do_GET(self): 35 | global screen_list 36 | if self.path == uri: 37 | self.send_response(200) 38 | self.send_header('Content-Type', 'text/plain') 39 | self.end_headers() 40 | self.wfile.write("\n".join(screen_list).encode()) 41 | 42 | if __name__ == '__main__': 43 | # New Thread: Get Command Result 44 | t1 = thread('1', sys.argv[1]) 45 | t1.start() 46 | # Webserver 47 | port = int(sys.argv[2]) 48 | print("URL: http://localhost:{0}{1}".format(port, uri)) 49 | Handler = ServerHandler 50 | httpd = BaseHTTPServer.HTTPServer(('0.0.0.0', port), Handler) 51 | httpd.serve_forever() -------------------------------------------------------------------------------- /Command2Api_Py3.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import uuid 3 | import threading 4 | import subprocess 5 | from http.server import HTTPServer, SimpleHTTPRequestHandler 6 | 7 | screen_list = [] 8 | 9 | uri = '/' + str(uuid.uuid4()).replace("-", "") 10 | 11 | class NewThread(threading.Thread): 12 | def __init__(self, thread_name, command): 13 | threading.Thread.__init__(self, name='Thread_' + thread_name) 14 | self.thread_name = int(thread_name) 15 | self.command = command 16 | 17 | def run(self): 18 | global screen_list 19 | ret = subprocess.Popen( 20 | self.command, 21 | shell=True, 22 | stdin=subprocess.PIPE, 23 | stdout=subprocess.PIPE, 24 | stderr=subprocess.PIPE 25 | ) 26 | for i in iter(ret.stdout.readline, b""): 27 | res = i.decode().strip() 28 | print(res) 29 | screen_list.append(res) 30 | 31 | 32 | class ServerHandler(SimpleHTTPRequestHandler): 33 | def do_GET(self): 34 | global screen_list 35 | if self.path == uri: 36 | self.send_response(200) 37 | self.send_header('Content-Type', 'text/plain') 38 | self.end_headers() 39 | self.wfile.write("\n".join(screen_list).encode()) 40 | 41 | 42 | if __name__ == '__main__': 43 | # New Thread: Get Command Result 44 | t1 = NewThread('1', sys.argv[1]) 45 | t1.start() 46 | # Webserver 47 | port = int(sys.argv[2]) 48 | print("URL: http://localhost:{0}{1}".format(port, uri)) 49 | httpd = HTTPServer(('0.0.0.0', port), ServerHandler) 50 | httpd.serve_forever() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Command2API 2 | 3 | **作者**:key@元亨实验室 4 | 5 | **简介**:这是一个可以实时获取执行命令结果的脚本,脚本原理就是一个线程开启Web服务,一个线程执行命令,通过全局变量与Web服务共享执行命令的结果。 6 | 7 | ## 使用方法 8 | 9 | 执行命令: 10 | 11 | ```shell 12 | python Command2Api.py "执行的命令" Web运行的端口 13 | python Command2Api_Py3.py "执行的命令" Web运行的端口 // Python3版本来自noobyan 14 | ``` 15 | 16 | 接着在命令中会输出对应的URL,访问即可获取信息: 17 | 18 | ![](images/0.png) 19 | 20 | -------------------------------------------------------------------------------- /images/0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gh0stkey/Command2API/dbfbf3a9c1072d431ce9541691f38d203439cfb0/images/0.png --------------------------------------------------------------------------------