coding utf-8 from paramiko import Transport import select ALL_HOSTS 19

 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
# -*- coding: utf-8 -*-
from paramiko import Transport
import select
ALL_HOSTS = {'192.168.66.250':['aliens','predator', 22]}
class SSHControl:
def __init__(self, host):
self.host = host
self.get_auth()
def get_auth(self):
host = ALL_HOSTS[self.host]
if host:
self.user = host[0]
self.password = host[1]
try:
self.port = host[2]
except IndexError:
self.port = 22
else:
print "Host %s not found" % self.host
def connect(self):
self.server = Transport((self.host, self.port))
self.server.connect(username=self.user, password=self.password)
self.session = self.server.open_session()
self.server.is_authenticated()
def create_session(self):
self.session = self.server.open_session()
def close_session(self):
self.server.close()
def send_command(self, command):
if not self.session:
print "Create a session"
if not self.session.exec_command(command):
print 'ERROR: Failed to run: %s' % command
def read_command(self):
data = ''
while select.select(self.session, ):
x = self.session.recv(512)
if not x:
break
data += x
select.select([],[],[],.1)
return data