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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#!/usr/bin/env python
import paramiko
import logging
###################################################################
## ATFSsh.Ssh Class contains variables and methods for
## Connecting to remote machine using SSH
##
## Variables:
## connections: List of Dictionaries.
## Each Dictionary contains {host, user, conn} information.
## host : Server IP address
## user : User on host
## conn : SSH Conn Object to user@host
###################################################################
class Ssh():
def __init__(self):
self.connections = []
def connect(self, host, user):
"""
Objective:
SSH to Server "host" as User "user"
Parameter:
host: Server IP Address
user: Login Username
Return:
Success: 0
Failure: 1
"""
logger = logging.getLogger('ATF_LOG')
connection = paramiko.SSHClient()
connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
connection.connect(host, username=user)
except paramiko.BadHostKeyException as result:
logger.exception(
"BadHostKeyException: Unable to Connect to Server: '" + host +
"' as User: '" + user + "'")
return 1
except paramiko.AuthenticationException:
logger.exception("AuthenticationException: Unable to Authenticate "
+ user + "@" + host)
return 1
except paramiko.SSHException:
logger.exception("SSHException: Unknown server " + host)
return 1
else:
self.connections.append({'host':host, 'user': user, 'conn':
connection})
logger.info("Successfully Able to SSH to: " + user + "@" + host)
return 0
def close(self, host, user):
"""
Objective:
Close SSH Connections for User "username"
on the Server "server"
Parameters:
server: Server IP Address
username: User on the Server
Return:
Success: 0
Failure: 1
"""
logger = logging.getLogger('ATF_LOG')
index = 0
for conn in self.connections:
if conn['host'] == host and conn['user'] == user:
conn['conn'].close()
logger.info("Closing SSH Connection: " + conn['user'] + "@" +
conn['host'])
self.connection[index:(index + 1)] = []
index = index - 1
index = index + 1
return 0
def closeall(self):
"""
Objective:
Close All Existing SSH Connections
Parameters:
None.
Return:
Success: 0
Failure: 1
"""
logger = logging.getLogger('ATF_LOG')
for conn in self.connections:
conn['conn'].close()
logger.info("Closing SSH Connection: " + conn['user'] + "@" +
conn['host'])
self.connections[:] = []
return 0
def getconnection(self, host, user):
"""
Objective:
Return SSH connection object for username@server
Parameters:
host: Server IP Address
user: User on the Server
Return:
Success:SSH connection Object If SSH Connection Object exists
for username@server
Failure: 1 (If SSH Connection doesn't exist)
"""
for conn in self.connections:
if conn['host'] == host and conn['user'] == user:
return conn['conn']
else:
return 1
def executecommand(self, command, host, user):
"""
Objective:
Execute Command "comamnd" on user@host
Parameters:
host: IP address of Server/Client
user: username of user on Host 'host'
command: command to execute
Return:
Success: 0
Failure: 1
"""
logger = logging.getLogger('ATF_LOG')
output = []
conn = self.getconnection(host, user)
if conn == 1:
logger.error("SSH Connection Not Established to:" + user + "@" +
host)
output = [1, 0, 0, 0]
return output
try:
stdin, stdout, stderr = conn.exec_command(command)
except paramiko.SSHException:
logger.exception("Unable to Execute Command: " + command)
output = [1, 0, 0, 0]
return output
else:
output = [0, stdin, stdout, stderr]
return output
|