aboutsummaryrefslogtreecommitdiff
path: root/rumba/ssh_support.py
blob: eb326413bfeb0e3d2cfd8ef8f215f2f4fd93092a (plain)
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
#
# SSH support for Rumba
#
#    Sander Vrijders  <sander.vrijders@intec.ugent.be>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA  02110-1301  USA

import paramiko

def get_ssh_client():
    ssh_client = paramiko.SSHClient()
    ssh_client.load_system_host_keys()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    return ssh_client

def execute_commands(testbed, ssh_config, commands, time_out=3):
    '''
    Remote execution of a list of shell command on hostname. By
    default this function will exit (timeout) after 3 seconds.

    @param testbed: testbed info
    @param ssh_config: ssh config of the node
    @param commands: *nix shell command
    @param time_out: time_out value in seconds, error will be generated if
    no result received in given number of seconds, the value None can
    be used when no timeout is needed
    '''
    ssh_client = get_ssh_client()

    try:
        #print("Connecting to %s@%s:%s (pwd=%s)" % (testbed.username,
        #        ssh_config.hostname, ssh_config.port, testbed.password))
        ssh_client.connect(ssh_config.hostname, ssh_config.port,
                           testbed.username, testbed.password,
                           look_for_keys=True, timeout=time_out)
        for command in commands:
            stdin, stdout, stderr = ssh_client.exec_command(command)
            del stdin, stdout
            err = str(stderr.read()).strip('b\'\"\\n')
            if err != "":
                err_array = err.split('\\n')
                for erra in err_array:
                    print(erra)
        ssh_client.close()

    except Exception as e:
        print(str(e))
        return

def execute_command(testbed, ssh_config, command, time_out=3):
    '''
    Remote execution of a list of shell command on hostname. By
    default this function will exit (timeout) after 3 seconds.

    @param testbed: testbed info
    @param ssh_config: ssh config of the node
    @param command: *nix shell command
    @param time_out: time_out value in seconds, error will be generated if
    no result received in given number of seconds, the value None can
    be used when no timeout is needed

    @return: stdout resulting from the command
    '''
    ssh_client = get_ssh_client()

    try:
        ssh_client.connect(ssh_config.hostname, ssh_config.port,
                           testbed.username, testbed.password,
                           look_for_keys=True, timeout=time_out)
        stdin, stdout, stderr = ssh_client.exec_command(command)
        del stdin
        err = str(stderr.read()).strip('b\'\"\\n')
        if err != "":
            print(err)
        output = str(stdout.read()).strip('b\'\"\\n')
        ssh_client.close()

        return output

    except Exception as e:
        print(str(e))
        return

def copy_file_to_testbed(testbed, ssh_config, text, file_name):
    '''
    Write a string to a given remote file.
    Overwrite the complete file if it already exists!

    @param testbed: testbed info
    @param ssh_config: ssh config of the node
    @param text: string to be written in file
    @param file_name: file name (including full path) on the host
    '''
    ssh_client = get_ssh_client()

    try:
        ssh_client.connect(ssh_config.hostname, ssh_config.port,
                           testbed.username,
                           testbed.password,
                           look_for_keys=True)

        cmd = "touch " + file_name + \
              "; chmod a+rwx " + file_name

        stdin, stdout, stderr = ssh_client.exec_command(cmd)
        del stdin, stdout
        err = str(stderr.read()).strip('b\'\"\\n')
        if err != "":
            print(err)

        sftp_client = ssh_client.open_sftp()
        remote_file = sftp_client.open(file_name, 'w')

        remote_file.write(text)
        remote_file.close()

    except Exception as e:
        print(str(e))

def setup_vlan(testbed, node, vlan_id, int_name):
    '''
    Gets the interface (ethx) to link mapping

    @param testbed: testbed info
    @param node: the node to create the VLAN on
    @param vlan_id: the VLAN id
    @param int_name: the name of the interface
    '''
    print("Setting up VLAN on node " + node.name)

    cmds = list()
    cmd = "sudo ip link add link " + \
          str(int_name) + \
          " name " + str(int_name) + \
          "." + str(vlan_id) + \
          " type vlan id " + str(vlan_id)
    cmds.append(cmd)
    cmd = "sudo ifconfig " + \
          str(int_name) + "." + \
          str(vlan_id) + " up"
    cmds.append(cmd)
    cmd = "sudo ethtool -K " + \
          str(int_name) + " rxvlan off"
    cmds.append(cmd)
    cmd = "sudo ethtool -K " + \
          str(int_name) + " txvlan off"
    execute_commands(testbed, node.ssh_config, cmds)