1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-01-31 02:00:45 +00:00

TelnetConnction: splitting from SshConnection

Perviously, a parameter passed into SshConnection controlled whether the
connection was established over SSH or Telnet. Now, there is a separate
class for Telnet connections.
This commit is contained in:
Sergei Trofimov 2016-12-08 11:48:41 +00:00
parent 1ed29a8385
commit b3cea0c0d2
3 changed files with 57 additions and 11 deletions

View File

@ -19,4 +19,4 @@ from devlib.trace.ftrace import FtraceCollector
from devlib.host import LocalConnection from devlib.host import LocalConnection
from devlib.utils.android import AdbConnection from devlib.utils.android import AdbConnection
from devlib.utils.ssh import SshConnection from devlib.utils.ssh import SshConnection, TelnetConnection

View File

@ -50,7 +50,7 @@ def ssh_get_shell(host, username, password=None, keyfile=None, port=None, timeou
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(original_prompt=original_prompt) conn = TelnetPxssh(original_prompt=original_prompt)
else: # ssh else: # ssh
conn = pxssh.pxssh() conn = pxssh.pxssh()
@ -74,11 +74,11 @@ def ssh_get_shell(host, username, password=None, keyfile=None, port=None, timeou
return conn return conn
class TelnetConnection(pxssh.pxssh): class TelnetPxssh(pxssh.pxssh):
# pylint: disable=arguments-differ # pylint: disable=arguments-differ
def __init__(self, original_prompt): def __init__(self, original_prompt):
super(TelnetConnection, self).__init__() super(TelnetPxssh, self).__init__()
self.original_prompt = original_prompt or r'[#$]' self.original_prompt = original_prompt or r'[#$]'
def login(self, server, username, password='', login_timeout=10, def login(self, server, username, password='', login_timeout=10,
@ -165,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, original_prompt) self.conn = ssh_get_shell(host, username, password, self.keyfile, port, timeout, False, None)
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)
@ -276,6 +276,29 @@ class SshConnection(object):
raise TimeoutError(e.command.replace(pass_string, ''), e.output) raise TimeoutError(e.command.replace(pass_string, ''), e.output)
class TelnetConnection(SshConnection):
def __init__(self,
host,
username,
password=None,
port=None,
timeout=None,
password_prompt=None,
original_prompt=None,
):
self.host = host
self.username = username
self.password = password
self.port = port
self.keyfile = None
self.lock = threading.Lock()
self.password_prompt = password_prompt if password_prompt is not None else self.default_password_prompt
logger.debug('Logging in {}@{}'.format(username, host))
timeout = timeout if timeout is not None else self.default_timeout
self.conn = ssh_get_shell(host, username, password, None, port, timeout, True, original_prompt)
def _give_password(password, command): def _give_password(password, command):
if not sshpass: if not sshpass:
raise HostError('Must have sshpass installed on the host in order to use password-based auth.') raise HostError('Must have sshpass installed on the host in order to use password-based auth.')

View File

@ -107,10 +107,9 @@ Connection Types
.. class:: SshConnection(host, username, password=None, keyfile=None, port=None,\ .. class:: SshConnection(host, username, password=None, keyfile=None, port=None,\
timeout=None, telnet=False, password_prompt=None,\ timeout=None, password_prompt=None)
original_prompt=None)
A connectioned to a device on the network over SSH or Telnet. A connectioned to a device on the network over SSH.
:param host: SSH host to which to connect :param host: SSH host to which to connect
:param username: username for SSH login :param username: username for SSH login
@ -130,9 +129,33 @@ Connection Types
:param timeout: Timeout for the connection in seconds. If a connection :param timeout: Timeout for the connection in seconds. If a connection
cannot be established within this time, an error will be cannot be established within this time, an error will be
raised. raised.
:param telnet: If ``True``, Telenet will be used instead of SSH. In this :param password_prompt: A string with the password prompt used by
case, all other parameters apart from "original_prompt" will ``sshpass``. Set this if your version of ``sshpass``
be ignored. uses somethin other than ``"[sudo] password"``.
.. class:: TelnetConnection(host, username, password=None, port=None,\
timeout=None, password_prompt=None,\
original_prompt=None)
A connectioned to a device on the network over Telenet.
.. note:: Since Telenet protocol is does not support file transfer, scp is
used for that purpose.
:param host: SSH host to which to connect
:param username: username for SSH login
:param password: password for the SSH connection
.. note:: In order to user password-based authentication,
``sshpass`` utility must be installed on the
system.
:param port: TCP port on which SSH server is litening on the remoted device.
Omit to use the default port.
:param timeout: Timeout for the connection in seconds. If a connection
cannot be established within this time, an error will be
raised.
:param password_prompt: A string with the password prompt used by :param password_prompt: A string with the password prompt used by
``sshpass``. Set this if your version of ``sshpass`` ``sshpass``. Set this if your version of ``sshpass``
uses somethin other than ``"[sudo] password"``. uses somethin other than ``"[sudo] password"``.