I'm using pexpect to a remote server and handover screen to me. the script run success. only problem is that the screen output kind of get squeezed after login using pexpect. it's like not getting a full screen terminal for input and output. the impact is while input command it could cause problem and some content can't be fully displayed.(get a "newline" in the middle of the screen) but i'm sure there's no problem with the screen output by using normal ssh login.
I use Iterm2 for remote login, but whatever Iterm or terminal. issue is the same.
import pexpect
import getpass, os , sys
from time import sleep
def ssh_command (user,password,servername):
ssh_newkey = 'Are you sure you want to continue connecting'
child = pexpect.spawn('ssh -l %s %s '%(user, servername))
fout = open('mylog.txt','wb')
child.logfile = fout
i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '],timeout =5)
if i == 0: # Timeout
print('ERROR!')
return None
if i == 1: # SSH does not have the public key. Just accept it.
child.sendline ('yes')
child.expect ('password: ')
i = child.expect([pexpect.TIMEOUT, 'password: '],timeout =5)
if i == 0: # Timeout
print('ERROR!')
return None
child.sendline(password)
child.interact()
expect output(like normal ssh login):
Mac:user|/var $ ssh admin101
user@admin101's password:
[user@admin101 ~]$ echo "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890"
squeezed terminal output(by using pexpect):
Mac:user|/var $ python remote.py
4567890an@admin101 ~]$ echo "1234567890 1234567890 1234567890 1234567890 123