Repository Public
1import Foundation23/// Coarse cause of a failed `ssh` invocation.4public enum SSHErrorKind: String, Sendable, Codable, Hashable, CaseIterable {5 case hostKeyChanged6 case hostKeyUnknown7 case authenticationFailed8 case connectionRefused9 case timedOut10 case nameResolutionFailed11 case networkUnreachable12 case connectionClosed13 case unknown1415 /// Short user-facing explanation (English).16 public var explanation: String {17 switch self {18 case .hostKeyChanged:19 return "The server's host key differs from the one recorded in known_hosts. This can mean the server was reinstalled, or that someone is intercepting the connection. Verify the new fingerprint with the server's administrator before changing known_hosts."20 case .hostKeyUnknown:21 return "The server's host key could not be verified. Connect once in a terminal to review and accept its fingerprint."22 case .authenticationFailed:23 return "The server rejected the authentication. Check the user name, and that the right key is loaded in your SSH agent and authorized on the server."24 case .connectionRefused:25 return "The server refused the connection. The SSH service may be stopped or listening on another port."26 case .timedOut:27 return "The connection timed out. The host may be down, or a firewall may be dropping the traffic."28 case .nameResolutionFailed:29 return "The host name could not be resolved. Check the spelling and your DNS or VPN."30 case .networkUnreachable:31 return "There is no network route to the host. Check your network or VPN connection."32 case .connectionClosed:33 return "The connection was closed by the server or by the network."34 case .unknown:35 return "SSH failed for an unrecognized reason. See the error output for details."36 }37 }38}3940/// Maps ssh's stderr text and exit status to an ``SSHErrorKind``.41public enum SSHErrorClassifier {42 private static let rules: [(SSHErrorKind, [String])] = [43 (.hostKeyChanged, ["remote host identification has changed", "host key has changed", "offending key", "offending ecdsa key", "offending ed25519 key", "offending rsa key"]),44 (.hostKeyUnknown, ["host key verification failed", "can't be established", "no host key is known", "no matching host key"]),45 (.nameResolutionFailed, ["could not resolve hostname", "name or service not known", "nodename nor servname provided", "temporary failure in name resolution"]),46 (.connectionRefused, ["connection refused"]),47 (.networkUnreachable, ["network is unreachable", "no route to host", "host is down"]),48 (.timedOut, ["timed out", "timeout"]),49 (.authenticationFailed, ["permission denied", "too many authentication failures", "no more authentication methods", "authentication failed"]),50 (.connectionClosed, ["connection closed", "connection reset", "closed by remote host", "broken pipe", "kex_exchange_identification"]),51 ]5253 /// Returns `nil` when nothing looks like an ssh failure: exit status 0, or a status other than 25554 /// (the remote command's own status) without a recognizable ssh error message.55 public static func classify(stderr: String, exitStatus: Int32) -> SSHErrorKind? {56 guard exitStatus != 0 else { return nil }57 let text = stderr.lowercased()58 for (kind, needles) in rules where needles.contains(where: text.contains) {59 return kind60 }61 return exitStatus == 255 ? .unknown : nil62 }63}64