import pexpect class SSH(): def connect(self): x = True sshConnection = pexpect.spawn('ssh -l matheus -p 22 192.168.0.254') sshConnection.expect('matheus@192.168.0.254\'s password:') sshConnection.sendline('senha') while True: print sshConnection.readline(); if x is True: sshConnection.sendline('uptime') x = False sshConnection.readline(); s = SSH() s.connect()