-
Notifications
You must be signed in to change notification settings - Fork 7
/
ssh_utilities.py
56 lines (45 loc) · 1.87 KB
/
ssh_utilities.py
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
import pexpect
from pxssh import pxssh, ExceptionPxssh
def ssh_execute(user, host, password, command):
ssh_answer = 'Are you sure you want to continue connecting'
ssession = pexpect.spawn('ssh -l %s %s %s'%(user, host, command))
reply = ssession.expect([pexpect.TIMEOUT, ssh_answer, 'password: '])
if reply == 0: # Timeout
raise ExceptionPxssh('ERROR:' + str(ssession.before) + ':' + str(ssession.after))
if reply == 1: # SSH does not have the public key. Just accept it.
ssession.sendline ('yes')
ssession.expect ('password: ')
reply = ssession.expect([pexpect.TIMEOUT, 'password: '])
if reply == 0: # Timeout
ExceptionPxssh('ERROR:' + str(ssession.before) + ':' + str(ssession.after))
ssession.sendline(password)
ssession.expect(pexpect.EOF)
return 'OK:' + str(ssession.before) + ':' + str(ssession.after)
class SSHClient(object):
def __init__(self, host, port, username, password):
#pxssh.__init__(self)
self.host = host
self.port = port
self.username = username
self.password = password
#self.force_password = True
#self.login(host, username,password=password, port=port)
def send_command(self, text):
#self.sendline(text)
#self.prompt()
#return self.before
return ssh_execute(self.username, self.host, self.password, text)
def close_channel(self):
#self.logout()
pass
class SSHClient_(pxssh):
def __init__(self, host, port, username, password):
pxssh.__init__(self)
self.force_password = True
self.login(host, username,password=password, port=port)
def send_command(self, text):
self.sendline(text)
self.prompt()
return self.before
def close_channel(self):
self.logout()