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
17 __author__ = 'Luke Hinds (lhinds@redhat.com)'
18 __url__ = 'http:/https://wiki.opnfv.org/display/security'
21 class connectionManager:
22 def __init__(self, hostname, user, password, *args):
23 self.hostname = hostname
25 self.password = password
28 def remotescript(self):
29 localpath = self.args[0]
30 remotepath = self.args[1]
33 client = paramiko.SSHClient()
34 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
35 client.connect(self.hostname, 22, self.user, self.password)
37 sftp = client.open_sftp()
38 sftp.put(localpath, remotepath)
41 stdin, stdout, stderr = client.exec_command(com)
42 stdout = stdout.readlines()
43 sftp.remove(remotepath)
46 # Spool it back (can be improved at later point)
48 output = output + line
54 client = paramiko.SSHClient()
55 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
56 client.connect(self.hostname, 22, self.user, self.password)
58 stdin, stdout, stderr = client.exec_command(com)
59 stdout = stdout.readlines()
62 # Spool it back (can be improved at later point)
64 output = output + line
68 print "There was no output for this command"
74 client = paramiko.SSHClient()
75 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
76 client.connect(self.hostname, 22, self.user, self.password)
79 stdin, stdout, stderr = client.exec_command(com)
80 stdout = stdout.readlines()
83 # Spool it back (can be improved at later point)
85 output = output + line
89 def download_reports(self):
90 dl_folder = self.args[0]
91 reportfile = self.args[1]
92 reportname = self.args[2]
93 resultsname = self.args[3]
94 transport = paramiko.Transport((self.hostname, 22))
95 transport.connect(username=self.user, password=self.password)
96 sftp = paramiko.SFTPClient.from_transport(transport)
98 # Download the reportfile (html) and the results (xml)
99 print 'Downloading \"{0}\"...'.format(reportname)
100 sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, reportname)))
101 print 'Downloading \"{0}\"...'.format(resultsname)
102 sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, resultsname)))