Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.