Paramiko + Mqtt

import threading, paramiko
from paramiko_expect import SSHClientInteraction
import socket
import paho.mqtt.client as mqtt


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

# PROMPT= '$'
PROMPT = 'wehner@tmo:~\$\s+'
def on_connect(self, client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
self.mqttclient.subscribe("test/#")

def on_message(self ,client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
if( msg.topic =="test/abc"):
tmp1=str(msg.payload)[1:]
tmp1=tmp1.strip("'")

words=tmp1.split()
tmp1=words.pop(0)
if tmp1.lower()== "command" :
tmp1=" "
tmp2=tmp1.join(words)
print(tmp2)
with SSHClientInteraction(self.client, timeout=10, display=True) as interact:
interact.send(tmp2 + '\n')
interact.expect(self.PROMPT, timeout=500)
cmd_output_uname = interact.current_output_clean
print ("output who" , cmd_output_uname , "ohw")

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,port=66, username=username, password=password, look_for_keys=False)
self.transport = paramiko.Transport((address, 66))
self.transport.connect(username=username, password=password)
self.mqttclient = mqtt.Client()
self.mqttclient.on_connect = self.on_connect
self.mqttclient.on_message = self.on_message

self.mqttclient.connect("localhost", 1883, 60)

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

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

def openShell(self):
self.shell = self.client.invoke_shell()
with SSHClientInteraction(self.client, timeout=10, display=True) as interact:
interact.expect(self.PROMPT)
cmd_output_uname = interact.current_output_clean
print ("cc1" , cmd_output_uname , "ccc1")
interact.send('who\n')
interact.expect(self.PROMPT, timeout=500)
cmd_output_uname = interact.current_output_clean
print ("output who" , cmd_output_uname , "ohw")

def sendShell(self, command):
if(self.shell):
self.shell.send(command + "\n")
else:
print("Shell not opened.")

def process(self):
global connection
while True:
# Print data when available
if self.shell != None and self.shell.recv_ready():
alldata = self.shell.recv(1024)
while self.shell.recv_ready():
alldata += self.shell.recv(1024)
strdata = str(alldata, "utf8")
strdata.replace('\r', '')
print(strdata, end = "")
if(strdata.endswith("$ ")):
print("\n$ ", end = "")

sshUsername = "user"
sshPassword = "passwd"
sshServer = socket.gethostbyname("tmo.net") #"93.206.206.186"

connection = ssh(sshServer, sshUsername, sshPassword)
connection.openShell()

while True:
command = input('$ ')
if command.startswith(" "):
command = command[1:]
connection.sendShell(command)