通过pexpect调用ssh存在终端/显示问题

使用以下代码段登录到给定的主机并执行一组命令,然后使该会话可供用户使用。

一切都按预期进行,除了以某种方式弄乱了显示设置一词。

例如ls显示未对齐输出,而vi <file>未正确显示文件内容(附有快照)

即使shell上的命令超出了当前行,在该行中移动也无法正常工作。

尝试使用TERM设置尝试了不同的选项,但无济于事

import pexpect,struct,fcntl,termios,signal,sys,os
def sigwinch_passthrough (sig,data):
    s = struct.pack("HHHH",0)
    a = struct.unpack('hhhh',fcntl.ioctl(sys.stdout.fileno(),termios.TIOCGWINSZ,s))
    global child
    child.setwinsize(a[0],a[1])

if len(sys.argv) <= 1:
    print "Usage: sys.argv[0] <ip>"
    sys.exit(1)

ip = sys.argv[1]
ip.strip()
print "10.2.12.",ip

child = pexpect.spawn('/usr/bin/ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no em7admin@10.2.12.%s' % ip,env=os.environ)
signal.signal(signal.SIGWINCH,sigwinch_passthrough)

child.setwinsize(400,400)
child.sendline('export TERM="xterm-256color"')

child.expect('password:.*',timeout=9)
child.sendline('em7admin')

child.expect ('.*\$ ')
child.sendline('sudo bash')
child.expect('.* password for.*:.*',timeout=9)

child.sendline('em7admin')
child.expect ('.*\# ')

child.sendline('unset TMOUT')
child.interact()

通过pexpect调用ssh存在终端/显示问题

abiaoa 回答:通过pexpect调用ssh存在终端/显示问题

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3047542.html

大家都在问