import subprocess
sp = subprocess.Popen(('ssh', 'localhost', 'ls'), stdout=subprocess.PIPE)
print(sp.stdout.read())
Using os.forkpty() to create a pseudo-terminal to ssh to a remote server and communicate with it