def executeCommand(host, command, hostname): c = SSHConnection(host, "root", "") response = c.connectWithoutPass( "/usr/share/controlies-ltspserver/.ssh/id_rsa") try: WS.websocket_send( 'http://ldap:8888', '<span style="font-size:14pt;">' + hostname + '</span> > <span style="font-size:10pt;">' + command + '</span><br>', 'mykey', 'mygroup') except: #return dict(response="fail", host=host, message="No se pudo conectar con el servidor websocket.<br/>") print "no_websocket" sys.exit(0) if response != True: print "no_ssh" sys.exit(0) #return dict(response="fail", host=host, message="No se pudo conectar. ¿Está encendido el equipo? ¿Has establecido la relación de confianza?<br/>") channel = c.exec_command(command) import select while True: if channel.exit_status_ready(): break rl, wl, xl = select.select([channel], [], [], 0.0) if len(rl) > 0: stdout_data = [] try: part = channel.recv(4096) while part: stdout_data.append(part) part = channel.recv(4096) except: raise complete = ''.join(stdout_data) n = 500 pieces = [complete[i:i + n] for i in range(0, len(complete), n)] HTML_PARSER = ansi2html() for i in pieces: html = HTML_PARSER.parse(i) try: WS.websocket_send('http://ldap:8888', html, 'mykey', 'mygroup') except: pass channel.close() c.close()
def executeCommand(host, command, hostname): c = SSHConnection(host,"root","") response = c.connectWithoutPass("/usr/share/controlies-ltspserver/.ssh/id_rsa") try: WS.websocket_send('http://ldap:8888','<span style="font-size:14pt;">'+hostname+'</span> > <span style="font-size:10pt;">'+command+'</span><br>','mykey','mygroup') except: #return dict(response="fail", host=host, message="No se pudo conectar con el servidor websocket.<br/>") print "no_websocket" sys.exit(0) if response != True: print "no_ssh" sys.exit(0) #return dict(response="fail", host=host, message="No se pudo conectar. ¿Está encendido el equipo? ¿Has establecido la relación de confianza?<br/>") channel = c.exec_command(command) import select while True: if channel.exit_status_ready(): break rl, wl, xl = select.select([channel], [], [], 0.0) if len(rl) > 0: stdout_data = [] try: part = channel.recv(4096) while part: stdout_data.append(part) part = channel.recv(4096) except: raise complete = ''.join(stdout_data) n = 500 pieces = [complete[i:i+n] for i in range(0, len(complete), n)] HTML_PARSER = ansi2html() for i in pieces: html = HTML_PARSER.parse(i) try: WS.websocket_send('http://ldap:8888',html,'mykey','mygroup') except: pass channel.close() c.close()