blob: 14bebd52f1c0a10f3e850af5ec8e9f144d46d82a [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
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 Karthick2e99c472017-03-22 19:13:51 -070017import os, sys, time
A R Karthickec1dde02016-10-06 13:52:25 -070018from paramiko import SSHClient, WarningPolicy, AutoAddPolicy
A.R Karthick2e99c472017-03-22 19:13:51 -070019import logging
20logging.getLogger('scapy.runtime').setLevel(logging.ERROR)
A R Karthick76a497a2017-04-12 10:59:39 -070021from CordTestUtils import log_test
A R Karthickec1dde02016-10-06 13:52:25 -070022
23class SSHTestAgent(object):
24 key_file = os.getenv('SSH_KEY_FILE', None)
A R Karthick19aaf5c2016-11-09 17:47:57 -080025 host = os.getenv('CORD_TEST_HOST', '172.17.0.1')
26 hosts_file = os.path.join(os.getenv('HOME'), '.ssh', 'known_hosts')
A R Karthickec1dde02016-10-06 13:52:25 -070027 user = 'ubuntu'
28 password = None
29
A R Karthick9313b762016-11-07 13:14:35 -080030 def __init__(self, host = host, user = user, password = password, port = 22):
31 self.host = host
A R Karthickec1dde02016-10-06 13:52:25 -070032 self.user = user
33 self.password = password
A R Karthick9313b762016-11-07 13:14:35 -080034 self.port = port
A R Karthickec1dde02016-10-06 13:52:25 -070035 self.client = SSHClient()
A R Karthickec1dde02016-10-06 13:52:25 -070036 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 Karthickf8e753c2017-03-22 11:50:38 -070040 host_remove = 'ssh-keygen -f "%s" -R [%s]:8101 2>/dev/null' %(self.hosts_file, self.host)
A R Karthickec1dde02016-10-06 13:52:25 -070041 try:
A R Karthick19aaf5c2016-11-09 17:47:57 -080042 os.system(host_remove)
A R Karthickd0a334d2016-11-10 17:47:08 -080043 except: pass
44
45 try:
A R Karthickec1dde02016-10-06 13:52:25 -070046 self.client.connect(self.host, username = self.user, password = self.password,
A R Karthick9313b762016-11-07 13:14:35 -080047 key_filename = self.key_file, timeout=timeout, port = self.port)
A R Karthickec1dde02016-10-06 13:52:25 -070048 except:
A R Karthick76a497a2017-04-12 10:59:39 -070049 log_test.error('Unable to connect to test host %s' %self.host)
A R Karthickec1dde02016-10-06 13:52:25 -070050 return False, None
A R Karthickf8e753c2017-03-22 11:50:38 -070051
A R Karthickec1dde02016-10-06 13:52:25 -070052 channel = self.client.get_transport().open_session()
53 channel.exec_command(cmd)
A R Karthick850795b2017-03-27 14:07:03 -070054 status_ready = False
A R Karthick9313b762016-11-07 13:14:35 -080055 if channel.exit_status_ready():
56 status = channel.recv_exit_status()
A R Karthick850795b2017-03-27 14:07:03 -070057 status_ready = True
A R Karthick9313b762016-11-07 13:14:35 -080058 else:
59 status = 0
A R Karthickec1dde02016-10-06 13:52:25 -070060 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 Karthick850795b2017-03-27 14:07:03 -070070 if status_ready is False:
71 status = channel.recv_exit_status()
72 st = status == 0
A.R Karthick2e99c472017-03-22 19:13:51 -070073 time.sleep(0.1)
A R Karthickec1dde02016-10-06 13:52:25 -070074 channel.close()
A R Karthickd0a334d2016-11-10 17:47:08 -080075 self.client.close()
A R Karthickec1dde02016-10-06 13:52:25 -070076 return st, output
77
78if __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)