Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
allow_agent=False,
look_for_keys=False,
gss_auth=False)
scp = None
while True:
source, destination, item, items_done = rc._queue.get()
destination = local(destination)
destination_dir = local(destination.dirname)
destination_dir.ensure(dir=True)
if not destination.check():
if scp is None:
scp = SCPClient(client.get_transport())
try:
scp.get(source, destination.strpath)
except SCPException:
# remote destination didn't exist
pass
except (SSHException, socket.timeout):
# SSH blew up :(
rc._queue.put((source, destination, item, items_done))
rc._queue.task_done()
continue
rc._progress_update(item, items_done)
rc._queue.task_done()
key_stream.close()
server_info_log(server['ip'], server['comment'], ', '.join(server_users))
except paramiko.ssh_exception.PasswordRequiredException:
server_error_log(
server['ip'],
server['comment'],
'The private key file is protected by a passphrase, which is currently not supported.'
)
except paramiko.ssh_exception.AuthenticationException:
server_error_log(
server['ip'],
server['comment'],
'Cannot connect to server because of an authentication problem.'
)
except scp.SCPException:
server_error_log(server['ip'], server['comment'], 'Cannot send file to server.')
except (paramiko.ssh_exception.NoValidConnectionsError, paramiko.ssh_exception.SSHException):
server_error_log(server['ip'], server['comment'], 'Cannot connect to server.')
except socket.timeout:
server_error_log(server['ip'], server['comment'], 'Cannot connect to server because of a timeout.')
else:
server_error_log(server['ip'], server['comment'], 'No user mentioned in configuration file!')
# read scp response
msg = b''
try:
msg = self.channel.recv(512)
except SocketTimeout:
raise SCPException('Timeout waiting for scp response')
# slice off the first byte, so this compare will work in py2 and py3
if msg and msg[0:1] == b'\x00':
return
elif msg and msg[0:1] == b'\x01':
raise SCPException(asunicode(msg[1:]))
elif self.channel.recv_stderr_ready():
msg = self.channel.recv_stderr(512)
raise SCPException(asunicode(msg))
elif not msg:
raise SCPException('No response from server')
else:
raise SCPException('Invalid response from server', msg)
# loop over scp commands, and receive as necessary
command = {b'C': self._recv_file,
b'T': self._set_time,
b'D': self._recv_pushd,
b'E': self._recv_popd}
while not self.channel.closed:
# wait for command as long as we're open
self.channel.sendall('\x00')
msg = self.channel.recv(1024)
if not msg: # chan closed while recving
break
assert msg[-1:] == b'\n'
msg = msg[:-1]
code = msg[0:1]
if code not in command:
raise SCPException(asunicode(msg[1:]))
command[code](msg[1:])
# directory times can't be set until we're done writing files
self._set_dirtimes()
try:
msg = self.channel.recv(512)
except SocketTimeout:
raise SCPException('Timeout waiting for scp response')
# slice off the first byte, so this compare will work in py2 and py3
if msg and msg[0:1] == b'\x00':
return
elif msg and msg[0:1] == b'\x01':
raise SCPException(asunicode(msg[1:]))
elif self.channel.recv_stderr_ready():
msg = self.channel.recv_stderr(512)
raise SCPException(asunicode(msg))
elif not msg:
raise SCPException('No response from server')
else:
raise SCPException('Invalid response from server', msg)
path = os.path.join(asbytes(self._recv_dir), name)
self._depth += 1
except:
self.channel.send(b'\x01')
raise SCPException('Bad directory format')
try:
if not os.path.exists(path):
os.mkdir(path, mode)
elif os.path.isdir(path):
os.chmod(path, mode)
else:
raise SCPException('%s: Not a directory' % path)
self._dirtimes[path] = (self._utime)
self._utime = None
self._recv_dir = path
except (OSError, SCPException) as e:
self.channel.send(b'\x01' + asbytes(str(e)))
raise
if self._rename:
path = self._recv_dir
self._rename = False
elif os.name == 'nt':
name = parts[2].decode('utf-8')
assert not os.path.isabs(name)
path = os.path.join(asunicode_win(self._recv_dir), name)
self._depth += 1
else:
name = parts[2]
assert not os.path.isabs(name)
path = os.path.join(asbytes(self._recv_dir), name)
self._depth += 1
except:
self.channel.send(b'\x01')
raise SCPException('Bad directory format')
try:
if not os.path.exists(path):
os.mkdir(path, mode)
elif os.path.isdir(path):
os.chmod(path, mode)
else:
raise SCPException('%s: Not a directory' % path)
self._dirtimes[path] = (self._utime)
self._utime = None
self._recv_dir = path
except (OSError, SCPException) as e:
self.channel.send(b'\x01' + asbytes(str(e)))
raise
and directories.
@type preserve_times: bool
"""
if not isinstance(remote_path, (list, tuple)):
remote_path = [remote_path]
remote_path = [self.sanitize(asbytes(r)) for r in remote_path]
self._recv_dir = local_path or os.getcwd()
self._depth = 0
self._rename = (len(remote_path) == 1 and
not os.path.isdir(os.path.abspath(local_path)))
if len(remote_path) > 1:
if not os.path.exists(self._recv_dir):
raise SCPException("Local path '%s' does not exist" %
asunicode(self._recv_dir))
elif not os.path.isdir(self._recv_dir):
raise SCPException("Local path '%s' is not a directory" %
asunicode(self._recv_dir))
rcsv = (b'', b' -r')[recursive]
prsv = (b'', b' -p')[preserve_times]
self.channel = self._open()
self._pushed = 0
self.channel.settimeout(self.socket_timeout)
self.channel.exec_command(b"scp" +
rcsv +
prsv +
b" -f " +
b' '.join(remote_path))
self._recv_all()
self.close()
def copy_crashes_dir_with_scp(ssh):
parent_dir_of_crashes_dir = os.path.dirname(config.crashes_dir)
try:
scp = SCPClient(ssh.get_transport())
scp.get(remote_path=config.remote_crashes_dir, local_path=parent_dir_of_crashes_dir, recursive=True,
preserve_times=True)
print('successfully fetched!!')
except SCPException as e:
print(f'Operation error: {e}')
except SocketTimeout:
"""
the fetcher will need multiple attempts if the ssh connection is bad and/or the copy dir is big
"""
print('SocketTimeout')
except PipeTimeout as pipetimeout:
print(f'timeout was reached on a read from a buffered Pipe: {pipetimeout}')
finally:
scp.close()