Remove unused CI_DEBUG
[functest.git] / testcases / SECTests / connect.py
1 #!/usr/bin/python
2 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # 0.1: OpenSCAP paramiko connection functions
12
13 import paramiko
14
15 __version__ = 0.1
16 __author__ = 'Luke Hinds (lhinds@redhat.com)'
17 __url__ = 'http:/https://wiki.opnfv.org/display/security'
18
19
20 class connectionManager:
21     def __init__(self, hostname, user, password, *args):
22         self.hostname = hostname
23         self.user = user
24         self.password = password
25         self.args = args
26
27     def remotescript(self):
28         localpath = self.args[0]
29         remotepath = self.args[1]
30         com = self.args[2]
31
32         client = paramiko.SSHClient()
33         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
34         client.connect(self.hostname, 22, self.user, self.password)
35
36         sftp = client.open_sftp()
37         sftp.put(localpath, remotepath)
38
39         output = ""
40         stdin, stdout, stderr = client.exec_command(com)
41         stdout = stdout.readlines()
42         sftp.remove(remotepath)
43         client.close()
44
45         # Spool it back (can be improved at later point)
46         for line in stdout:
47             output = output + line
48         if output != "":
49             return output
50
51     def remotecmd(self):
52         com = self.args[0]
53         client = paramiko.SSHClient()
54         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
55         client.connect(self.hostname, 22, self.user, self.password)
56         output = ""
57         stdin, stdout, stderr = client.exec_command(com)
58         stdout = stdout.readlines()
59         client.close()
60
61         # Spool it back (can be improved at later point)
62         for line in stdout:
63             output = output + line
64         if output != "":
65             return output
66         else:
67             print "There was no output for this command"
68
69     def run_tool(self):
70         # dist = self.args[0]
71         # report = self.args[1]
72         com = self.args[2]
73         client = paramiko.SSHClient()
74         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
75         client.connect(self.hostname, 22, self.user, self.password)
76
77         output = ""
78         stdin, stdout, stderr = client.exec_command(com)
79         stdout = stdout.readlines()
80         client.close()
81
82         # Spool it back (can be improved at later point)
83         for line in stdout:
84             output = output + line
85         if output != "":
86             return output
87
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)
96
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)))
102         sftp.close()
103         transport.close()