2011-07-07 06:11:39 +00:00
|
|
|
function host = FindFreeSSH(host_list, wait_time, command)
|
|
|
|
% function host = FindFreeSSH(host_list, wait_time, command)
|
|
|
|
%
|
|
|
|
% Find a free ssh host not running openEMS
|
|
|
|
%
|
|
|
|
% internal function used by RunOpenEMS
|
|
|
|
%
|
|
|
|
% host_list: give a list of possible host
|
|
|
|
%
|
|
|
|
% wait_time: wait x seconds after not finding a free host and rechecking
|
2011-11-07 10:54:34 +00:00
|
|
|
% default: 600 seconds
|
2011-07-07 06:11:39 +00:00
|
|
|
%
|
|
|
|
% command: unix command to check for free host (empty result --> free)
|
2011-11-24 08:00:13 +00:00
|
|
|
% default: 'ps -e | grep openEMS'
|
2011-07-07 06:11:39 +00:00
|
|
|
%
|
|
|
|
% See also RunOpenEMS
|
|
|
|
%
|
|
|
|
% openEMS matlab interface
|
|
|
|
% -----------------------
|
|
|
|
% author: Thorsten Liebig
|
|
|
|
|
|
|
|
if (nargin<3)
|
|
|
|
% command which should return an empty string if host is available
|
2011-07-13 07:39:41 +00:00
|
|
|
command = 'ps -e | grep openEMS';
|
2011-07-07 06:11:39 +00:00
|
|
|
end
|
|
|
|
|
2011-12-22 12:38:32 +00:00
|
|
|
% 10 seconds ssh timeout
|
|
|
|
time_out = 10;
|
|
|
|
|
2011-07-07 06:11:39 +00:00
|
|
|
if (nargin<2)
|
2011-11-07 10:54:34 +00:00
|
|
|
wait_time = 600;
|
2011-07-07 06:11:39 +00:00
|
|
|
end
|
|
|
|
|
2011-11-24 08:00:13 +00:00
|
|
|
if ischar(host_list)
|
|
|
|
fid=fopen(host_list);
|
|
|
|
if (fid==-1)
|
|
|
|
error('FindFreeSSH: cant open host file');
|
|
|
|
end
|
|
|
|
clear host_list;
|
|
|
|
host_list = {};
|
|
|
|
while 1
|
|
|
|
line = fgetl(fid);
|
|
|
|
if ischar(line)
|
|
|
|
host_list{end+1} = line;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
fclose(fid);
|
|
|
|
elseif ~iscell(host_list)
|
|
|
|
error('FindFreeSSH: unknown host list format');
|
|
|
|
end
|
|
|
|
|
2011-07-07 06:11:39 +00:00
|
|
|
while 1
|
|
|
|
for n = 1:numel(host_list)
|
|
|
|
host = host_list{n};
|
2011-12-22 12:38:32 +00:00
|
|
|
[status, result] = unix(['ssh -o ConnectTimeout=' num2str(time_out) ' ' host ' ''' command '''']);
|
2011-11-24 08:00:13 +00:00
|
|
|
|
|
|
|
if (isempty(result) && status==1)
|
2011-07-07 06:11:39 +00:00
|
|
|
disp(['FindFreeSSH:: found a free host: ' host ]);
|
|
|
|
return
|
2011-11-24 08:00:13 +00:00
|
|
|
elseif (~isempty(result) && status==0)
|
2011-07-07 06:11:39 +00:00
|
|
|
disp(['FindFreeSSH:: ' host ' is busy running openEMS ... ' ]);
|
2011-11-24 08:00:13 +00:00
|
|
|
else
|
|
|
|
disp(['FindFreeSSH:: shh connection to ' host ' failed ... ' ]);
|
2011-07-07 06:11:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
host = '';
|
|
|
|
|
|
|
|
if (wait_time<=0)
|
|
|
|
warning('openEMS:FindFreeSSH',' unable to find a free host ');
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
disp([' no free host found waiting for ' num2str(wait_time) ' seconds ... '])
|
|
|
|
pause(wait_time)
|
|
|
|
end
|