1
###############################################################################
2
#    pyrpl - DSP servo controller for quantum optics with the RedPitaya
3
#    Copyright (C) 2014-2016  Leonhard Neuhaus  (neuhaus@spectro.jussieu.fr)
4
#
5
#    This program is free software: you can redistribute it and/or modify
6
#    it under the terms of the GNU General Public License as published by
7
#    the Free Software Foundation, either version 3 of the License, or
8
#    (at your option) any later version.
9
#
10
#    This program is distributed in the hope that it will be useful,
11
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
#    GNU General Public License for more details.
14
#
15
#    You should have received a copy of the GNU General Public License
16
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
###############################################################################
18

19

20 3
import paramiko
21 3
from time import sleep
22 3
from scp import SCPClient
23 3
import logging
24

25

26 3
class SshShell(object):
27
    """ This is a wrapper around paramiko.SSHClient and scp.SCPClient
28
    I provides a ssh connection with the ability to transfer files over it"""
29 3
    def __init__(
30
            self,
31
            hostname='localhost',
32
            user='root',
33
            password='root',
34
            delay=0.05, 
35
            timeout=3,
36
            sshport=22,
37
            shell=True):
38 3
        self._logger = logging.getLogger(name=__name__)
39 3
        self.delay = delay
40 3
        self.apprunning = False
41 3
        self.hostname = hostname
42 3
        self.sshport=sshport
43 3
        self.user = user
44 3
        self.password = password
45 3
        self.timeout= timeout
46 3
        self.ssh = paramiko.SSHClient()
47 3
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
48 3
        self.ssh.connect(
49
            hostname,
50
            username=self.user,
51
            password=self.password,
52
            port=self.sshport,
53
            timeout=timeout)
54 0
        if shell:
55 0
            self.channel = self.ssh.invoke_shell()
56 0
        self.startscp()
57

58 3
    def startscp(self):
59 0
        self.scp = SCPClient(self.ssh.get_transport())
60

61 3
    def write(self, text):
62 0
        if self.channel.send_ready() and not text == "":
63 0
            return self.channel.send(text)
64
        else:
65 0
            return -1
66

67 3
    def read_nbytes(self, nbytes):
68 0
        if self.channel.recv_ready():
69 0
            return self.channel.recv(nbytes)
70
        else:
71 0
            return b""
72

73 3
    def read(self):
74 0
        sumstring = ""
75 0
        while True:
76 0
            string = self.read_nbytes(1024).decode('utf-8')
77 0
            sumstring += string
78 0
            if not string:
79 0
                break
80 0
        self._logger.debug(sumstring)
81 0
        return sumstring
82

83 3
    def askraw(self, question=""):
84 0
        self.write(question)
85 0
        sleep(self.delay)
86 0
        return self.read()
87

88 3
    def ask(self, question=""):
89 0
        return self.askraw(question + '\n')
90

91 3
    def __del__(self):
92 3
        self.endapp()
93 3
        try:
94 3
            self.channel.close()
95 3
        except AttributeError:
96 3
            pass  # already broken
97 3
        self.ssh.close()
98

99 3
    def endapp(self):
100 3
        pass
101

102 3
    def reboot(self):
103 0
        self.endapp()
104 0
        self.ask("shutdown -r now")
105 0
        self.__del__()
106

107 3
    def shutdown(self):
108 0
        self.endapp()
109 0
        self.ask("shutdown now")
110 0
        self.__del__()
111

112 3
    def get_mac_addresses(self):
113
        """
114
        returns all MAC addresses of the SSH device.
115
        """
116 0
        macs = list()
117 0
        nextgood = False
118 0
        for token in self.ask('ifconfig | grep HWaddr').split():
119 0
            if nextgood and len(token.split(':'))==6:
120 0
                macs.append(token)
121 0
            if token == 'HWaddr':
122 0
                nextgood = True
123
            else:
124 0
                nextgood = False
125 0
        return macs

Read our documentation on viewing source code .

Loading