1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
14 def load_json_file(full_path):
15 """Loads JSON from file
16 :param target_filename:
19 if not os.path.isfile(full_path):
20 raise Exception('File(%s) does not exist' % full_path)
22 with open(full_path, 'r') as file:
23 return json.load(file)
26 def write_json_file(full_path, data):
27 """write JSON from file
28 :param target_filename:
32 with open(full_path, 'w+') as file:
33 file.write(json.dumps(data))
36 class SSHClient(object):
37 def __init__(self, ip, username, password=None, pkey=None,
38 key_filename=None, log=None, look_for_keys=False,
40 self.client = paramiko.SSHClient()
41 self.client.load_system_host_keys()
42 self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
43 self.client.connect(ip, username=username, password=password,
44 pkey=pkey, key_filename=key_filename,
45 look_for_keys=look_for_keys,
46 allow_agent=allow_agent)
52 def ssh(self, command):
54 self.log.debug("Executing: %s" % command)
55 stdin, stdout, stderr = self.client.exec_command(command)
56 ret = stdout.channel.recv_exit_status()
58 for line in stdout.read().splitlines():
62 self.log.debug("*** FAILED to run command %s (%s)" % (command, ret))
64 "Unable to run \ncommand: %s\nret: %s"
67 self.log.debug("*** SUCCESSFULLY run command %s" % command)
70 def scp(self, source, dest, method='put'):
72 self.log.info("Copy %s -> %s" % (source, dest))
73 ftp = self.client.open_sftp()