blob: b2d1342bbdb974a7605d00227811f96c4f5f8e99 [file] [log] [blame]
A.R Karthick2e99c472017-03-22 19:13:51 -07001import os, sys, time
A R Karthickec1dde02016-10-06 13:52:25 -07002from paramiko import SSHClient, WarningPolicy, AutoAddPolicy
A.R Karthick2e99c472017-03-22 19:13:51 -07003import logging
4logging.getLogger('scapy.runtime').setLevel(logging.ERROR)
A R Karthick76a497a2017-04-12 10:59:39 -07005from CordTestUtils import log_test
A R Karthickec1dde02016-10-06 13:52:25 -07006
7class SSHTestAgent(object):
8 key_file = os.getenv('SSH_KEY_FILE', None)
A R Karthick19aaf5c2016-11-09 17:47:57 -08009 host = os.getenv('CORD_TEST_HOST', '172.17.0.1')
10 hosts_file = os.path.join(os.getenv('HOME'), '.ssh', 'known_hosts')
A R Karthickec1dde02016-10-06 13:52:25 -070011 user = 'ubuntu'
12 password = None
13
A R Karthick9313b762016-11-07 13:14:35 -080014 def __init__(self, host = host, user = user, password = password, port = 22):
15 self.host = host
A R Karthickec1dde02016-10-06 13:52:25 -070016 self.user = user
17 self.password = password
A R Karthick9313b762016-11-07 13:14:35 -080018 self.port = port
A R Karthickec1dde02016-10-06 13:52:25 -070019 self.client = SSHClient()
A R Karthickec1dde02016-10-06 13:52:25 -070020 self.client.set_missing_host_key_policy(AutoAddPolicy())
21
22 def run_cmd(self, cmd, timeout = 5):
23 """Run the command on the test host"""
A R Karthickf8e753c2017-03-22 11:50:38 -070024 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 -070025 try:
A R Karthick19aaf5c2016-11-09 17:47:57 -080026 os.system(host_remove)
A R Karthickd0a334d2016-11-10 17:47:08 -080027 except: pass
28
29 try:
A R Karthickec1dde02016-10-06 13:52:25 -070030 self.client.connect(self.host, username = self.user, password = self.password,
A R Karthick9313b762016-11-07 13:14:35 -080031 key_filename = self.key_file, timeout=timeout, port = self.port)
A R Karthickec1dde02016-10-06 13:52:25 -070032 except:
A R Karthick76a497a2017-04-12 10:59:39 -070033 log_test.error('Unable to connect to test host %s' %self.host)
A R Karthickec1dde02016-10-06 13:52:25 -070034 return False, None
A R Karthickf8e753c2017-03-22 11:50:38 -070035
A R Karthickec1dde02016-10-06 13:52:25 -070036 channel = self.client.get_transport().open_session()
37 channel.exec_command(cmd)
A R Karthick850795b2017-03-27 14:07:03 -070038 status_ready = False
A R Karthick9313b762016-11-07 13:14:35 -080039 if channel.exit_status_ready():
40 status = channel.recv_exit_status()
A R Karthick850795b2017-03-27 14:07:03 -070041 status_ready = True
A R Karthick9313b762016-11-07 13:14:35 -080042 else:
43 status = 0
A R Karthickec1dde02016-10-06 13:52:25 -070044 output = None
45 st = status == 0
46 if st:
47 output = ''
48 while True:
49 data = channel.recv(4096)
50 if data:
51 output += data
52 else:
53 break
A R Karthick850795b2017-03-27 14:07:03 -070054 if status_ready is False:
55 status = channel.recv_exit_status()
56 st = status == 0
A.R Karthick2e99c472017-03-22 19:13:51 -070057 time.sleep(0.1)
A R Karthickec1dde02016-10-06 13:52:25 -070058 channel.close()
A R Karthickd0a334d2016-11-10 17:47:08 -080059 self.client.close()
A R Karthickec1dde02016-10-06 13:52:25 -070060 return st, output
61
62if __name__ == '__main__':
63 agent = SSHTestAgent(user = 'ubuntu', password = 'ubuntu')
64 cmds = ('docker images', 'docker ps')
65 for cmd in cmds:
66 st, output = agent.run_cmd(cmd)
67 print('Command \"%s\" returned with status: %s' %(cmd, st))
68 if st:
69 print('%s\n' %output)