17 from com.trilead.ssh2
import (Connection, SCPClient
as JavaSCPClient,
18 SFTPException, SFTPv3Client,
19 SFTPv3DirectoryEntry, StreamGobbler)
22 'Importing Trilead SSH library failed. '
23 'Make sure you have the Trilead JAR distribution in your CLASSPATH.'
27 from java.io
import (BufferedReader, File, FileOutputStream, InputStreamReader,
30 from .abstractclient
import (AbstractShell, AbstractSSHClient,
31 AbstractSFTPClient, AbstractCommand,
32 SSHClientException, SFTPFileInfo)
34 from robot.api
import logger
46 condition = _shell.waitForCondition(rc , int(timeout) * 1000)
48 if condition & timeout_condition != 0:
49 raise SSHClientException(
"Timed out in %s seconds" % int(timeout))
54 client = Connection(self.
configconfig.host, self.
configconfig.port)
55 timeout = int(float(self.
configconfig.timeout)*1000)
56 client.connect(
None, timeout, timeout)
63 def _login(self, username, password, allow_agent='ignored', look_for_keys='ignored',
64 proxy_cmd=None, jumphost_alias_or_index=None, read_config=False, keep_alive_interval=None):
65 if allow_agent
or look_for_keys
or keep_alive_interval:
67 "`jumphost_index_or_alias` and `keep_alive_interval`"
68 " do not work with Jython.")
70 auth = self.
clientclient.authenticateWithPassword(username, password)
if password \
71 else self.
clientclient.authenticateWithNone(username)
73 raise SSHClientException
76 allow_agent='ignored', look_for_keys='ignored',
77 proxy_cmd=None, jumphost_alias_or_index=None,
78 read_config=False, keep_alive_interval=None):
79 if allow_agent
or look_for_keys
or keep_alive_interval:
81 "`jumphost_index_or_alias` and `keep_alive_interval`"
82 " do not work with Jython.")
84 success = self.
clientclient.authenticateWithPublicKey(username,
88 raise SSHClientException
91 raise SSHClientException
93 def _start_command(self, command, sudo=False, sudo_password=None, invoke_subsystem=False, forward_agent=False):
94 new_shell = self.
clientclient.openSession()
96 new_shell.requestDumbPTY()
98 cmd.run_in(new_shell, sudo, sudo_password, invoke_subsystem)
115 self.
clientclient.createLocalPortForwarder(int(local_port), remote_host, int(remote_port))
116 logger.info(
"Now forwarding port %s to %s:%s ..." % (local_port, remote_host, remote_port))
121 def __init__(self, client, term_type, term_width, term_height):
122 shell = client.openSession()
123 shell.requestPTY(term_type, term_width, term_height, 0, 0,
None)
133 return ''.join(chr(b & 0xFF)
for b
in read_bytes)
143 logger.warn(
'Setting width or height is not supported with Jython.')
146 return self.
_stdout_stdout.available()
156 self.
_client_client = SFTPv3Client(ssh_client)
157 self.
_client_client.setCharset(encoding)
158 super(SFTPClient, self).
__init__(encoding)
161 for item
in self.
_client_client.ls(path):
162 if item.filename
not in (
'.',
'..'):
163 yield SFTPFileInfo(item.filename, item.attributes.permissions)
166 attributes = self.
_client_client.stat(path)
170 remote_file = self.
_client_client.createFile(destination)
172 file_stat = self.
_client_client.fstat(remote_file)
173 file_stat.permissions = mode
174 self.
_client_client.fsetstat(remote_file, file_stat)
175 except SFTPException:
180 self.
_client_client.write(remote_file, position, data, 0, len(data))
183 self.
_client_client.closeFile(remote_file)
185 def _get_file(self, remote_path, local_path, scp_preserve_times):
186 local_file = FileOutputStream(local_path)
187 remote_file_size = self.
_client_client.stat(remote_path).size
188 remote_file = self.
_client_client.openFileRO(remote_path)
189 array_size_bytes = 4096
190 data = jarray.zeros(array_size_bytes,
'b')
193 read_bytes = self.
_client_client.read(remote_file, offset, data, 0,
195 data_length = len(data)
198 if remote_file_size - offset < array_size_bytes:
199 data_length = remote_file_size - offset
200 local_file.write(data, 0, data_length)
201 offset += data_length
202 self.
_client_client.closeFile(remote_file)
207 return self.
_client_client.canonicalPath(path)
210 return self.
_client_client.readLink(path)
218 self.
_scp_client_scp_client.put(source, destination)
221 self.
_scp_client_scp_client.get(source, destination)
225 '`scp=TRANSFER` or `scp=OFF`.')
229 '`scp=TRANSFER` or `scp=OFF`.')
236 super(SCPTransferClient, self).
__init__(ssh_client, encoding)
238 def _put_file(self, source, destination, mode, newline, path_separator, scp_preserve_times):
240 self.
_scp_client_scp_client.put(source, destination.rsplit(path_separator, 1)[0])
242 def _get_file(self, remote_path, local_path, scp_preserve_times):
243 self.
_scp_client_scp_client.get(remote_path, local_path.rsplit(os.sep, 1)[0])
253 rc = self.
_shell_shell.getExitStatus()
or 0
255 return stdout, stderr, rc
258 reader = BufferedReader(InputStreamReader(StreamGobbler(stream),
261 line = reader.readLine()
262 while line
is not None:
263 result += line +
'\n'
264 line = reader.readLine()
269 self.
_shell_shell.execCommand(command)
273 if sudo_password
is None:
274 self.
_shell_shell.execCommand(command)
276 self.
_shell_shell.execCommand(
'echo %s | sudo --stdin --prompt "" %s' % (sudo_password, command))
280 self.
_shell_shell.startSubSystem(command)
Base class for the remote command.
Base class for the SFTP implementation.
def _create_remote_file(self, destination, mode)
Base class for the SSH client implementation.
Base class for the shell implementation.
Wrapper class for the language specific file information objects.
def _create_scp_all_client(self)
def _start_command(self, command, sudo=False, sudo_password=None, invoke_subsystem=False, forward_agent=False)
def _create_sftp_client(self)
def _login(self, username, password, allow_agent='ignored', look_for_keys='ignored', proxy_cmd=None, jumphost_alias_or_index=None, read_config=False, keep_alive_interval=None)
def _create_scp_transfer_client(self)
def enable_logging(logfile)
Enables logging of SSH events to a file.
def _login_with_public_key(self, username, key_file, password, allow_agent='ignored', look_for_keys='ignored', proxy_cmd=None, jumphost_alias_or_index=None, read_config=False, keep_alive_interval=None)
def create_local_ssh_tunnel(self, local_port, remote_host, remote_port, *args)
def _read_from_stream(self, stream)
def _execute_with_sudo(self, sudo_password=None)
def read_outputs(self, timeout=None, *args)
def put_directory(self, source, destination, *args)
def __init__(self, ssh_client)
def get_directory(self, source, destination, *args)
def get_file(self, source, destination, *args)
def put_file(self, source, destination, *args)
def _put_file(self, source, destination, mode, newline, path_separator, scp_preserve_times)
def _get_file(self, remote_path, local_path, scp_preserve_times)
def __init__(self, ssh_client, encoding)
def __init__(self, ssh_client, encoding)
def _close_remote_file(self, remote_file)
def _readlink(self, path)
def _get_file(self, remote_path, local_path, scp_preserve_times)
def _absolute_path(self, path)
def _write_to_remote_file(self, remote_file, data, position)
def _create_remote_file(self, destination, mode)
def read_byte(self)
Reads a single byte from the shell.
def _output_available(self)
def read(self)
Reads all the output from the shell.
def __init__(self, client, term_type, term_width, term_height)
def resize(width, height)
def write(self, text)
Writes the text in the current shell.
def _wait_until_timeout(_shell, timeout)