3 # Copyright (c) 2016 Red Hat
4 # Luke Hinds (lhinds@redhat.com)
5 # This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # 0.1: OpenSCAP paramiko connection functions
16 __author__ = 'Luke Hinds (lhinds@redhat.com)'
17 __url__ = 'http:/https://wiki.opnfv.org/display/security'
20 class connectionManager:
21 def __init__(self, hostname, user, password, *args):
22 self.hostname = hostname
24 self.password = password
27 def remotescript(self):
28 localpath = self.args[0]
29 remotepath = self.args[1]
32 client = paramiko.SSHClient()
33 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
34 client.connect(self.hostname, 22, self.user, self.password)
36 sftp = client.open_sftp()
37 sftp.put(localpath, remotepath)
40 stdin, stdout, stderr = client.exec_command(com)
41 stdout = stdout.readlines()
42 sftp.remove(remotepath)
45 # Spool it back (can be improved at later point)
47 output = output + line
53 client = paramiko.SSHClient()
54 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
55 client.connect(self.hostname, 22, self.user, self.password)
57 stdin, stdout, stderr = client.exec_command(com)
58 stdout = stdout.readlines()
61 # Spool it back (can be improved at later point)
63 output = output + line
67 print "There was no output for this command"
71 # report = self.args[1]
73 client = paramiko.SSHClient()
74 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
75 client.connect(self.hostname, 22, self.user, self.password)
78 stdin, stdout, stderr = client.exec_command(com)
79 stdout = stdout.readlines()
82 # Spool it back (can be improved at later point)
84 output = output + line
88 def download_reports(self):
89 dl_folder = self.args[0]
90 reportfile = self.args[1]
91 reportname = self.args[2]
92 resultsname = self.args[3]
93 transport = paramiko.Transport((self.hostname, 22))
94 transport.connect(username=self.user, password=self.password)
95 sftp = paramiko.SFTPClient.from_transport(transport)
97 # Download the reportfile (html) and the results (xml)
98 print 'Downloading \"{0}\"...'.format(reportname)
99 sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, reportname)))
100 print 'Downloading \"{0}\"...'.format(resultsname)
101 sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, resultsname)))