46
48
return "".join(stdout.readlines())
48
def copy(self, localpath, remotepath):
49
"""Copy a file through sftp from the localpath to the remotepath"""
50
if not os.path.isfile(localpath):
50
def put(self, local_path, remote_path):
51
"""Copy a file through sftp from the local_path to the remote_path"""
52
if not os.path.isfile(local_path):
51
53
raise RuntimeError("File to copy does not exist")
53
55
sftp = self.client.open_sftp()
54
sftp.put(localpath, remotepath)
56
sftp.put(local_path, remote_path)
58
60
class LinuxRemoteHelper(SSH):
59
61
"""This class executes remote linux commands through the ssh connection"""
61
def remove_remote_dir(self, dir_name):
64
config_stack = config.get_device_config_stack()
65
host = config_stack.get('ssh_ip')
66
user = config_stack.get('ssh_user')
67
password = config_stack.get('ssh_password')
68
super(LinuxRemoteHelper, self).__init__(host, user, password)
70
def remove_dir(self, dir_name):
62
71
self.run('rm -rf {}'.format(dir_name))
64
def create_remote_dir(self, dir_name):
73
def create_dir(self, dir_name):
65
74
self.run('mkdir {}'.format(dir_name))
67
def remove_remote_file(self, file_name):
76
def remove_file(self, file_name):
68
77
self.run('rm -f {}'.format(file_name))
70
79
def copy_file(self, remote_from, remote_to):
78
87
self.run('cp -rf {rfrom} {rto}'.format(rfrom=remote_from,
90
def create_random_file(self, file_path, kb=0, mb=0, gb=0):
91
""" Create a file with random data in the remote host
92
:param file_path: The path to store the file
93
:param kb: The kilobytes of the file
94
:param mb: The megabytes of the file
95
:param gb: The gigabytes fo the file
96
:return: The file path to the created file with random content and size
97
equals to kb kilobytes + mb megabytes + gb gigabytes
99
count = kb + mb * 1024 + gb * 1024 * 1024
100
self.run('dd if=/dev/urandom of={file} bs=1024 count={count}'.
101
format(file=file_path, count=count))
103
def get_sha1(self, file_path):
104
"""Calculate the sha1 digest to the specified file"""
105
res = self.run("sha1sum {file}".format(file=file_path))
106
return res.split()[0]