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