author | wenzelm |
Sun, 16 Oct 2016 16:58:09 +0200 | |
changeset 64254 | b1aef25ce8df |
parent 64233 | ef6f7e8a018c |
child 64256 | c3197aeae90b |
permissions | -rw-r--r-- |
64123 | 1 |
/* Title: Pure/General/ssh.scala |
2 |
Author: Makarius |
|
3 |
||
64124 | 4 |
SSH client based on JSch (see also http://www.jcraft.com/jsch/examples). |
64123 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
10 |
import java.io.{InputStream, OutputStream, ByteArrayOutputStream} |
64131 | 11 |
|
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
12 |
import scala.collection.{mutable, JavaConversions} |
64131 | 13 |
|
64222 | 14 |
import com.jcraft.jsch.{JSch, Logger => JSch_Logger, Session => JSch_Session, SftpException, |
64132 | 15 |
OpenSSHConfig, UserInfo, Channel => JSch_Channel, ChannelExec, ChannelSftp, SftpATTRS} |
64123 | 16 |
|
17 |
||
18 |
object SSH |
|
19 |
{ |
|
64185 | 20 |
/* target machine: user@host syntax */ |
64141 | 21 |
|
64185 | 22 |
object Target |
64141 | 23 |
{ |
24 |
val Pattern = "^([^@]+)@(.+)$".r |
|
25 |
||
26 |
def parse(s: String): (String, String) = |
|
27 |
s match { |
|
28 |
case Pattern(user, host) => (user, host) |
|
29 |
case _ => ("", s) |
|
30 |
} |
|
31 |
||
32 |
def unapplySeq(s: String): Option[List[String]] = |
|
64185 | 33 |
parse(s) match { |
34 |
case (_, "") => None |
|
35 |
case (user, host) => Some(List(user, host)) |
|
36 |
} |
|
64141 | 37 |
} |
38 |
||
64142 | 39 |
val default_port = 22 |
40 |
||
64141 | 41 |
|
64123 | 42 |
/* init */ |
43 |
||
64130 | 44 |
def init(options: Options): SSH = |
64123 | 45 |
{ |
64130 | 46 |
val config_dir = Path.explode(options.string("ssh_config_dir")) |
64123 | 47 |
if (!config_dir.is_dir) error("Bad ssh config directory: " + config_dir) |
48 |
||
49 |
val jsch = new JSch |
|
50 |
||
64130 | 51 |
val config_file = Path.explode(options.string("ssh_config_file")) |
64123 | 52 |
if (config_file.is_file) |
53 |
jsch.setConfigRepository(OpenSSHConfig.parseFile(File.platform_path(config_file))) |
|
54 |
||
55 |
val known_hosts = config_dir + Path.explode("known_hosts") |
|
56 |
if (!known_hosts.is_file) known_hosts.file.createNewFile |
|
57 |
jsch.setKnownHosts(File.platform_path(known_hosts)) |
|
58 |
||
64130 | 59 |
val identity_files = |
60 |
Library.space_explode(':', options.string("ssh_identity_files")).map(Path.explode(_)) |
|
64123 | 61 |
for (identity_file <- identity_files if identity_file.is_file) |
62 |
jsch.addIdentity(File.platform_path(identity_file)) |
|
63 |
||
64130 | 64 |
new SSH(options, jsch) |
64123 | 65 |
} |
66 |
||
64130 | 67 |
def connect_timeout(options: Options): Int = |
68 |
options.seconds("ssh_connect_timeout").ms.toInt |
|
69 |
||
64123 | 70 |
|
71 |
/* logging */ |
|
72 |
||
73 |
def logging(verbose: Boolean = true, debug: Boolean = false) |
|
74 |
{ |
|
75 |
JSch.setLogger(if (verbose) new Logger(debug) else null) |
|
76 |
} |
|
77 |
||
78 |
private class Logger(debug: Boolean) extends JSch_Logger |
|
79 |
{ |
|
80 |
def isEnabled(level: Int): Boolean = level != JSch_Logger.DEBUG || debug |
|
81 |
||
82 |
def log(level: Int, msg: String) |
|
83 |
{ |
|
84 |
level match { |
|
85 |
case JSch_Logger.ERROR | JSch_Logger.FATAL => Output.error_message(msg) |
|
86 |
case JSch_Logger.WARN => Output.warning(msg) |
|
87 |
case _ => Output.writeln(msg) |
|
88 |
} |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
||
64128 | 93 |
/* user info */ |
94 |
||
95 |
object No_User_Info extends UserInfo |
|
96 |
{ |
|
97 |
def getPassphrase: String = null |
|
98 |
def getPassword: String = null |
|
99 |
def promptPassword(msg: String): Boolean = false |
|
100 |
def promptPassphrase(msg: String): Boolean = false |
|
101 |
def promptYesNo(msg: String): Boolean = false |
|
102 |
def showMessage(msg: String): Unit = Output.writeln(msg) |
|
103 |
} |
|
104 |
||
105 |
||
64132 | 106 |
/* channel */ |
64129 | 107 |
|
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
108 |
class Channel[C <: JSch_Channel] private[SSH]( |
64166 | 109 |
val session: Session, val kind: String, val channel: C) |
64129 | 110 |
{ |
64130 | 111 |
override def toString: String = kind + " " + session.toString |
64129 | 112 |
|
64142 | 113 |
def close() { channel.disconnect } |
64129 | 114 |
} |
115 |
||
64132 | 116 |
|
64191 | 117 |
/* Sftp channel */ |
118 |
||
119 |
type Attrs = SftpATTRS |
|
120 |
||
64233 | 121 |
sealed case class Dir_Entry(name: Path, attrs: Attrs) |
64191 | 122 |
{ |
123 |
def is_file: Boolean = attrs.isReg |
|
124 |
def is_dir: Boolean = attrs.isDir |
|
125 |
} |
|
126 |
||
127 |
class Sftp private[SSH](session: Session, kind: String, channel: ChannelSftp) |
|
128 |
extends Channel[ChannelSftp](session, kind, channel) |
|
129 |
{ |
|
130 |
channel.connect(connect_timeout(session.options)) |
|
131 |
||
64228
b46969a851a9
expand relatively to given environment, notably remote HOME;
wenzelm
parents:
64226
diff
changeset
|
132 |
val settings: Map[String, String] = |
b46969a851a9
expand relatively to given environment, notably remote HOME;
wenzelm
parents:
64226
diff
changeset
|
133 |
{ |
b46969a851a9
expand relatively to given environment, notably remote HOME;
wenzelm
parents:
64226
diff
changeset
|
134 |
val home = channel.getHome |
b46969a851a9
expand relatively to given environment, notably remote HOME;
wenzelm
parents:
64226
diff
changeset
|
135 |
Map("HOME" -> home, "USER_HOME" -> home) |
b46969a851a9
expand relatively to given environment, notably remote HOME;
wenzelm
parents:
64226
diff
changeset
|
136 |
} |
64233 | 137 |
def expand_path(path: Path): Path = path.expand_env(settings) |
138 |
def remote_path(path: Path): String = expand_path(path).implode |
|
64191 | 139 |
|
64233 | 140 |
def chmod(permissions: Int, path: Path): Unit = channel.chmod(permissions, remote_path(path)) |
141 |
def mv(path1: Path, path2: Path): Unit = channel.rename(remote_path(path1), remote_path(path2)) |
|
142 |
def rm(path: Path): Unit = channel.rm(remote_path(path)) |
|
143 |
def mkdir(path: Path): Unit = channel.mkdir(remote_path(path)) |
|
144 |
def rmdir(path: Path): Unit = channel.rmdir(remote_path(path)) |
|
64191 | 145 |
|
64233 | 146 |
def stat(path: Path): Option[Dir_Entry] = |
147 |
try { Some(Dir_Entry(expand_path(path), channel.stat(remote_path(path)))) } |
|
64222 | 148 |
catch { case _: SftpException => None } |
149 |
||
64233 | 150 |
def is_file(path: Path): Boolean = stat(path).map(_.is_file) getOrElse false |
151 |
def is_dir(path: Path): Boolean = stat(path).map(_.is_dir) getOrElse false |
|
64191 | 152 |
|
64254 | 153 |
def mkdirs(path: Path): Unit = |
154 |
if (!is_dir(path)) { |
|
155 |
session.execute( |
|
156 |
"perl -e \"use File::Path make_path; make_path('" + remote_path(path) + "');\"") |
|
157 |
if (!is_dir(path)) error("Failed to create directory: " + quote(remote_path(path))) |
|
158 |
} |
|
159 |
||
64233 | 160 |
def read_dir(path: Path): List[Dir_Entry] = |
64191 | 161 |
{ |
64233 | 162 |
val dir = channel.ls(remote_path(path)) |
64191 | 163 |
(for { |
164 |
i <- (0 until dir.size).iterator |
|
165 |
a = dir.get(i).asInstanceOf[AnyRef] |
|
166 |
name = Untyped.get[String](a, "filename") |
|
167 |
attrs = Untyped.get[Attrs](a, "attrs") |
|
168 |
if name != "." && name != ".." |
|
64233 | 169 |
} yield Dir_Entry(Path.basic(name), attrs)).toList |
64191 | 170 |
} |
171 |
||
64233 | 172 |
def find_files(root: Path, pred: Dir_Entry => Boolean = _ => true): List[Dir_Entry] = |
64191 | 173 |
{ |
64233 | 174 |
def find(dir: Path): List[Dir_Entry] = |
64191 | 175 |
read_dir(dir).flatMap(entry => |
176 |
{ |
|
64233 | 177 |
val file = dir + entry.name |
64191 | 178 |
if (entry.is_dir) find(file) |
179 |
else if (pred(entry)) List(entry.copy(name = file)) |
|
180 |
else Nil |
|
181 |
}) |
|
64233 | 182 |
find(root) |
64191 | 183 |
} |
184 |
||
64233 | 185 |
def open_input(path: Path): InputStream = channel.get(remote_path(path)) |
186 |
def open_output(path: Path): OutputStream = channel.put(remote_path(path)) |
|
64191 | 187 |
|
64233 | 188 |
def read_file(path: Path, local_path: Path): Unit = |
189 |
channel.get(remote_path(path), File.platform_path(local_path)) |
|
190 |
def read_bytes(path: Path): Bytes = using(open_input(path))(Bytes.read_stream(_)) |
|
191 |
def read(path: Path): String = using(open_input(path))(File.read_stream(_)) |
|
64191 | 192 |
|
64233 | 193 |
def write_file(path: Path, local_path: Path): Unit = |
194 |
channel.put(File.platform_path(local_path), remote_path(path)) |
|
195 |
def write_bytes(path: Path, bytes: Bytes): Unit = |
|
196 |
using(open_output(path))(bytes.write_stream(_)) |
|
197 |
def write(path: Path, text: String): Unit = |
|
198 |
using(open_output(path))(stream => Bytes(text).write_stream(stream)) |
|
64191 | 199 |
} |
200 |
||
201 |
||
64132 | 202 |
/* exec channel */ |
203 |
||
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
204 |
private val exec_wait_delay = Time.seconds(0.3) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
205 |
|
64166 | 206 |
class Exec private[SSH](session: Session, kind: String, channel: ChannelExec) |
207 |
extends Channel[ChannelExec](session, kind, channel) |
|
64131 | 208 |
{ |
209 |
def kill(signal: String) { channel.sendSignal(signal) } |
|
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
210 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
211 |
val exit_status: Future[Int] = |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
212 |
Future.thread("ssh_wait") { |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
213 |
while (!channel.isClosed) Thread.sleep(exec_wait_delay.ms) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
214 |
channel.getExitStatus |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
215 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
216 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
217 |
val stdin: OutputStream = channel.getOutputStream |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
218 |
val stdout: InputStream = channel.getInputStream |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
219 |
val stderr: InputStream = channel.getErrStream |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
220 |
|
64166 | 221 |
// connect after preparing streams |
222 |
channel.connect(connect_timeout(session.options)) |
|
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
223 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
224 |
def result( |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
225 |
progress_stdout: String => Unit = (_: String) => (), |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
226 |
progress_stderr: String => Unit = (_: String) => (), |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
227 |
strict: Boolean = true): Process_Result = |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
228 |
{ |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
229 |
stdin.close |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
230 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
231 |
def read_lines(stream: InputStream, progress: String => Unit): List[String] = |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
232 |
{ |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
233 |
val result = new mutable.ListBuffer[String] |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
234 |
val line_buffer = new ByteArrayOutputStream(100) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
235 |
def line_flush() |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
236 |
{ |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
237 |
val line = line_buffer.toString(UTF8.charset_name) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
238 |
progress(line) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
239 |
result += line |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
240 |
line_buffer.reset |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
241 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
242 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
243 |
var c = 0 |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
244 |
var finished = false |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
245 |
while (!finished) { |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
246 |
while ({ c = stream.read; c != -1 && c != 10 }) line_buffer.write(c) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
247 |
if (c == 10) line_flush() |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
248 |
else if (channel.isClosed) { |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
249 |
if (line_buffer.size > 0) line_flush() |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
250 |
finished = true |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
251 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
252 |
else Thread.sleep(exec_wait_delay.ms) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
253 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
254 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
255 |
result.toList |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
256 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
257 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
258 |
val out_lines = Future.thread("ssh_stdout") { read_lines(stdout, progress_stdout) } |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
259 |
val err_lines = Future.thread("ssh_stderr") { read_lines(stderr, progress_stderr) } |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
260 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
261 |
def terminate() |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
262 |
{ |
64136 | 263 |
close |
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
264 |
out_lines.join |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
265 |
err_lines.join |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
266 |
exit_status.join |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
267 |
} |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
268 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
269 |
val rc = |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
270 |
try { exit_status.join } |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
271 |
catch { case Exn.Interrupt() => terminate(); Exn.Interrupt.return_code } |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
272 |
|
64136 | 273 |
close |
64134
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
274 |
if (strict && rc == Exn.Interrupt.return_code) throw Exn.Interrupt() |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
275 |
|
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
276 |
Process_Result(rc, out_lines.join, err_lines.join) |
57581e4026fe
proper support for exec channel (see also bash.scala);
wenzelm
parents:
64133
diff
changeset
|
277 |
} |
64131 | 278 |
} |
279 |
||
280 |
||
64123 | 281 |
/* session */ |
282 |
||
64166 | 283 |
class Session private[SSH](val options: Options, val session: JSch_Session) |
64123 | 284 |
{ |
64166 | 285 |
def update_options(new_options: Options): Session = new Session(new_options, session) |
286 |
||
64123 | 287 |
override def toString: String = |
288 |
(if (session.getUserName == null) "" else session.getUserName + "@") + |
|
289 |
(if (session.getHost == null) "" else session.getHost) + |
|
64142 | 290 |
(if (session.getPort == default_port) "" else ":" + session.getPort) + |
64126 | 291 |
(if (session.isConnected) "" else " (disconnected)") |
64123 | 292 |
|
64142 | 293 |
def close() { session.disconnect } |
64123 | 294 |
|
64191 | 295 |
def sftp(): Sftp = |
296 |
{ |
|
297 |
val kind = "sftp" |
|
298 |
val channel = session.openChannel(kind).asInstanceOf[ChannelSftp] |
|
299 |
new Sftp(this, kind, channel) |
|
300 |
} |
|
64135
865dda40e1cc
provide execute operation, similar to Isabelle_System.bash;
wenzelm
parents:
64134
diff
changeset
|
301 |
|
64166 | 302 |
def exec(command: String): Exec = |
64129 | 303 |
{ |
64130 | 304 |
val kind = "exec" |
305 |
val channel = session.openChannel(kind).asInstanceOf[ChannelExec] |
|
64190
c62b99e3ec07
provide USER_HOME, such that symbolic Path.explode("~") can be used remotely;
wenzelm
parents:
64185
diff
changeset
|
306 |
channel.setCommand("export USER_HOME=\"$HOME\"\n" + command) |
64166 | 307 |
new Exec(this, kind, channel) |
64129 | 308 |
} |
64123 | 309 |
|
64191 | 310 |
def execute(command: String, |
311 |
progress_stdout: String => Unit = (_: String) => (), |
|
312 |
progress_stderr: String => Unit = (_: String) => (), |
|
313 |
strict: Boolean = true): Process_Result = |
|
314 |
exec(command).result(progress_stdout, progress_stderr, strict) |
|
64137 | 315 |
|
316 |
||
317 |
/* tmp dirs */ |
|
318 |
||
319 |
def rm_tree(remote_dir: String): Unit = |
|
320 |
execute("rm -r -f " + File.bash_string(remote_dir)).check |
|
321 |
||
322 |
def tmp_dir(): String = |
|
323 |
execute("mktemp -d -t tmp.XXXXXXXXXX").check.out |
|
324 |
||
64233 | 325 |
def with_tmp_dir[A](body: Path => A): A = |
64137 | 326 |
{ |
327 |
val remote_dir = tmp_dir() |
|
64233 | 328 |
try { body(Path.explode(remote_dir)) } finally { rm_tree(remote_dir) } |
64137 | 329 |
} |
64123 | 330 |
} |
331 |
} |
|
332 |
||
64130 | 333 |
class SSH private(val options: Options, val jsch: JSch) |
64123 | 334 |
{ |
64166 | 335 |
def update_options(new_options: Options): SSH = new SSH(new_options, jsch) |
336 |
||
64226 | 337 |
def open_session(host: String, user: String = "", port: Int = SSH.default_port): SSH.Session = |
64123 | 338 |
{ |
64141 | 339 |
val session = jsch.getSession(if (user == "") null else user, host, port) |
64125
a034dac5ca3c
clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents:
64124
diff
changeset
|
340 |
|
a034dac5ca3c
clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents:
64124
diff
changeset
|
341 |
session.setUserInfo(SSH.No_User_Info) |
a034dac5ca3c
clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents:
64124
diff
changeset
|
342 |
session.setConfig("MaxAuthTries", "3") |
a034dac5ca3c
clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents:
64124
diff
changeset
|
343 |
|
64130 | 344 |
if (options.bool("ssh_compression")) { |
64123 | 345 |
session.setConfig("compression.s2c", "zlib@openssh.com,zlib,none") |
346 |
session.setConfig("compression.c2s", "zlib@openssh.com,zlib,none") |
|
347 |
session.setConfig("compression_level", "9") |
|
348 |
} |
|
64125
a034dac5ca3c
clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents:
64124
diff
changeset
|
349 |
|
64130 | 350 |
session.connect(SSH.connect_timeout(options)) |
351 |
new SSH.Session(options, session) |
|
64123 | 352 |
} |
353 |
} |