Skip to content

Instantly share code, notes, and snippets.

@yunazuno
Created May 14, 2014 16:10
Show Gist options
  • Save yunazuno/3b840f4697dc0cab81f7 to your computer and use it in GitHub Desktop.
Save yunazuno/3b840f4697dc0cab81f7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pexpect
import signal
import fcntl
import termios
import struct
def login_and_source_config(command_spawn, config_file):
c = pexpect.spawn(command_spawn, timeout=3)
prompt = ['.*$']
try:
c.expect(prompt)
with open(config_file) as f:
config = f.read()
c.sendline('CONFIG_FILE=$(mktemp)')
c.expect(prompt)
c.sendline("""cat << EOF > $TMP_CONFIG_FILE
{config}
EOF
""".format(config=config))
c.expect(prompt)
c.sendline('source $TMP_CONFIG_FILE')
c.expect(prompt)
def sigwinch_passthrough(sig, data):
# @see https://github.com/pexpect/pexpect/blob/master/examples/script.py
# Check for buggy platforms (see pexpect.setwinsize()).
if 'TIOCGWINSZ' in dir(termios):
TIOCGWINSZ = termios.TIOCGWINSZ
else:
TIOCGWINSZ = 1074295912 # assume
s = struct.pack("HHHH", 0, 0, 0, 0)
a = struct.unpack('HHHH', fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ, s))
c.setwinsize(a[0], a[1])
signal.signal(signal.SIGWINCH, sigwinch_passthrough)
c.interact()
except pexpect.TIMEOUT:
return
if __name__ == '__main__':
import sys
config_file_for_source = sys.argv[1]
login_command = " ".join(sys.argv[2:])
login_and_source_config(login_command, config_file_for_source)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment