示例#1
0
        public void CreateOrUpdate(Gamelet gamelet)
        {
            var sshDir = Path.GetDirectoryName(path);

            try
            {
                Directory.CreateDirectory(sshDir);
            }
            catch (Exception e)
            {
                Trace.WriteLine($"Failed to create ssh directory: {e}");
                throw new SshKeyException(ErrorStrings.FailedToCreateSshDirectory(e.Message));
            }

            var target = new SshTarget(gamelet);
            var host   = gamelet.IpAddr;

            if (target.Port != 22)
            {
                host = $"[{host}]:{target.Port}";
            }

            var lines = gamelet.PublicKeys.Select(hostKey =>
                                                  $"{host} {AlgorithmToString(hostKey.Algorithm)} {hostKey.PublicKey}");

            try
            {
                File.WriteAllLines(path, lines);
            }
            catch (Exception e)
            {
                Trace.WriteLine($"Failed to write known_hosts file: {e}");
                throw new SshKnownHostsException(
                          ErrorStrings.FailedToWriteKnownHostsFile(e.Message));
            }
        }