Kailash Khalasi | 2f567a4 | 2017-09-27 13:50:05 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Zack Williams | c6722d5 | 2020-01-13 16:34:33 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import |
| 16 | |
Kailash Khalasi | 2f567a4 | 2017-09-27 13:50:05 -0700 | [diff] [blame] | 17 | import paramiko |
| 18 | |
Zack Williams | c6722d5 | 2020-01-13 16:34:33 -0700 | [diff] [blame] | 19 | |
| 20 | def onos_command_execute(host, portNum, cmd, user="karaf", passwd="karaf"): |
Kailash Khalasi | 2f567a4 | 2017-09-27 13:50:05 -0700 | [diff] [blame] | 21 | """ |
| 22 | :param host: onos-cord or onos-fabric |
| 23 | :param portNum: 8102 or 8101 |
| 24 | :param cmd: command to execute |
| 25 | :param user: onos/karaf |
| 26 | :param passwd: onos/karaf |
| 27 | :return: output of command executed inside onos karaf (shell) |
| 28 | """ |
| 29 | try: |
| 30 | client = paramiko.SSHClient() |
| 31 | client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 32 | client.connect(host, port=int(portNum), username=user, password=passwd) |
| 33 | stdin, stdout, stderr = client.exec_command(cmd) |
| 34 | while not stdout.channel.exit_status_ready(): |
| 35 | if stdout.channel.recv_ready(): |
| 36 | return stdout.read() |
| 37 | finally: |
| 38 | client.close() |
| 39 | |
Zack Williams | c6722d5 | 2020-01-13 16:34:33 -0700 | [diff] [blame] | 40 | |
Kailash Khalasi | 2f567a4 | 2017-09-27 13:50:05 -0700 | [diff] [blame] | 41 | def get_compute_node_ip(compute_node): |
| 42 | """ |
| 43 | :param compute_node: one compute node information from output of 'cordvtn-nodes' |
| 44 | :return: data_ip of that compute node |
| 45 | """ |
| 46 | for line in compute_node.splitlines(): |
| 47 | columns = line.split() |
| 48 | if len(columns) >= 2: |
Zack Williams | c6722d5 | 2020-01-13 16:34:33 -0700 | [diff] [blame] | 49 | return columns[2].split("/")[0] |