Matteo Scandolo | 48d3d2d | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
A.R Karthick | 2e99c47 | 2017-03-22 19:13:51 -0700 | [diff] [blame] | 17 | import os, sys, time |
A R Karthick | ec1dde0 | 2016-10-06 13:52:25 -0700 | [diff] [blame] | 18 | from paramiko import SSHClient, WarningPolicy, AutoAddPolicy |
A.R Karthick | 2e99c47 | 2017-03-22 19:13:51 -0700 | [diff] [blame] | 19 | import logging |
| 20 | logging.getLogger('scapy.runtime').setLevel(logging.ERROR) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 21 | from CordTestUtils import log_test |
A R Karthick | ec1dde0 | 2016-10-06 13:52:25 -0700 | [diff] [blame] | 22 | |
| 23 | class SSHTestAgent(object): |
| 24 | key_file = os.getenv('SSH_KEY_FILE', None) |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 25 | host = os.getenv('CORD_TEST_HOST', '172.17.0.1') |
| 26 | hosts_file = os.path.join(os.getenv('HOME'), '.ssh', 'known_hosts') |
A R Karthick | ec1dde0 | 2016-10-06 13:52:25 -0700 | [diff] [blame] | 27 | user = 'ubuntu' |
| 28 | password = None |
| 29 | |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 30 | def __init__(self, host = host, user = user, password = password, port = 22): |
| 31 | self.host = host |
A R Karthick | ec1dde0 | 2016-10-06 13:52:25 -0700 | [diff] [blame] | 32 | self.user = user |
| 33 | self.password = password |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 34 | self.port = port |
A R Karthick | ec1dde0 | 2016-10-06 13:52:25 -0700 | [diff] [blame] | 35 | self.client = SSHClient() |
A R Karthick | ec1dde0 | 2016-10-06 13:52:25 -0700 | [diff] [blame] | 36 | self.client.set_missing_host_key_policy(AutoAddPolicy()) |
| 37 | |
| 38 | def run_cmd(self, cmd, timeout = 5): |
| 39 | """Run the command on the test host""" |
A R Karthick | f8e753c | 2017-03-22 11:50:38 -0700 | [diff] [blame] | 40 | host_remove = 'ssh-keygen -f "%s" -R [%s]:8101 2>/dev/null' %(self.hosts_file, self.host) |
A R Karthick | ec1dde0 | 2016-10-06 13:52:25 -0700 | [diff] [blame] | 41 | try: |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 42 | os.system(host_remove) |
A R Karthick | d0a334d | 2016-11-10 17:47:08 -0800 | [diff] [blame] | 43 | except: pass |
| 44 | |
| 45 | try: |
A R Karthick | ec1dde0 | 2016-10-06 13:52:25 -0700 | [diff] [blame] | 46 | self.client.connect(self.host, username = self.user, password = self.password, |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 47 | key_filename = self.key_file, timeout=timeout, port = self.port) |
A R Karthick | ec1dde0 | 2016-10-06 13:52:25 -0700 | [diff] [blame] | 48 | except: |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 49 | log_test.error('Unable to connect to test host %s' %self.host) |
A R Karthick | ec1dde0 | 2016-10-06 13:52:25 -0700 | [diff] [blame] | 50 | return False, None |
A R Karthick | f8e753c | 2017-03-22 11:50:38 -0700 | [diff] [blame] | 51 | |
A R Karthick | ec1dde0 | 2016-10-06 13:52:25 -0700 | [diff] [blame] | 52 | channel = self.client.get_transport().open_session() |
| 53 | channel.exec_command(cmd) |
A R Karthick | 850795b | 2017-03-27 14:07:03 -0700 | [diff] [blame] | 54 | status_ready = False |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 55 | if channel.exit_status_ready(): |
| 56 | status = channel.recv_exit_status() |
A R Karthick | 850795b | 2017-03-27 14:07:03 -0700 | [diff] [blame] | 57 | status_ready = True |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 58 | else: |
| 59 | status = 0 |
A R Karthick | ec1dde0 | 2016-10-06 13:52:25 -0700 | [diff] [blame] | 60 | output = None |
| 61 | st = status == 0 |
| 62 | if st: |
| 63 | output = '' |
| 64 | while True: |
| 65 | data = channel.recv(4096) |
| 66 | if data: |
| 67 | output += data |
| 68 | else: |
| 69 | break |
A R Karthick | 850795b | 2017-03-27 14:07:03 -0700 | [diff] [blame] | 70 | if status_ready is False: |
| 71 | status = channel.recv_exit_status() |
| 72 | st = status == 0 |
A.R Karthick | 2e99c47 | 2017-03-22 19:13:51 -0700 | [diff] [blame] | 73 | time.sleep(0.1) |
A R Karthick | ec1dde0 | 2016-10-06 13:52:25 -0700 | [diff] [blame] | 74 | channel.close() |
A R Karthick | d0a334d | 2016-11-10 17:47:08 -0800 | [diff] [blame] | 75 | self.client.close() |
A R Karthick | ec1dde0 | 2016-10-06 13:52:25 -0700 | [diff] [blame] | 76 | return st, output |
| 77 | |
| 78 | if __name__ == '__main__': |
| 79 | agent = SSHTestAgent(user = 'ubuntu', password = 'ubuntu') |
| 80 | cmds = ('docker images', 'docker ps') |
| 81 | for cmd in cmds: |
| 82 | st, output = agent.run_cmd(cmd) |
| 83 | print('Command \"%s\" returned with status: %s' %(cmd, st)) |
| 84 | if st: |
| 85 | print('%s\n' %output) |