By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,483 Members | 3,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,483 IT Pros & Developers. It's quick & easy.

Split lines in Paramiko

P: 2
Hello everyone, good to be here.
First of all, let me start my apologising. This is my first post, and i'm already posting a big one. Sorry for that.

In my Paramiko/Python script, i need to output the SSH command results to a webpage. When i do this, the result is displayed in one single line, even though there are several. If i just run the code in Python without HTML, i can break the lines. How can show Paramiko output in lines in HTML?

If i use this code below, i get the line breaks correctly in HTML, but that brings me some other issues.:

Expand|Select|Wrap|Line Numbers
  1. print("Content-type: text/html")
  2. print("")
  3. print("<html><head>")
  4. print("")
  5. print("</head><body>")
  6.  
  7. client = paramiko.SSHClient()
  8. client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  9. client.connect(hostname='192.168.40.169', username='admin', password='password', port='22')
  10. cmd = '''show run'''
  11. stdin, stdout, stderr = client.exec_command(cmd)
  12. time.sleep(1)
  13. for line in stdout:
  14.     while True:
  15.         line = stdout.readline()
  16.         if not line:
  17.             break
  18.         print(line + '<br />')
  19. print("</body></html>")
  20.  
Notice the HTML headers and the <br /> tag when printing. This way it breaks the lines. But, this way i can't send multiple commands or pass variables from PHP to Python. The script doesn't accept them (don't know why...)

The correct script should be the following. It accepts variables from PHP, and accepts multiple commands. I can check if the response to each command was what i was expecting. But this script is not showing me split lines:

Expand|Select|Wrap|Line Numbers
  1. import threading, paramiko
  2. import time
  3. import sys
  4. #==========================
  5. command = sys.argv[1]
  6. command2 = sys.argv[2]
  7. command3 = sys.argv[3]
  8. command4 = sys.argv[4]
  9. command5 = sys.argv[5]
  10. command6 = sys.argv[6]
  11. command7 = sys.argv[7]
  12. command8 = sys.argv[8]
  13. command9 = sys.argv[9]
  14. command10 = sys.argv[10]
  15. #==========================
  16. print("<html><head>")
  17. print("</head><body>")
  18.  
  19. class ssh:
  20.     shell = None
  21.     client = None
  22.     transport = None
  23.  
  24.     def __init__(self, address, username, password):
  25. #        print(("Connecting to server on ip", str(address) + "."))
  26.         self.client = paramiko.client.SSHClient()
  27.         self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
  28.         self.client.connect(address, username=username, password=password, look_for_keys=False)
  29.         self.transport = paramiko.Transport((address, 22))
  30.         self.transport.connect(username=username, password=password)
  31.  
  32.         thread = threading.Thread(target=self.process)
  33.         thread.daemon = True
  34.         thread.start()
  35.  
  36.     def close_connection(self):
  37.         if(self.client != None):
  38.             self.client.close()
  39.             self.transport.close()
  40.  
  41.     def open_shell(self):
  42.         self.shell = self.client.invoke_shell()
  43.  
  44.     def send_shell(self, command):
  45.         if(self.shell):
  46.             self.shell.send(command + "\n")
  47.         else:
  48.             print("<h1>Shell não aberta.</h1>")
  49.  
  50.     def process(self):
  51.         global strdata, fulldata
  52.         while True:
  53.             # Print data when available
  54.             if self.shell is not None and self.shell.recv_ready():
  55.                 alldata = self.shell.recv(1024)
  56.                 while self.shell.recv_ready():
  57.                     alldata += self.shell.recv(1024)
  58.                 strdata = strdata + str(alldata)
  59.                 fulldata = fulldata + alldata.decode("UTF-8")
  60.                 strdata = self.print_lines(strdata) # print all received data except last line
  61.  
  62.     def print_lines(self, data):
  63.         last_line = data
  64.         if '\n' in data:
  65.             lines = data.splitlines()
  66.             for i in range(0, len(lines)-1):
  67.                 print((lines[i]))
  68.             last_line = lines[len(lines) - 1]
  69.             if data.endswith('\n'):
  70.                 print(lines.split(last_line))
  71.                 last_line = ''
  72.         return last_line
  73.  
  74. sshUsername = "admin"
  75. sshPassword = "password"
  76. sshServer = "192.168.40.169"
  77.  
  78. connection = ssh(sshServer, sshUsername, sshPassword)
  79. connection.open_shell()
  80. connection.send_shell(command)
  81. connection.send_shell(command2)
  82. connection.send_shell(command3)
  83. connection.send_shell(command4)
  84. connection.send_shell(command5)
  85. connection.send_shell(command6)
  86. connection.send_shell(command7)
  87. connection.send_shell(command8)
  88. connection.send_shell(command9)
  89. connection.send_shell(command10)
  90. time.sleep(1)
  91. print(fulldata)   # This contains the complete data received.
  92. connection.close_connection()
  93.  
  94. print("</body></html>")
  95.  
I think i should probably place a 'br /' tag somewhere, but i've tried everything and it's just not working. :( I really really need help with this one.

The output should be something like this:

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

But this is what i got:

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

Forgive the big post, it's my first time here and i really need help.
Apr 29 '19 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.