-
Notifications
You must be signed in to change notification settings - Fork 0
/
SSHKernel.wl
54 lines (41 loc) · 1.26 KB
/
SSHKernel.wl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
(* ::Package:: *)
BeginPackage["SSHKernel`"]
SSHKernel::usage=
"SSHKernel will launch a remote kernel over SSH, using the system PATH ssh which is assumed to have password-less login to the remote machine configured."
Begin["`Private`"]
LinkPorts=Function[
{link},
InputForm[link][[1,1]]//(Reverse[StringSplit[#,"@"]]&)/@StringSplit[#,","]&
];
SSHKernel[hostname0_]:=Block[
{hostname=hostname0},
Module[
{link, proc, mappedPorts},
link=LinkCreate[LinkProtocol->"TCPIP"];
proc=StartProcess[Join@@{
{"ssh","-tt"},
StringRiffle[Prepend[#,"-R0"],":"]&/@LinkPorts[link],
{hostname}
}];
Pause[0.5];
mappedPorts=ReadString[
ProcessConnection[proc,"StandardError"],
EndOfBuffer]//Select[
StringSplit[#,"\n"],
StringMatchQ[___~~"Allocated port"~~___]
]&//(
Rule@@StringSplit[
StringTrim[#],{" ",":"}
][[{9,3}]])&/@#&//Association;
WriteLine[
proc,
"/opt/Wolfram/WolframEngine/11.2/Executables/wolfram -wstp -LinkMode Connect -LinkProtocol TCPIP -LinkName "
<>StringRiffle[mappedPorts[#]
<>"@127.0.0.1"&/@LinkPorts[link][[All,2]],","]
<>" -LinkHost 127.0.0.1 -subkernel; exit"];
Needs["SubKernels`LinkKernels`"];
LaunchKernels[link]
]
];
End[]
EndPackage[]