summaryrefslogtreecommitdiffstats
path: root/libs/connect/ssh.py
blob: a419c31b074415b14cb434a6ec7539673b719c7e (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
105
106
107
108
"""
 sshConnection Class for connecting and performing operation on
 remote server using SSH Protocol.
"""
import paramiko
import time
from atfglobals import GlobalObj

class SshConnection():
    
    def __init__(self): 
        self._connection = paramiko.SSHClient()

    def connect(self, host, user, password):
        """
        Objective:
            SSH to Server "host" as User "user"

        Parameter:
            host: Server IP Address 
            user: Login Username
            password: Login password

        Return:
            Success: 0
            Failure: 1
        """  
        logger = GlobalObj.getLoggerObj()
        self._connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        try:
            self._connection.connect(host, username=user, password=password)

        except paramiko.BadHostKeyException as result:
            logger.error("BadHostKeyException: Unable to Connect to Server: '" + host +
                         "' as User: '" + user + "'")
            return 1

        except paramiko.AuthenticationException:
            logger.error("AuthenticationException: Unable to Authenticate " +
                         user + "@" + host)
            return 1

        except paramiko.SSHException:
            logger.error("SSHException: Unknown server " + host)
            return 1      

        return 0

    def close(self):
        """
        Objective:
            Close SSH Connections 
        """
        self._connection.close()
        return 

    def executecommand(self, command, commandInput=None):
        """
        Objective:
            Execute Command "comamnd" 

        Parameters:
            command: command to execute

        Return:
            Success: 0
            Failure: 1
        """
        logger = GlobalObj.getLoggerObj()
        output = {}
        output["exitstatus"] = None
        output["stdoutdata"] = None
        output["stderrdata"] = None
        exit_status_ready_flag = True
                        
        try:
            transport = self._connection.get_transport()
            channel = transport.open_session()
            channel.exec_command(command)
            # Adding sleep to get the correct exit_status.
            time.sleep(5)
            exit_status_ready_flag = channel.exit_status_ready()
            if not exit_status_ready_flag:
                stdin = channel.makefile("wb")

                if commandInput:
                    stdin.write(commandInput)
                else:
                    logger.error("This command requirs Command Input \
                                 after executing comamnd for command completion")
                    stdin.write("\n")
                    return output
                
            stdout = channel.makefile("rb")
            stderr = channel.makefile_stderr("rb")
            exit_status = channel.recv_exit_status()

            output["exitstatus"] = exit_status
            output["stdoutdata"] = stdout.readlines()
            output["stderrdata"] = stderr.readlines()

        except paramiko.SSHException:
            logger.error("Unable to Execute Command: " + command)

        return output