Skip to content

Instantly share code, notes, and snippets.

@xl7dev
Created January 28, 2018 06:00
Show Gist options
  • Save xl7dev/4d2c5b1b4229207209fd2702c14f72fe to your computer and use it in GitHub Desktop.
Save xl7dev/4d2c5b1b4229207209fd2702c14f72fe to your computer and use it in GitHub Desktop.
auto login fortress
#!/usr/bin/env python
# encoding: utf-8
"""
@author: xl7dev
"""
import sys
import pyotp
import pexpect
import logging
import signal
import fcntl
import struct
import termios
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
class fortress(object):
def __init__(self):
self.ssh_host = ''
self.ssh_port =
self.google_auth_key = ''
self.username = ''
self.password = ''
def login(self):
totp = pyotp.TOTP(self.google_auth_key)
code = totp.now()
child = pexpect.spawn('ssh -v -p %d %s@%s' % (self.ssh_port, self.username, self.ssh_host))
signal.signal(signal.SIGWINCH, self.sigwinch_passthrough)
child.expect('Verification code>:')
child.sendline(code)
winsize = self.getwinsize()
child.setwinsize(winsize[0], winsize[1])
child.expect('Password>:')
child.sendline(self.password)
child.expect('Option>:')
print child.before
child.interact()
def sigwinch_passthrough(self):
winsize = self.getwinsize()
global child
child.setwinsize(winsize[0], winsize[1])
def getwinsize(self):
"""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
s = struct.pack('HHHH', 0, 0, 0, 0)
x = fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ, s)
return struct.unpack('HHHH', x)[0:2]
def run(self):
self.login()
if __name__ == '__main__':
demo = fortress()
demo.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment