1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
##############################################################################
# Copyright (c) 2017 ZTE Corporation and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
import json
import os
import paramiko
def load_json_file(full_path):
"""Loads JSON from file
:param target_filename:
:return:
"""
if not os.path.isfile(full_path):
raise Exception('File(%s) does not exist' % full_path)
with open(full_path, 'r') as file:
return json.load(file)
def write_json_file(full_path, data):
"""write JSON from file
:param target_filename:
:return:
"""
with open(full_path, 'w+') as file:
file.write(json.dumps(data))
class SSHClient(object):
def __init__(self, ip, username, password=None, pkey=None,
key_filename=None, log=None, look_for_keys=False,
allow_agent=False):
self.client = paramiko.SSHClient()
self.client.load_system_host_keys()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(ip, username=username, password=password,
pkey=pkey, key_filename=key_filename,
look_for_keys=look_for_keys,
allow_agent=allow_agent)
self.log = log
def __del__(self):
self.client.close()
def ssh(self, command):
if self.log:
self.log.debug("Executing: %s" % command)
stdin, stdout, stderr = self.client.exec_command(command)
ret = stdout.channel.recv_exit_status()
output = list()
for line in stdout.read().splitlines():
output.append(line.decode('utf-8'))
if ret:
if self.log:
self.log.debug("*** FAILED to run command %s (%s)" % (command, ret))
raise Exception(
"Unable to run \ncommand: %s\nret: %s"
% (command, ret))
if self.log:
self.log.debug("*** SUCCESSFULLY run command %s" % command)
return ret, output
def scp(self, source, dest, method='put'):
if self.log:
self.log.info("Copy %s -> %s" % (source, dest))
ftp = self.client.open_sftp()
if method == 'put':
ftp.put(source, dest)
elif method == 'get':
ftp.get(source, dest)
ftp.close()
|