RunOpenEMS: support for windows ssh using putty
Signed-off-by: Thorsten Liebig <Thorsten.Liebig@gmx.de>
This commit is contained in:
parent
b74dd90558
commit
09092844c4
@ -1,5 +1,5 @@
|
||||
function host = FindFreeSSH(host_list, wait_time, command)
|
||||
% function host = FindFreeSSH(host_list, wait_time, command)
|
||||
function host = FindFreeSSH(host_list, Settings, wait_time, command)
|
||||
% function host = FindFreeSSH(host_list, Settings, wait_time, command)
|
||||
%
|
||||
% Find a free ssh host not running openEMS
|
||||
%
|
||||
@ -19,7 +19,7 @@ function host = FindFreeSSH(host_list, wait_time, command)
|
||||
% -----------------------
|
||||
% author: Thorsten Liebig
|
||||
|
||||
if (nargin<3)
|
||||
if (nargin<4)
|
||||
% command which should return an empty string if host is available
|
||||
command = 'ps -e | grep openEMS';
|
||||
end
|
||||
@ -27,10 +27,20 @@ end
|
||||
% 10 seconds ssh timeout
|
||||
time_out = 10;
|
||||
|
||||
if (nargin<2)
|
||||
if (nargin<3)
|
||||
wait_time = 600;
|
||||
end
|
||||
|
||||
if ~isunix
|
||||
ssh_command = [Settings.SSH.Putty.Path '/plink '];
|
||||
ssh_options = [' -i ' Settings.SSH.Putty.Key];
|
||||
command = ['"' command '"'];
|
||||
else
|
||||
ssh_command = 'ssh';
|
||||
ssh_options = ['-o ConnectTimeout=' num2str(time_out)];
|
||||
command = ['''' command ''''];
|
||||
end
|
||||
|
||||
if ischar(host_list)
|
||||
fid=fopen(host_list);
|
||||
if (fid==-1)
|
||||
@ -54,8 +64,7 @@ end
|
||||
while 1
|
||||
for n = 1:numel(host_list)
|
||||
host = host_list{n};
|
||||
[status, result] = unix(['ssh -o ConnectTimeout=' num2str(time_out) ' ' host ' ''' command '''']);
|
||||
|
||||
[status, result] = unix([ssh_command ' ' ssh_options ' ' host ' ' command ]);
|
||||
if (isempty(result) && status==1)
|
||||
disp(['FindFreeSSH:: found a free host: ' host ]);
|
||||
return
|
||||
|
@ -9,11 +9,15 @@ function RunOpenEMS(Sim_Path, Sim_File, opts, Settings)
|
||||
% opts = '--engine=fastest';
|
||||
%
|
||||
% optional:
|
||||
% (ssh only on unix with working ssh client)
|
||||
% Note: ssh only on unix with working ssh client or windows with putty client
|
||||
% openEMS Linux server or Windows with cygwin necessary
|
||||
% Settings.SSH.host = '<hostname or ip>'
|
||||
% Settings.SSH.bin = '<path_to_openEMS>/openEMS.sh'
|
||||
% ssh optional:
|
||||
% Settings.SSH.host_list = {'list','of','hosts'}; %searches for a free host
|
||||
% %on Windows needed additionally
|
||||
% Settings.SSH.Putty.Path = '<path_to>\putty';
|
||||
% Settings.SSH.Putty.Key = '<path_to>\putty_private_key.ppk';
|
||||
%
|
||||
% optional MPI:
|
||||
% Settings.MPI.xxx --> help RunOpenEMS_MPI
|
||||
@ -48,17 +52,40 @@ if (isfield(Settings,'MPI') && isunix)
|
||||
end
|
||||
end
|
||||
|
||||
ssh_command = 'ssh';
|
||||
scp_command = 'scp';
|
||||
scp_options = '';
|
||||
ssh_options = '';
|
||||
|
||||
enable_ssh = 0;
|
||||
enable_ssh = isfield(Settings,'SSH') && isunix;
|
||||
|
||||
if ~isunix
|
||||
enable_ssh = isfield(Settings,'SSH') && isfield(Settings.SSH,'Putty');
|
||||
if (enable_ssh)
|
||||
ssh_command = [Settings.SSH.Putty.Path '/plink '];
|
||||
ssh_options = [ssh_options ' -i ' Settings.SSH.Putty.Key];
|
||||
|
||||
scp_command = [Settings.SSH.Putty.Path '/pscp '];
|
||||
scp_options = [scp_options ' -i ' Settings.SSH.Putty.Key];
|
||||
end
|
||||
end
|
||||
|
||||
savePath = pwd;
|
||||
cd(Sim_Path);
|
||||
|
||||
if (isfield(Settings,'SSH') && isunix)
|
||||
% ssh options: no X forwarding; no password prompt (use pub keys!); no
|
||||
% host checking
|
||||
scp_options = '-C -o "PasswordAuthentication no" -o "StrictHostKeyChecking no"';
|
||||
ssh_options = [scp_options ' -x'];
|
||||
|
||||
if (enable_ssh)
|
||||
scp_options = [scp_options ' -C'];
|
||||
ssh_options = [ssh_options ' -x -C'];
|
||||
|
||||
% ssh options: no X forwarding; no password prompt (use pub keys!); no host checking
|
||||
if (isunix)
|
||||
ssh_options = [ssh_options ' -o "PasswordAuthentication no" -o "StrictHostKeyChecking no"'];
|
||||
scp_options = [scp_options ' -o "PasswordAuthentication no" -o "StrictHostKeyChecking no"'];
|
||||
end
|
||||
|
||||
if isfield(Settings.SSH,'host_list')
|
||||
host = FindFreeSSH(Settings.SSH.host_list);
|
||||
host = FindFreeSSH(Settings.SSH.host_list, Settings);
|
||||
if ~isempty(host)
|
||||
Settings.SSH.host = host;
|
||||
else
|
||||
@ -67,7 +94,7 @@ if (isfield(Settings,'SSH') && isunix)
|
||||
end
|
||||
|
||||
% create a tmp working dir
|
||||
[status, result] = unix(['ssh ' ssh_options ' ' Settings.SSH.host ' "mktemp -d /tmp/openEMS_XXXXXXXXXXXX"']);
|
||||
[status, result] = unix([ssh_command ' ' ssh_options ' ' Settings.SSH.host ' "mktemp -d /tmp/openEMS_XXXXXXXXXXXX"']);
|
||||
if (status~=0)
|
||||
disp(result);
|
||||
error('openEMS:RunOpenEMS','mktemp failed to create tmp directory!');
|
||||
@ -77,19 +104,19 @@ if (isfield(Settings,'SSH') && isunix)
|
||||
disp(['Running remote openEMS on ' Settings.SSH.host ' at working dir: ' ssh_work_path]);
|
||||
|
||||
%copy openEMS all simulation files to the ssh host
|
||||
[stat, res] = unix(['scp ' scp_options ' * ' Settings.SSH.host ':' ssh_work_path '/']);
|
||||
[stat, res] = unix([scp_command ' ' scp_options ' * ' Settings.SSH.host ':' ssh_work_path '/']);
|
||||
if (stat~=0)
|
||||
disp(res);
|
||||
error('openEMS:RunOpenEMS','scp failed!');
|
||||
end
|
||||
|
||||
%run openEMS (with log file if requested)
|
||||
if isfield(Settings,'LogFile')
|
||||
if isfield(Settings,'LogFile') && isunix
|
||||
append_unix = [' 2>&1 | tee ' Settings.LogFile];
|
||||
else
|
||||
append_unix = [];
|
||||
end
|
||||
status = unix(['ssh ' ssh_options ' ' Settings.SSH.host ' "cd ' ssh_work_path ' && ' Settings.SSH.bin ' ' Sim_File ' ' opts '"' append_unix]);
|
||||
status = unix([ssh_command ' ' ssh_options ' ' Settings.SSH.host ' "cd ' ssh_work_path ' && ' Settings.SSH.bin ' ' Sim_File ' ' opts '"' append_unix]);
|
||||
if (status~=0)
|
||||
disp(result);
|
||||
error('openEMS:RunOpenEMS','ssh openEMS failed!');
|
||||
@ -98,14 +125,14 @@ if (isfield(Settings,'SSH') && isunix)
|
||||
disp( 'Remote simulation done... copying back results and cleaning up...' );
|
||||
|
||||
%copy back all results
|
||||
[stat, res] = unix(['scp -r ' scp_options ' ' Settings.SSH.host ':' ssh_work_path '/* ''' pwd '''/']);
|
||||
[stat, res] = unix([scp_command ' -r ' scp_options ' ' Settings.SSH.host ':' ssh_work_path '/* ' pwd '/']);
|
||||
if (stat~=0);
|
||||
disp(res);
|
||||
error('openEMS:RunOpenEMS','scp failed!');
|
||||
end
|
||||
|
||||
%cleanup
|
||||
[stat, res] = unix(['ssh ' ssh_options ' ' Settings.SSH.host ' rm -r ' ssh_work_path]);
|
||||
[stat, res] = unix([ssh_command ' ' ssh_options ' ' Settings.SSH.host ' rm -r ' ssh_work_path]);
|
||||
if (stat~=0);
|
||||
disp(res);
|
||||
warning('openEMS:RunOpenEMS','remote cleanup failed!');
|
||||
|
Loading…
Reference in New Issue
Block a user