|
#!/usr/bin/python
import sys
import time
import pexpect
import os
import struct
import fcntl
import termios
import signal
def sigwinch_passthrough (sig, data):
winsize = getwinsize()
global child
child.setwinsize(winsize[0],winsize[1])
def getwinsize():
"""This returns the window size of the child tty.
The return value is a tuple of (rows, cols).
"""
if 'TIOCGWINSZ' in dir(termios):
TIOCGWINSZ = termios.TIOCGWINSZ
else:
TIOCGWINSZ = 1074295912L # Assume
s = struct.pack('HHHH', 0, 0, 0, 0)
x = fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ, s)
return struct.unpack('HHHH', x)[0:2]
if __name__ == '__main__':
user = 'username'
passwd = 'password'
host = 'example.com'
print 'ssh ' + user + '@' + host + ' ...'
time.sleep(1)
child = pexpect.spawn('ssh relay', env = {"TERM" : "xterm-256color"})
signal.signal(signal.SIGWINCH, sigwinch_passthrough)
if len(sys.argv) > 1:
child.expect("Enter PASSCODE:")
child.sendline("PIN-CODE" + sys.argv[1])
child.expect('-bash-baidu-ssl.*')
child.sendline('ssh ' + host)
winsize = getwinsize();
child.setwinsize(winsize[0], winsize[1])
child.expect('.*password:.*')
child.sendline(passwd)
child.interact()
pass
|
|
|