author | wenzelm |
Mon, 10 Oct 2016 16:04:57 +0200 | |
changeset 64132 | c2594513687b |
parent 64131 | f01fca58e0a5 |
child 64133 | e8407039b572 |
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 |
||
64131 | 10 |
import java.io.{InputStream, OutputStream} |
11 |
||
12 |
import scala.collection.JavaConversions |
|
13 |
||
64123 | 14 |
import com.jcraft.jsch.{JSch, Logger => JSch_Logger, Session => JSch_Session, |
64132 | 15 |
OpenSSHConfig, UserInfo, Channel => JSch_Channel, ChannelExec, ChannelSftp, SftpATTRS} |
64123 | 16 |
|
17 |
||
18 |
object SSH |
|
19 |
{ |
|
20 |
/* init */ |
|
21 |
||
64130 | 22 |
def init(options: Options): SSH = |
64123 | 23 |
{ |
64130 | 24 |
val config_dir = Path.explode(options.string("ssh_config_dir")) |
64123 | 25 |
if (!config_dir.is_dir) error("Bad ssh config directory: " + config_dir) |
26 |
||
27 |
val jsch = new JSch |
|
28 |
||
64130 | 29 |
val config_file = Path.explode(options.string("ssh_config_file")) |
64123 | 30 |
if (config_file.is_file) |
31 |
jsch.setConfigRepository(OpenSSHConfig.parseFile(File.platform_path(config_file))) |
|
32 |
||
33 |
val known_hosts = config_dir + Path.explode("known_hosts") |
|
34 |
if (!known_hosts.is_file) known_hosts.file.createNewFile |
|
35 |
jsch.setKnownHosts(File.platform_path(known_hosts)) |
|
36 |
||
64130 | 37 |
val identity_files = |
38 |
Library.space_explode(':', options.string("ssh_identity_files")).map(Path.explode(_)) |
|
64123 | 39 |
for (identity_file <- identity_files if identity_file.is_file) |
40 |
jsch.addIdentity(File.platform_path(identity_file)) |
|
41 |
||
64130 | 42 |
new SSH(options, jsch) |
64123 | 43 |
} |
44 |
||
64130 | 45 |
def connect_timeout(options: Options): Int = |
46 |
options.seconds("ssh_connect_timeout").ms.toInt |
|
47 |
||
64123 | 48 |
|
49 |
/* logging */ |
|
50 |
||
51 |
def logging(verbose: Boolean = true, debug: Boolean = false) |
|
52 |
{ |
|
53 |
JSch.setLogger(if (verbose) new Logger(debug) else null) |
|
54 |
} |
|
55 |
||
56 |
private class Logger(debug: Boolean) extends JSch_Logger |
|
57 |
{ |
|
58 |
def isEnabled(level: Int): Boolean = level != JSch_Logger.DEBUG || debug |
|
59 |
||
60 |
def log(level: Int, msg: String) |
|
61 |
{ |
|
62 |
level match { |
|
63 |
case JSch_Logger.ERROR | JSch_Logger.FATAL => Output.error_message(msg) |
|
64 |
case JSch_Logger.WARN => Output.warning(msg) |
|
65 |
case _ => Output.writeln(msg) |
|
66 |
} |
|
67 |
} |
|
68 |
} |
|
69 |
||
70 |
||
64128 | 71 |
/* user info */ |
72 |
||
73 |
object No_User_Info extends UserInfo |
|
74 |
{ |
|
75 |
def getPassphrase: String = null |
|
76 |
def getPassword: String = null |
|
77 |
def promptPassword(msg: String): Boolean = false |
|
78 |
def promptPassphrase(msg: String): Boolean = false |
|
79 |
def promptYesNo(msg: String): Boolean = false |
|
80 |
def showMessage(msg: String): Unit = Output.writeln(msg) |
|
81 |
} |
|
82 |
||
83 |
||
64132 | 84 |
/* channel */ |
64129 | 85 |
|
64130 | 86 |
class Channel[C <: JSch_Channel] private[SSH](val session: Session, |
87 |
val kind: String, val channel_options: Options, val channel: C) |
|
64129 | 88 |
{ |
64130 | 89 |
override def toString: String = kind + " " + session.toString |
64129 | 90 |
|
91 |
def close { channel.disconnect } |
|
92 |
} |
|
93 |
||
64132 | 94 |
|
95 |
/* exec channel */ |
|
96 |
||
64131 | 97 |
class Exec private[SSH]( |
98 |
session: Session, kind: String, channel_options: Options, channel: ChannelExec) |
|
99 |
extends Channel[ChannelExec](session, kind, channel_options, channel) |
|
100 |
{ |
|
101 |
def kill(signal: String) { channel.sendSignal(signal) } |
|
102 |
} |
|
103 |
||
104 |
||
64132 | 105 |
/* Sftp channel */ |
106 |
||
107 |
type Attrs = SftpATTRS |
|
108 |
||
109 |
sealed case class Dir_Entry(name: String, attrs: Attrs) |
|
110 |
{ |
|
111 |
def is_file: Boolean = attrs.isReg |
|
112 |
def is_dir: Boolean = attrs.isDir |
|
113 |
} |
|
114 |
||
64131 | 115 |
class Sftp private[SSH]( |
116 |
session: Session, kind: String, channel_options: Options, channel: ChannelSftp) |
|
117 |
extends Channel[ChannelSftp](session, kind, channel_options, channel) |
|
118 |
{ |
|
119 |
def home: String = channel.getHome() |
|
120 |
||
121 |
def chmod(permissions: Int, remote_path: String) { channel.chmod(permissions, remote_path) } |
|
122 |
def mv(remote_path1: String, remote_path2: String): Unit = |
|
123 |
channel.rename(remote_path1, remote_path2) |
|
124 |
def rm(remote_path: String) { channel.rm(remote_path) } |
|
125 |
def mkdir(remote_path: String) { channel.mkdir(remote_path) } |
|
126 |
def rmdir(remote_path: String) { channel.rmdir(remote_path) } |
|
127 |
||
64132 | 128 |
def stat(remote_path: String): Dir_Entry = |
129 |
Dir_Entry(remote_path, channel.stat(remote_path)) |
|
130 |
||
131 |
def read_dir(remote_path: String): List[Dir_Entry] = |
|
132 |
{ |
|
133 |
val dir = channel.ls(remote_path) |
|
134 |
(for { |
|
135 |
i <- (0 until dir.size).iterator |
|
136 |
a = dir.get(i).asInstanceOf[AnyRef] |
|
137 |
name = Untyped.get[String](a, "filename") |
|
138 |
attrs = Untyped.get[Attrs](a, "attrs") |
|
139 |
if name != "." && name != ".." |
|
140 |
} yield Dir_Entry(name, attrs)).toList |
|
141 |
} |
|
142 |
||
143 |
def find_files(remote_path: String, pred: Dir_Entry => Boolean = _ => true): List[Dir_Entry] = |
|
144 |
{ |
|
145 |
def find(dir: String): List[Dir_Entry] = |
|
146 |
read_dir(dir).flatMap(entry => |
|
147 |
{ |
|
148 |
val file = dir + "/" + entry.name |
|
149 |
if (entry.is_dir) find(file) else if (pred(entry)) List(entry) else Nil |
|
150 |
}) |
|
151 |
find(remote_path) |
|
152 |
} |
|
153 |
||
64131 | 154 |
def open_input(remote_path: String): InputStream = channel.get(remote_path) |
155 |
def open_output(remote_path: String): OutputStream = channel.put(remote_path) |
|
156 |
||
157 |
def read_file(remote_path: String, local_path: Path): Unit = |
|
158 |
channel.get(remote_path, File.platform_path(local_path)) |
|
159 |
def read_bytes(remote_path: String): Bytes = |
|
160 |
using(open_input(remote_path))(Bytes.read_stream(_)) |
|
161 |
def read(remote_path: String): String = |
|
162 |
using(open_input(remote_path))(File.read_stream(_)) |
|
163 |
||
164 |
def write_file(remote_path: String, local_path: Path): Unit = |
|
165 |
channel.put(File.platform_path(local_path), remote_path) |
|
166 |
def write_bytes(remote_path: String, bytes: Bytes): Unit = |
|
167 |
using(open_output(remote_path))(bytes.write_stream(_)) |
|
168 |
def write(remote_path: String, text: String): Unit = |
|
169 |
using(open_output(remote_path))(stream => Bytes(text).write_stream(stream)) |
|
170 |
} |
|
171 |
||
64129 | 172 |
|
64123 | 173 |
/* session */ |
174 |
||
64130 | 175 |
class Session private[SSH](val session_options: Options, val session: JSch_Session) |
64123 | 176 |
{ |
177 |
override def toString: String = |
|
178 |
(if (session.getUserName == null) "" else session.getUserName + "@") + |
|
179 |
(if (session.getHost == null) "" else session.getHost) + |
|
64126 | 180 |
(if (session.getPort == 22) "" else ":" + session.getPort) + |
181 |
(if (session.isConnected) "" else " (disconnected)") |
|
64123 | 182 |
|
64126 | 183 |
def close { session.disconnect } |
64123 | 184 |
|
64131 | 185 |
def exec(command: String, options: Options = session_options): Exec = |
64129 | 186 |
{ |
64130 | 187 |
val kind = "exec" |
188 |
val channel = session.openChannel(kind).asInstanceOf[ChannelExec] |
|
64129 | 189 |
channel.setCommand(command) |
190 |
||
64130 | 191 |
channel.connect(connect_timeout(options)) |
64131 | 192 |
new Exec(this, kind, options, channel) |
64129 | 193 |
} |
64123 | 194 |
|
64131 | 195 |
def sftp(options: Options = session_options): Sftp = |
64130 | 196 |
{ |
197 |
val kind = "sftp" |
|
198 |
val channel = session.openChannel(kind).asInstanceOf[ChannelSftp] |
|
199 |
||
200 |
channel.connect(connect_timeout(options)) |
|
64131 | 201 |
new Sftp(this, kind, options, channel) |
64130 | 202 |
} |
64123 | 203 |
} |
204 |
} |
|
205 |
||
64130 | 206 |
class SSH private(val options: Options, val jsch: JSch) |
64123 | 207 |
{ |
64130 | 208 |
def open_session(host: String, port: Int = 22, user: String = null): SSH.Session = |
64123 | 209 |
{ |
210 |
val session = jsch.getSession(user, host, port) |
|
64125
a034dac5ca3c
clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents:
64124
diff
changeset
|
211 |
|
a034dac5ca3c
clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents:
64124
diff
changeset
|
212 |
session.setUserInfo(SSH.No_User_Info) |
a034dac5ca3c
clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents:
64124
diff
changeset
|
213 |
session.setConfig("MaxAuthTries", "3") |
a034dac5ca3c
clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents:
64124
diff
changeset
|
214 |
|
64130 | 215 |
if (options.bool("ssh_compression")) { |
64123 | 216 |
session.setConfig("compression.s2c", "zlib@openssh.com,zlib,none") |
217 |
session.setConfig("compression.c2s", "zlib@openssh.com,zlib,none") |
|
218 |
session.setConfig("compression_level", "9") |
|
219 |
} |
|
64125
a034dac5ca3c
clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents:
64124
diff
changeset
|
220 |
|
64130 | 221 |
session.connect(SSH.connect_timeout(options)) |
222 |
new SSH.Session(options, session) |
|
64123 | 223 |
} |
224 |
} |