mirror of
https://github.com/ARM-software/devlib.git
synced 2025-02-07 13:40:48 +00:00
Merge pull request #52 from garethstockwell/master
TelnetConnection: support for systems which do not prompt for a password
This commit is contained in:
commit
2d496486bf
@ -43,14 +43,14 @@ sshpass = None
|
|||||||
logger = logging.getLogger('ssh')
|
logger = logging.getLogger('ssh')
|
||||||
|
|
||||||
|
|
||||||
def ssh_get_shell(host, username, password=None, keyfile=None, port=None, timeout=10, telnet=False):
|
def ssh_get_shell(host, username, password=None, keyfile=None, port=None, timeout=10, telnet=False, original_prompt=None):
|
||||||
_check_env()
|
_check_env()
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
while True:
|
while True:
|
||||||
if telnet:
|
if telnet:
|
||||||
if keyfile:
|
if keyfile:
|
||||||
raise ValueError('keyfile may not be used with a telnet connection.')
|
raise ValueError('keyfile may not be used with a telnet connection.')
|
||||||
conn = TelnetConnection()
|
conn = TelnetConnection(original_prompt=original_prompt)
|
||||||
else: # ssh
|
else: # ssh
|
||||||
conn = pxssh.pxssh()
|
conn = pxssh.pxssh()
|
||||||
|
|
||||||
@ -77,20 +77,32 @@ def ssh_get_shell(host, username, password=None, keyfile=None, port=None, timeou
|
|||||||
class TelnetConnection(pxssh.pxssh):
|
class TelnetConnection(pxssh.pxssh):
|
||||||
# pylint: disable=arguments-differ
|
# pylint: disable=arguments-differ
|
||||||
|
|
||||||
def login(self, server, username, password='', original_prompt=r'[#$]', login_timeout=10,
|
def __init__(self, original_prompt):
|
||||||
|
super(TelnetConnection, self).__init__()
|
||||||
|
self.original_prompt = original_prompt or r'[#$]'
|
||||||
|
|
||||||
|
def login(self, server, username, password='', login_timeout=10,
|
||||||
auto_prompt_reset=True, sync_multiplier=1, port=23):
|
auto_prompt_reset=True, sync_multiplier=1, port=23):
|
||||||
cmd = 'telnet -l {} {} {}'.format(username, server, port)
|
args = ['telnet']
|
||||||
|
if username is not None:
|
||||||
|
args += ['-l', username]
|
||||||
|
args += [server, str(port)]
|
||||||
|
cmd = ' '.join(args)
|
||||||
|
|
||||||
spawn._spawn(self, cmd) # pylint: disable=protected-access
|
spawn._spawn(self, cmd) # pylint: disable=protected-access
|
||||||
i = self.expect('(?i)(?:password)', timeout=login_timeout)
|
|
||||||
if i == 0:
|
|
||||||
self.sendline(password)
|
|
||||||
i = self.expect([original_prompt, 'Login incorrect'], timeout=login_timeout)
|
|
||||||
else:
|
|
||||||
raise pxssh.ExceptionPxssh('could not log in: did not see a password prompt')
|
|
||||||
|
|
||||||
if i:
|
if password is None:
|
||||||
raise pxssh.ExceptionPxssh('could not log in: password was incorrect')
|
i = self.expect([self.original_prompt, 'Login timed out'], timeout=login_timeout)
|
||||||
|
else:
|
||||||
|
i = self.expect('(?i)(?:password)', timeout=login_timeout)
|
||||||
|
if i == 0:
|
||||||
|
self.sendline(password)
|
||||||
|
i = self.expect([self.original_prompt, 'Login incorrect'], timeout=login_timeout)
|
||||||
|
else:
|
||||||
|
raise pxssh.ExceptionPxssh('could not log in: did not see a password prompt')
|
||||||
|
|
||||||
|
if i:
|
||||||
|
raise pxssh.ExceptionPxssh('could not log in: password was incorrect')
|
||||||
|
|
||||||
if not self.sync_original_prompt(sync_multiplier):
|
if not self.sync_original_prompt(sync_multiplier):
|
||||||
self.close()
|
self.close()
|
||||||
@ -142,6 +154,7 @@ class SshConnection(object):
|
|||||||
timeout=None,
|
timeout=None,
|
||||||
telnet=False,
|
telnet=False,
|
||||||
password_prompt=None,
|
password_prompt=None,
|
||||||
|
original_prompt=None,
|
||||||
):
|
):
|
||||||
self.host = host
|
self.host = host
|
||||||
self.username = username
|
self.username = username
|
||||||
@ -152,7 +165,7 @@ class SshConnection(object):
|
|||||||
self.password_prompt = password_prompt if password_prompt is not None else self.default_password_prompt
|
self.password_prompt = password_prompt if password_prompt is not None else self.default_password_prompt
|
||||||
logger.debug('Logging in {}@{}'.format(username, host))
|
logger.debug('Logging in {}@{}'.format(username, host))
|
||||||
timeout = timeout if timeout is not None else self.default_timeout
|
timeout = timeout if timeout is not None else self.default_timeout
|
||||||
self.conn = ssh_get_shell(host, username, password, self.keyfile, port, timeout, telnet)
|
self.conn = ssh_get_shell(host, username, password, self.keyfile, port, timeout, telnet, original_prompt)
|
||||||
|
|
||||||
def push(self, source, dest, timeout=30):
|
def push(self, source, dest, timeout=30):
|
||||||
dest = '{}@{}:{}'.format(self.username, self.host, dest)
|
dest = '{}@{}:{}'.format(self.username, self.host, dest)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user