首页 > 解决方案 > 使用 Python Web 框架从远程 ssh 服务器向我的 html 页面显示消息

问题描述

我使用Python flask 和 paramiko从我的 html 表单在我的远程 ssh 服务器上执行命令。

它应该将消息显示回我的 html 文件:

(来自 ssh 服务器中执行的文件的结果)

欢迎任何想法或网站链接

谢谢你。

标签: pythonhtmlflaskserverparamiko

解决方案


您可以在函数中使用类似的东西。调用该函数后,将“输出”返回到 html。

import sys, paramiko


if len(sys.argv) < 4:
    print "args missing"
    sys.exit(1)

hostname = sys.argv[1]
password = sys.argv[2]
command = sys.argv[3]

username = "admin"
port = 22

try:
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.WarningPolicy())

    client.connect(hostname, port=port, username=username, password=password)

    stdin, stdout, stderr = client.exec_command(command)
    output = stdout.read(),

finally:
    client.close()

推荐阅读