HTML页面中的Paramiko分割线

时间:2019-04-29 11:01:32

标签: html python-3.x paramiko

在我的Paramiko / Python脚本中,我需要将连接结果输出到HTML网页。当我这样做时,即使有几行,结果也会显示在一行中。如果我只是在没有HTML的Python中运行代码,那么我可能会断行。 如何在HTML的行中显示Paramiko输出?

如果我在下面使用此代码,则会在HTML中正确断行,但这带来了其他一些问题。

print("Content-type: text/html")
print("")
print("<html><head>")
print("")
print("</head><body>")

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname='192.168.40.169', username='admin', password='password', port='22')
cmd = '''show run'''
stdin, stdout, stderr = client.exec_command(cmd)
time.sleep(1)
for line in stdout:
    while True:
        line = stdout.readline()
        if not line:
            break
        print(line + '<br />')
print("</body></html>")

打印时注意HTML标头和'br /'标签。这样就打破了界限。除此之外,这样我不能发送多个命令或将变量从PHP传递到Python。脚本不接受它们(不知道为什么...)

一个有问题的脚本,它在一个会话中接受PHP变量传递,多个SSH命令,但又没有断行,就像这样:

import threading, paramiko
import time
import sys
#==========================
command = sys.argv[1]
command2 = sys.argv[2]
command3 = sys.argv[3]
command4 = sys.argv[4]
command5 = sys.argv[5]
command6 = sys.argv[6]
command7 = sys.argv[7]
command8 = sys.argv[8]
command9 = sys.argv[9]
command10 = sys.argv[10]
#==========================
print("<html><head>")
print("</head><body>")

class ssh:
    shell = None
    client = None
    transport = None

    def __init__(self, address, username, password):
#        print(("Connecting to server on ip", str(address) + "."))
        self.client = paramiko.client.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
        self.client.connect(address, username=username, password=password, look_for_keys=False)
        self.transport = paramiko.Transport((address, 22))
        self.transport.connect(username=username, password=password)

        thread = threading.Thread(target=self.process)
        thread.daemon = True
        thread.start()

    def close_connection(self):
        if(self.client != None):
            self.client.close()
            self.transport.close()

    def open_shell(self):
        self.shell = self.client.invoke_shell()

    def send_shell(self, command):
        if(self.shell):
            self.shell.send(command + "\n")
        else:
            print("<h1>Shell não aberta.</h1>")

    def process(self):
        global strdata, fulldata
        while True:
            # Print data when available
            if self.shell is not None and self.shell.recv_ready():
                alldata = self.shell.recv(1024)
                while self.shell.recv_ready():
                    alldata += self.shell.recv(1024)
                strdata = strdata + str(alldata)
                fulldata = fulldata + alldata.decode("UTF-8")
                strdata = self.print_lines(strdata) # print all received data except last line

    def print_lines(self, data):
        last_line = data
        if '\n' in data:
            lines = data.splitlines()
            for i in range(0, len(lines)-1):
                print((lines[i]))
            last_line = lines[len(lines) - 1]
            if data.endswith('\n'):
                print(lines.split(last_line))
                last_line = ''
        return last_line

sshUsername = "admin"
sshPassword = "password"
sshServer = "192.168.40.169"

connection = ssh(sshServer, sshUsername, sshPassword)
connection.open_shell()
connection.send_shell(command)
connection.send_shell(command2)
connection.send_shell(command3)
connection.send_shell(command4)
connection.send_shell(command5)
connection.send_shell(command6)
connection.send_shell(command7)
connection.send_shell(command8)
connection.send_shell(command9)
connection.send_shell(command10)
time.sleep(1)
print(fulldata)   # This contains the complete data received.
connection.close_connection()

print("</body></html>")

我认为我应该在某个地方放置一个“ br /”标签,但是我已经尝试了所有方法,但是它没有用。 :( 我真的真的需要帮助。

输出应该是这样的:

R5#en
R%#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R5(config)#hostname R5
R5(config)#

但这就是我得到的:

R5#en R5#conf t Enter configuration commands, one per line. End with CNTL/Z. R5(config)#hostname R5 R5(config)#

原谅大文章...

0 个答案:

没有答案