Revision bf9defce

/dev/null
1
#!/usr/bin/env python
2

  
/dev/null
1
#!/usr/bin/env python
2

  
/dev/null
1
import Queue
2
import threading
3

  
4
class Session(threading.Thread):
5
    
6
    def __init__(self, capabilities):
7
        Thread.__init__()
8
        self._capabilities = capabilities
9
        self._id = None
10
        self._inQ = Queue.Queue() # server -> client
11
        self._outQ = Queue.Queue() # client -> server
12
        
13
    def connect(self):
14
        self.start()
15
        
16
    def run(self):
17
        raise NotImplementedError
18
        
19
    def send(self, msg):
20
        self._inQ.add(msg)
21

  
22
    def expect_close(self, val=True):
23
        '''operations.CloseSession must call this before a call to send(),
24
        so that the remote endpoint closing the connection does not result
25
        in an exception'''
26
        self._expect_close = val
27

  
28
    @property
29
    def id(self):
30
        'Session ID'
31
        return self._id
32
    
33
    @property
34
    def is_connected(self):
35
        return self._is_connected
36
    
/dev/null
1
import paramiko
2

  
3
from select import select as select
4

  
5
from session import Session
6

  
7
class SSH(Session):
8
    
9
    BUFSIZE = 4096
10
    
11
    DELIM = ']]>>]]>'
12
    
13
    def __init__(self, loadKnownHosts=True, hostname=None, port=22, authType=None, authInfo=None):
14
        Session.__init__(self)
15
        self._client = paramiko.SSHClient()
16
        if loadKnownHosts:
17
            self._client.load_system_host_keys()
18
        self._channel = None
19
        self._callback = None
20
        self._inBuf = ''
21
        self._outBuf = ''
22
    
23
    def _connect(self):
24
        self._callback = None #smthn
25
        self._outQ.add(hello)
26
    
27
    def _remote_closed(self):
28
        if not self._expectingClose:
29
            raise SomeException
30
    
31
    def _this_just_in(self, data):
32
        self._inBuf += data
33
        pos = self._inBuf.find(DELIM)
34
        if pos != -1:
35
            msg = self._inBuf[:pos]
36
            self._inBuf = self._inBuf[(pos + len(DELIM)):]
37
            self._callback(msg)
38
    
39
    def connect(self):
40
        self._connect()
41
        self._channel = self._client.get_transport().open_session()
42
        self._channel.invoke_subsystem('netconf')
43
        Session.connect(self)
44
    
45
    def run(self):
46
        sock = self._channel
47
        sock.setblocking(0)
48
        outQ = self._outQ
49
        while True:
50
            (r, w, e) = select([sock], [sock], [], 60)
51
            if w:
52
                if not outQ.empty():
53
                   self._outBuffer += ( outQ.get() + DELIM )
54
                if self._outBuffer:
55
                    n = sock.send(self._outBuffer)
56
                    self._outBuffer = self._outBuffer[n:]
57
            if r:
58
                data = sock.recv(BUFSIZE)
59
                if data:
60
                    self._this_just_in(data)
61
                else:
62
                    self._remote_closed()
63
                    
64
class MissingHostKeyPolicy(paramiko.MissingHostKeyPolicy): pass

Also available in: Unified diff