aboutsummaryrefslogtreecommitdiffstats
path: root/testcases/SECTests/connect.py
blob: fe8fbacfdef6ea05ad951c958920d7c10db3e013 (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/python
#
# Copyright (c) 2016 Red Hat
# Luke Hinds (lhinds@redhat.com)
# 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
#
# 0.1: OpenSCAP paramiko connection functions

import os
import paramiko

__version__ = 0.1
__author__ = 'Luke Hinds (lhinds@redhat.com)'
__url__ = 'http:/https://wiki.opnfv.org/display/security'


class connectionManager:
    def __init__(self, hostname, user, password, *args):
        self.hostname = hostname
        self.user = user
        self.password = password
        self.args = args

    def remotescript(self):
        localpath = self.args[0]
        remotepath = self.args[1]
        com = self.args[2]

        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(self.hostname, 22, self.user, self.password)

        sftp = client.open_sftp()
        sftp.put(localpath, remotepath)

        output = ""
        stdin, stdout, stderr = client.exec_command(com)
        stdout = stdout.readlines()
        sftp.remove(remotepath)
        client.close()

        # Spool it back (can be improved at later point)
        for line in stdout:
            output = output + line
        if output != "":
            return output

    def remotecmd(self):
        com = self.args[0]
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(self.hostname, 22, self.user, self.password)
        output = ""
        stdin, stdout, stderr = client.exec_command(com)
        stdout = stdout.readlines()
        client.close()

        # Spool it back (can be improved at later point)
        for line in stdout:
            output = output + line
        if output != "":
            return output
        else:
            print "There was no output for this command"

    def run_tool(self):
        dist = self.args[0]
        report = self.args[1]
        com = self.args[2]
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(self.hostname, 22, self.user, self.password)

        output = ""
        stdin, stdout, stderr = client.exec_command(com)
        stdout = stdout.readlines()
        client.close()

        # Spool it back (can be improved at later point)
        for line in stdout:
            output = output + line
        if output != "":
            return output

    def download_reports(self):
        dl_folder = self.args[0]
        reportfile = self.args[1]
        reportname = self.args[2]
        resultsname = self.args[3]
        transport = paramiko.Transport((self.hostname, 22))
        transport.connect(username=self.user, password=self.password)
        sftp = paramiko.SFTPClient.from_transport(transport)

        # Download the reportfile (html) and the results (xml)
        print 'Downloading \"{0}\"...'.format(reportname)
        sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, reportname)))
        print 'Downloading \"{0}\"...'.format(resultsname)
        sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, resultsname)))
        sftp.close()
        transport.close()