Robot Framework SSH Library
library.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from __future__ import print_function
17 
18 import re
19 import os
20 from .deco import keyword
21 try:
22  from robot.api import logger
23 except ImportError:
24  logger = None
25 
26 from .sshconnectioncache import SSHConnectionCache
27 from .abstractclient import SSHClientException
28 from .client import SSHClient
29 from .config import (Configuration, IntegerEntry, LogLevelEntry, NewlineEntry,
30  StringEntry, TimeEntry)
31 from .utils import ConnectionCache, is_string, is_truthy, plural_or_not
32 from .version import VERSION
33 
34 
35 __version__ = VERSION
36 
37 
442 class SSHLibrary():
443  ROBOT_LIBRARY_SCOPE = 'GLOBAL'
444  ROBOT_LIBRARY_VERSION = __version__
445 
446  DEFAULT_TIMEOUT = '3 seconds'
447  DEFAULT_NEWLINE = 'LF'
448  DEFAULT_PROMPT = None
449  DEFAULT_LOGLEVEL = 'INFO'
450  DEFAULT_TERM_TYPE = 'vt100'
451  DEFAULT_TERM_WIDTH = 80
452  DEFAULT_TERM_HEIGHT = 24
453  DEFAULT_PATH_SEPARATOR = '/'
454  DEFAULT_ENCODING = 'UTF-8'
455  DEFAULT_ESCAPE_ANSI = False
456  DEFAULT_ENCODING_ERRORS = 'strict'
457 
458 
483  def __init__(self,
484  timeout=DEFAULT_TIMEOUT,
485  newline=DEFAULT_NEWLINE,
486  prompt=DEFAULT_PROMPT,
487  loglevel=DEFAULT_LOGLEVEL,
488  term_type=DEFAULT_TERM_TYPE,
489  width=DEFAULT_TERM_WIDTH,
490  height=DEFAULT_TERM_HEIGHT,
491  path_separator=DEFAULT_PATH_SEPARATOR,
492  encoding=DEFAULT_ENCODING,
493  escape_ansi=DEFAULT_ESCAPE_ANSI,
494  encoding_errors=DEFAULT_ENCODING_ERRORS):
495  self._connections_connections = SSHConnectionCache()
497  timeout or self.DEFAULT_TIMEOUTDEFAULT_TIMEOUT,
498  newline or self.DEFAULT_NEWLINEDEFAULT_NEWLINE,
499  prompt or self.DEFAULT_PROMPTDEFAULT_PROMPT,
500  loglevel or self.DEFAULT_LOGLEVELDEFAULT_LOGLEVEL,
501  term_type or self.DEFAULT_TERM_TYPEDEFAULT_TERM_TYPE,
502  width or self.DEFAULT_TERM_WIDTHDEFAULT_TERM_WIDTH,
503  height or self.DEFAULT_TERM_HEIGHTDEFAULT_TERM_HEIGHT,
504  path_separator or self.DEFAULT_PATH_SEPARATORDEFAULT_PATH_SEPARATOR,
505  encoding or self.DEFAULT_ENCODINGDEFAULT_ENCODING,
506  escape_ansi or self.DEFAULT_ESCAPE_ANSIDEFAULT_ESCAPE_ANSI,
507  encoding_errors or self.DEFAULT_ENCODING_ERRORSDEFAULT_ENCODING_ERRORS
508  )
509  self._last_commands_last_commands = dict()
510 
511  @property
512  current = property
513 
514  def current(self):
515  return self._connections_connections.current
516 
517  @keyword(types=None)
518 
548  def set_default_configuration(self, timeout=None, newline=None, prompt=None,
549  loglevel=None, term_type=None, width=None,
550  height=None, path_separator=None,
551  encoding=None, escape_ansi=None, encoding_errors=None):
552  self._config_config.update(timeout=timeout, newline=newline, prompt=prompt,
553  loglevel=loglevel, term_type=term_type, width=width,
554  height=height, path_separator=path_separator,
555  encoding=encoding, escape_ansi=escape_ansi, encoding_errors=encoding_errors)
556 
557 
587  def set_client_configuration(self, timeout=None, newline=None, prompt=None,
588  term_type=None, width=None, height=None,
589  path_separator=None, encoding=None, escape_ansi=None, encoding_errors=None):
590  self.currentcurrentcurrent.config.update(timeout=timeout, newline=newline,
591  prompt=prompt, term_type=term_type,
592  width=width, height=height,
593  path_separator=path_separator,
594  encoding=encoding, escape_ansi=escape_ansi,
595  encoding_errors=encoding_errors)
596 
597 
615  def enable_ssh_logging(self, logfile):
616  if SSHClient.enable_logging(logfile):
617  self._log_log('SSH log is written to <a href="%s">file</a>.' % logfile,
618  'HTML')
619 
620 
683  def open_connection(self, host, alias=None, port=22, timeout=None,
684  newline=None, prompt=None, term_type=None, width=None,
685  height=None, path_separator=None, encoding=None, escape_ansi=None, encoding_errors=None):
686  timeout = timeout or self._config_config.timeout
687  newline = newline or self._config_config.newline
688  prompt = prompt or self._config_config.prompt
689  term_type = term_type or self._config_config.term_type
690  width = width or self._config_config.width
691  height = height or self._config_config.height
692  path_separator = path_separator or self._config_config.path_separator
693  encoding = encoding or self._config_config.encoding
694  escape_ansi = escape_ansi or self._config_config.escape_ansi
695  encoding_errors = encoding_errors or self._config_config.encoding_errors
696  client = SSHClient(host, alias, port, timeout, newline, prompt,
697  term_type, width, height, path_separator, encoding, escape_ansi, encoding_errors)
698  connection_index = self._connections_connections.register(client, alias)
699  client.config.update(index=connection_index)
700  return connection_index
701 
702 
726  def switch_connection(self, index_or_alias):
727  old_index = self._connections_connections.current_index
728  if index_or_alias is None:
729  self.close_connectionclose_connection()
730  else:
731  self._connections_connections.switch(index_or_alias)
732  return old_index
733 
734 
746  def close_connection(self):
747  connections = self._connections_connections
748  connections.close_current()
749 
750 
751 
767  self._connections_connections.close_all()
768 
769 
848  def get_connection(self, index_or_alias=None, index=False, host=False,
849  alias=False, port=False, timeout=False, newline=False,
850  prompt=False, term_type=False, width=False, height=False,
851  encoding=False, escape_ansi=False):
852  if not index_or_alias:
853  index_or_alias = self._connections_connections.current_index
854  try:
855  config = self._connections_connections.get_connection(index_or_alias).config
856  except RuntimeError:
857  config = SSHClient(None).config
858  except AttributeError:
859  config = SSHClient(None).config
860  self._log_log(str(config), self._config_config.loglevel)
861  return_values = tuple(self._get_config_values_get_config_values(config, index, host,
862  alias, port, timeout,
863  newline, prompt,
864  term_type, width, height,
865  encoding, escape_ansi))
866  if not return_values:
867  return config
868  if len(return_values) == 1:
869  return return_values[0]
870  return return_values
871 
872  def _log(self, msg, level='INFO'):
873  level = self._active_loglevel_active_loglevel(level)
874  if level != 'NONE':
875  msg = msg.strip()
876  if not msg:
877  return
878  if logger:
879  logger.write(msg, level)
880  else:
881  print('*%s* %s' % (level, msg))
882 
883  def _active_loglevel(self, level):
884  if level is None:
885  return self._config_config.loglevel
886  if is_string(level) and \
887  level.upper() in ['TRACE', 'DEBUG', 'INFO', 'WARN', 'HTML', 'NONE']:
888  return level.upper()
889  raise AssertionError("Invalid log level '%s'." % level)
890 
891  def _get_config_values(self, config, index, host, alias, port, timeout,
892  newline, prompt, term_type, width, height, encoding, escape_ansi):
893  if is_truthy(index):
894  yield config.index
895  if is_truthy(host):
896  yield config.host
897  if is_truthy(alias):
898  yield config.alias
899  if is_truthy(port):
900  yield config.port
901  if is_truthy(timeout):
902  yield config.timeout
903  if is_truthy(newline):
904  yield config.newline
905  if is_truthy(prompt):
906  yield config.prompt
907  if is_truthy(term_type):
908  yield config.term_type
909  if is_truthy(width):
910  yield config.width
911  if is_truthy(height):
912  yield config.height
913  if is_truthy(encoding):
914  yield config.encoding
915  if is_truthy(escape_ansi):
916  yield config.escape_ansi
917 
918 
935  def get_connections(self):
936  configs = [c.config for c in self._connections_connections._connections if c]
937  for c in configs:
938  self._log_log(str(c), self._config_config.loglevel)
939  return configs
940 
941 
1002  def login(self, username=None, password=None, allow_agent=False, look_for_keys=False, delay='0.5 seconds',
1003  proxy_cmd=None, read_config=False, jumphost_index_or_alias=None, keep_alive_interval='0 seconds'):
1004  jumphost_connection_conf = self.get_connectionget_connection(index_or_alias=jumphost_index_or_alias) \
1005  if jumphost_index_or_alias else None
1006  jumphost_connection = self._connections_connections.connections[jumphost_connection_conf.index-1] \
1007  if jumphost_connection_conf and jumphost_connection_conf.index else None
1008 
1009  return self._login_login(self.currentcurrentcurrent.login, username, password, is_truthy(allow_agent),
1010  is_truthy(look_for_keys), delay, proxy_cmd, is_truthy(read_config),
1011  jumphost_connection, keep_alive_interval)
1012 
1013 
1070  def login_with_public_key(self, username=None, keyfile=None, password='',
1071  allow_agent=False, look_for_keys=False,
1072  delay='0.5 seconds', proxy_cmd=None,
1073  jumphost_index_or_alias=None,
1074  read_config=False, keep_alive_interval='0 seconds'):
1075  if proxy_cmd and jumphost_index_or_alias:
1076  raise ValueError("`proxy_cmd` and `jumphost_connection` are mutually exclusive SSH features.")
1077  jumphost_connection_conf = self.get_connectionget_connection(index_or_alias=jumphost_index_or_alias) if jumphost_index_or_alias else None
1078  jumphost_connection = self._connections_connections.connections[jumphost_connection_conf.index-1] if jumphost_connection_conf and jumphost_connection_conf.index else None
1079  return self._login_login(self.currentcurrentcurrent.login_with_public_key, username,
1080  keyfile, password, is_truthy(allow_agent),
1081  is_truthy(look_for_keys), delay, proxy_cmd,
1082  jumphost_connection, is_truthy(read_config), keep_alive_interval)
1083 
1084  def _login(self, login_method, username, *args):
1085  self._log_log("Logging into '%s:%s' as '%s'."
1086  % (self.currentcurrentcurrent.config.host, self.currentcurrentcurrent.config.port,
1087  username), self._config_config.loglevel)
1088  try:
1089  login_output = login_method(username, *args)
1090  if is_truthy(self.currentcurrentcurrent.config.escape_ansi):
1091  login_output = self._escape_ansi_sequences_escape_ansi_sequences(login_output)
1092  self._log_log('Read output: %s' % login_output, self._config_config.loglevel)
1093  return login_output
1094  except SSHClientException as e:
1095  raise RuntimeError(e)
1096 
1097 
1120  def get_pre_login_banner(self, host=None, port=22):
1121  if host:
1122  banner = SSHClient.get_banner_without_login(host, port)
1123  elif self.currentcurrentcurrent:
1124  banner = self.currentcurrentcurrent.get_banner()
1125  else:
1126  raise RuntimeError("'host' argument is mandatory if there is no open connection.")
1127  return banner.decode(self.DEFAULT_ENCODINGDEFAULT_ENCODING)
1128 
1129 
1198  def execute_command(self, command, return_stdout=True, return_stderr=False,
1199  return_rc=False, sudo=False, sudo_password=None, timeout=None, output_during_execution=False,
1200  output_if_timeout=False, invoke_subsystem=False, forward_agent=False):
1201  if not is_truthy(sudo):
1202  self._log_log("Executing command '%s'." % command, self._config_config.loglevel)
1203  else:
1204  self._log_log("Executing command 'sudo %s'." % command, self._config_config.loglevel)
1205  opts = self._legacy_output_options_legacy_output_options(return_stdout, return_stderr,
1206  return_rc)
1207  stdout, stderr, rc = self.currentcurrentcurrent.execute_command(command, sudo, sudo_password,
1208  timeout, output_during_execution, output_if_timeout,
1209  is_truthy(invoke_subsystem), forward_agent)
1210  return self._return_command_output_return_command_output(stdout, stderr, rc, *opts)
1211 
1212 
1254  def start_command(self, command, sudo=False, sudo_password=None, invoke_subsystem=False, forward_agent=False):
1255  if not is_truthy(sudo):
1256  self._log_log("Starting command '%s'." % command, self._config_config.loglevel)
1257  else:
1258  self._log_log("Starting command 'sudo %s'." % command, self._config_config.loglevel)
1259  if self.currentcurrentcurrent.config.index not in self._last_commands_last_commands.keys():
1260  self._last_commands_last_commands[self.currentcurrentcurrent.config.index] = command
1261  else:
1262  temp_dict = {self.currentcurrentcurrent.config.index: command}
1263  self._last_commands_last_commands.update(temp_dict)
1264  self.currentcurrentcurrent.start_command(command, sudo, sudo_password, is_truthy(invoke_subsystem), is_truthy(forward_agent))
1265 
1266 
1313  def read_command_output(self, return_stdout=True, return_stderr=False,
1314  return_rc=False, timeout=None):
1315  self._log_log("Reading output of command '%s'." % self._last_commands_last_commands.get(self.currentcurrentcurrent.config.index), self._config_config.loglevel)
1316  opts = self._legacy_output_options_legacy_output_options(return_stdout, return_stderr,
1317  return_rc)
1318  try:
1319  stdout, stderr, rc = self.currentcurrentcurrent.read_command_output(timeout=timeout)
1320  except SSHClientException as msg:
1321  raise RuntimeError(msg)
1322  return self._return_command_output_return_command_output(stdout, stderr, rc, *opts)
1323 
1324 
1353  def create_local_ssh_tunnel(self, local_port, remote_host, remote_port=22, bind_address=None):
1354  self.currentcurrentcurrent.create_local_ssh_tunnel(local_port, remote_host, remote_port, bind_address)
1355 
1356  def _legacy_output_options(self, stdout, stderr, rc):
1357  if not is_string(stdout):
1358  return stdout, stderr, rc
1359  stdout = stdout.lower()
1360  if stdout == 'stderr':
1361  return False, True, rc
1362  if stdout == 'both':
1363  return True, True, rc
1364  return stdout, stderr, rc
1365 
1366  def _return_command_output(self, stdout, stderr, rc, return_stdout,
1367  return_stderr, return_rc):
1368  self._log_log("Command exited with return code %d." % rc, self._config_config.loglevel)
1369  ret = []
1370  if is_truthy(return_stdout):
1371  ret.append(stdout.rstrip('\n'))
1372  if is_truthy(return_stderr):
1373  ret.append(stderr.rstrip('\n'))
1374  if is_truthy(return_rc):
1375  ret.append(rc)
1376  if len(ret) == 1:
1377  return ret[0]
1378  return ret
1379 
1380 
1403  def write(self, text, loglevel=None):
1404  self._write_write(text, add_newline=True)
1405  return self._read_and_log_read_and_log(loglevel, self.currentcurrentcurrent.read_until_newline)
1406 
1407 
1423  def write_bare(self, text):
1424  self._write_write(text)
1425 
1426  def _write(self, text, add_newline=False):
1427  try:
1428  self.currentcurrentcurrent.write(text, is_truthy(add_newline))
1429  except SSHClientException as e:
1430  raise RuntimeError(e)
1431 
1432 
1455  def write_until_expected_output(self, text, expected, timeout,
1456  retry_interval, loglevel=None):
1457  self._read_and_log_read_and_log(loglevel, self.currentcurrentcurrent.write_until_expected, text,
1458  expected, timeout, retry_interval)
1459 
1460 
1486  def read(self, loglevel=None, delay=None):
1487  return self._read_and_log_read_and_log(loglevel, self.currentcurrentcurrent.read, delay)
1488 
1489 
1512  def read_until(self, expected, loglevel=None):
1513  return self._read_and_log_read_and_log(loglevel, self.currentcurrentcurrent.read_until, expected)
1514 
1515 
1549  def read_until_prompt(self, loglevel=None, strip_prompt=False):
1550  return self._read_and_log_read_and_log(loglevel, self.currentcurrentcurrent.read_until_prompt, is_truthy(strip_prompt))
1551 
1552 
1579  def read_until_regexp(self, regexp, loglevel=None):
1580  return self._read_and_log_read_and_log(loglevel, self.currentcurrentcurrent.read_until_regexp,
1581  regexp)
1582 
1583  def _read_and_log(self, loglevel, reader, *args):
1584  try:
1585  output = reader(*args)
1586  except SSHClientException as e:
1587  if is_truthy(self.currentcurrentcurrent.config.escape_ansi):
1588  message = self._escape_ansi_sequences_escape_ansi_sequences(e.args[0])
1589  raise RuntimeError(message)
1590  raise RuntimeError(e)
1591  if is_truthy(self.currentcurrentcurrent.config.escape_ansi):
1592  output = self._escape_ansi_sequences_escape_ansi_sequences(output)
1593  self._log_log(output, loglevel)
1594  return output
1595 
1596  @staticmethod
1598  ansi_escape = re.compile(r'(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]', flags=re.IGNORECASE)
1599  output = ansi_escape.sub('', output)
1600  return ("%r" % output)[1:-1].encode().decode('unicode-escape')
1601 
1602 
1654  def get_file(self, source, destination='.', scp='OFF', scp_preserve_times=False):
1655  return self._run_command_run_command(self.currentcurrentcurrent.get_file, source,
1656  destination, scp, scp_preserve_times)
1657 
1658 
1702  def get_directory(self, source, destination='.', recursive=False,
1703  scp='OFF', scp_preserve_times=False):
1704  return self._run_command_run_command(self.currentcurrentcurrent.get_directory, source,
1705  destination, is_truthy(recursive), scp, scp_preserve_times)
1706 
1707 
1764  def put_file(self, source, destination='.', mode='0744', newline='',
1765  scp='OFF', scp_preserve_times=False):
1766  return self._run_command_run_command(self.currentcurrentcurrent.put_file, source,
1767  destination, mode, newline, scp, scp_preserve_times)
1768 
1769 
1820  def put_directory(self, source, destination='.', mode='0744', newline='',
1821  recursive=False, scp='OFF', scp_preserve_times=False):
1822  return self._run_command_run_command(self.currentcurrentcurrent.put_directory, source,
1823  destination, mode, newline,
1824  is_truthy(recursive), scp, scp_preserve_times)
1825 
1826  def _run_command(self, command, *args):
1827  try:
1828  files = command(*args)
1829  except SSHClientException as e:
1830  raise RuntimeError(e)
1831  if files:
1832  for src, dst in files:
1833  self._log_log("'%s' -> '%s'" % (src, dst), self._config_config.loglevel)
1834 
1835 
1846  def file_should_exist(self, path):
1847  if not self.currentcurrentcurrent.is_file(path):
1848  raise AssertionError("File '%s' does not exist." % path)
1849 
1850 
1861  def file_should_not_exist(self, path):
1862  if self.currentcurrentcurrent.is_file(path):
1863  raise AssertionError("File '%s' exists." % path)
1864 
1865 
1876  def directory_should_exist(self, path):
1877  if not self.currentcurrentcurrent.is_dir(path):
1878  raise AssertionError("Directory '%s' does not exist." % path)
1879 
1880 
1892  if self.currentcurrentcurrent.is_dir(path):
1893  raise AssertionError("Directory '%s' exists." % path)
1894 
1895 
1923  def list_directory(self, path, pattern=None, absolute=False):
1924  try:
1925  items = self.currentcurrentcurrent.list_dir(path, pattern, is_truthy(absolute))
1926  except SSHClientException as msg:
1927  raise RuntimeError(msg)
1928  self._log_log('%d item%s:\n%s' % (len(items), plural_or_not(items),
1929  '\n'.join(items)), self._config_config.loglevel)
1930  return items
1931 
1932 
1933  def list_files_in_directory(self, path, pattern=None, absolute=False):
1934  absolute = is_truthy(absolute)
1935  try:
1936  files = self.currentcurrentcurrent.list_files_in_dir(path, pattern, absolute)
1937  except SSHClientException as msg:
1938  raise RuntimeError(msg)
1939  files = self.currentcurrentcurrent.list_files_in_dir(path, pattern, absolute)
1940  self._log_log('%d file%s:\n%s' % (len(files), plural_or_not(files),
1941  '\n'.join(files)), self._config_config.loglevel)
1942  return files
1943 
1944 
1945  def list_directories_in_directory(self, path, pattern=None, absolute=False):
1946  try:
1947  dirs = self.currentcurrentcurrent.list_dirs_in_dir(path, pattern, is_truthy(absolute))
1948  except SSHClientException as msg:
1949  raise RuntimeError(msg)
1950  self._log_log('%d director%s:\n%s' % (len(dirs),
1951  'y' if len(dirs) == 1 else 'ies',
1952  '\n'.join(dirs)), self._config_config.loglevel)
1953  return dirs
1954 
1955 
1957 
1958  def __init__(self, timeout, newline, prompt, loglevel, term_type, width,
1959  height, path_separator, encoding, escape_ansi, encoding_errors):
1960  super(_DefaultConfiguration, self).__init__(
1961  timeout=TimeEntry(timeout),
1962  newline=NewlineEntry(newline),
1963  prompt=StringEntry(prompt),
1964  loglevel=LogLevelEntry(loglevel),
1965  term_type=StringEntry(term_type),
1966  width=IntegerEntry(width),
1967  height=IntegerEntry(height),
1968  path_separator=StringEntry(path_separator),
1969  encoding=StringEntry(encoding),
1970  escape_ansi=StringEntry(escape_ansi),
1971  encoding_errors=StringEntry(encoding_errors)
1972  )
A simple configuration class.
Definition: config.py:40
Integer value to be stored in stored in :py:class:Configuration.
Definition: config.py:113
Log level to be stored in :py:class:Configuration.
Definition: config.py:138
New line sequence to be stored in :py:class:Configuration.
Definition: config.py:154
String value to be stored in :py:class:Configuration.
Definition: config.py:102
Time string to be stored in :py:class:Configuration.
Definition: config.py:124
SSHLibrary is a Robot Framework test library for SSH and SFTP.
Definition: library.py:442
def close_connection(self)
Closes the current connection.
Definition: library.py:746
def write(self, text, loglevel=None)
Writes the given text on the remote machine and appends a newline.
Definition: library.py:1403
def _legacy_output_options(self, stdout, stderr, rc)
Definition: library.py:1356
def set_default_configuration(self, timeout=None, newline=None, prompt=None, loglevel=None, term_type=None, width=None, height=None, path_separator=None, encoding=None, escape_ansi=None, encoding_errors=None)
Update the default configuration.
Definition: library.py:551
def read_until(self, expected, loglevel=None)
Consumes and returns the server output until expected is encountered.
Definition: library.py:1512
def put_directory(self, source, destination='.', mode='0744', newline='', recursive=False, scp='OFF', scp_preserve_times=False)
Uploads a directory, including its content, from the local machine to the remote machine.
Definition: library.py:1821
def _escape_ansi_sequences(output)
Definition: library.py:1597
def read(self, loglevel=None, delay=None)
Consumes and returns everything available on the server output.
Definition: library.py:1486
def _read_and_log(self, loglevel, reader, *args)
Definition: library.py:1583
def login(self, username=None, password=None, allow_agent=False, look_for_keys=False, delay='0.5 seconds', proxy_cmd=None, read_config=False, jumphost_index_or_alias=None, keep_alive_interval='0 seconds')
Logs into the SSH server with the given username and password.
Definition: library.py:1003
def get_connections(self)
Returns information about all the open connections.
Definition: library.py:935
def _write(self, text, add_newline=False)
Definition: library.py:1426
def list_directories_in_directory(self, path, pattern=None, absolute=False)
A wrapper for List Directory that returns only directories.
Definition: library.py:1945
def write_bare(self, text)
Writes the given text on the remote machine without appending a newline.
Definition: library.py:1423
def _return_command_output(self, stdout, stderr, rc, return_stdout, return_stderr, return_rc)
Definition: library.py:1367
def put_file(self, source, destination='.', mode='0744', newline='', scp='OFF', scp_preserve_times=False)
Uploads file(s) from the local machine to the remote machine.
Definition: library.py:1765
def read_until_prompt(self, loglevel=None, strip_prompt=False)
Consumes and returns the server output until the prompt is found.
Definition: library.py:1549
def read_until_regexp(self, regexp, loglevel=None)
Consumes and returns the server output until a match to regexp is found.
Definition: library.py:1579
def switch_connection(self, index_or_alias)
Switches the active connection by index or alias.
Definition: library.py:726
def set_client_configuration(self, timeout=None, newline=None, prompt=None, term_type=None, width=None, height=None, path_separator=None, encoding=None, escape_ansi=None, encoding_errors=None)
Update the configuration of the current connection.
Definition: library.py:589
def _get_config_values(self, config, index, host, alias, port, timeout, newline, prompt, term_type, width, height, encoding, escape_ansi)
Definition: library.py:892
def directory_should_not_exist(self, path)
Fails if the given path points to an existing directory.
Definition: library.py:1891
def get_file(self, source, destination='.', scp='OFF', scp_preserve_times=False)
Downloads file(s) from the remote machine to the local machine.
Definition: library.py:1654
def close_all_connections(self)
Closes all open connections.
Definition: library.py:766
def list_files_in_directory(self, path, pattern=None, absolute=False)
A wrapper for List Directory that returns only files.
Definition: library.py:1933
def enable_ssh_logging(self, logfile)
Enables logging of SSH protocol output to given logfile.
Definition: library.py:615
def _log(self, msg, level='INFO')
Definition: library.py:872
def file_should_exist(self, path)
Fails if the given path does NOT point to an existing file.
Definition: library.py:1846
def _active_loglevel(self, level)
Definition: library.py:883
def login_with_public_key(self, username=None, keyfile=None, password='', allow_agent=False, look_for_keys=False, delay='0.5 seconds', proxy_cmd=None, jumphost_index_or_alias=None, read_config=False, keep_alive_interval='0 seconds')
Logs into the SSH server using key-based authentication.
Definition: library.py:1074
def open_connection(self, host, alias=None, port=22, timeout=None, newline=None, prompt=None, term_type=None, width=None, height=None, path_separator=None, encoding=None, escape_ansi=None, encoding_errors=None)
Opens a new SSH connection to the given host and port.
Definition: library.py:685
def get_directory(self, source, destination='.', recursive=False, scp='OFF', scp_preserve_times=False)
Downloads a directory, including its content, from the remote machine to the local machine.
Definition: library.py:1703
def get_connection(self, index_or_alias=None, index=False, host=False, alias=False, port=False, timeout=False, newline=False, prompt=False, term_type=False, width=False, height=False, encoding=False, escape_ansi=False)
Returns information about the connection.
Definition: library.py:851
def file_should_not_exist(self, path)
Fails if the given path points to an existing file.
Definition: library.py:1861
def read_command_output(self, return_stdout=True, return_stderr=False, return_rc=False, timeout=None)
Returns outputs of the most recent started command.
Definition: library.py:1314
def __init__(self, timeout=DEFAULT_TIMEOUT, newline=DEFAULT_NEWLINE, prompt=DEFAULT_PROMPT, loglevel=DEFAULT_LOGLEVEL, term_type=DEFAULT_TERM_TYPE, width=DEFAULT_TERM_WIDTH, height=DEFAULT_TERM_HEIGHT, path_separator=DEFAULT_PATH_SEPARATOR, encoding=DEFAULT_ENCODING, escape_ansi=DEFAULT_ESCAPE_ANSI, encoding_errors=DEFAULT_ENCODING_ERRORS)
SSHLibrary allows some import time configuration.
Definition: library.py:494
def _run_command(self, command, *args)
Definition: library.py:1826
def list_directory(self, path, pattern=None, absolute=False)
Returns and logs items in the remote path, optionally filtered with pattern.
Definition: library.py:1923
def directory_should_exist(self, path)
Fails if the given path does not point to an existing directory.
Definition: library.py:1876
def create_local_ssh_tunnel(self, local_port, remote_host, remote_port=22, bind_address=None)
The keyword uses the existing connection to set up local port forwarding (the openssh -L option) from...
Definition: library.py:1353
def _login(self, login_method, username, *args)
Definition: library.py:1084
def execute_command(self, command, return_stdout=True, return_stderr=False, return_rc=False, sudo=False, sudo_password=None, timeout=None, output_during_execution=False, output_if_timeout=False, invoke_subsystem=False, forward_agent=False)
Executes command on the remote machine and returns its outputs.
Definition: library.py:1200
def start_command(self, command, sudo=False, sudo_password=None, invoke_subsystem=False, forward_agent=False)
Starts execution of the command on the remote machine and returns immediately.
Definition: library.py:1254
def get_pre_login_banner(self, host=None, port=22)
Returns the banner supplied by the server upon connect.
Definition: library.py:1120
def write_until_expected_output(self, text, expected, timeout, retry_interval, loglevel=None)
Writes the given text repeatedly until expected appears in the server output.
Definition: library.py:1456
def __init__(self, timeout, newline, prompt, loglevel, term_type, width, height, path_separator, encoding, escape_ansi, encoding_errors)
Definition: library.py:1959