Esempio n. 1
0
        protected override void ProcessRecord()
        {
            //  Commandファイルから読み取り
            if ((this.Command == null || this.Command.Length == 0) &&
                !string.IsNullOrEmpty(this.CommandFile) && File.Exists(this.CommandFile))
            {
                string text = File.ReadAllText(this.CommandFile);
                this.Command = pattern_return.Split(text);
            }

            this.Session ??= new SshSession()
            {
                Server              = this.Server,
                Port                = this.Port,
                User                = this.User,
                Password            = this.Password,
                KeyboardInteractive = this.KeyboardInteractive,
                Effemeral           = true, //  コマンドパラメータでSession指定が無い場合、Effemeral。
            };

            var client = Session.CreateAndConnectSshClient();

            if (client.IsConnected)
            {
                foreach (string line in Command)
                {
                    SshCommand command = client.CreateCommand(line);
                    command.Execute();

                    /*
                     * List<string> splitResult = pattern_return.Split(command.Result).ToList();
                     *
                     * if (splitResult.Count > 0 && string.IsNullOrEmpty(splitResult[0]))
                     * {
                     *  splitResult.RemoveAt(0);
                     * }
                     * if (splitResult.Count > 0 && string.IsNullOrEmpty(splitResult[splitResult.Count - 1]))
                     * {
                     *  splitResult.RemoveAt(splitResult.Count - 1);
                     * }
                     */
                    var splitResult = pattern_return.Split(command.Result).Trim();

                    if (string.IsNullOrEmpty(this.OutputFile))
                    {
                        WriteObject(string.Join("\r\n", splitResult), true);
                    }
                    else
                    {
                        TargetDirectory.CreateParent(this.OutputFile);
                        using (var sw = new StreamWriter(OutputFile, true, new UTF8Encoding(false)))
                        {
                            sw.Write(string.Join("\r\n", splitResult));
                        }
                    }
                }
            }
            Session.CloseIfEffemeral();
        }
Esempio n. 2
0
        public void Init(string logDir, string logPreName)
        {
            _lock ??= new AsyncLock();

            string today = DateTime.Now.ToString("yyyyMMdd");

            _logFilePath = Path.Combine(logDir, $"{logPreName}_{today}.log");
            TargetDirectory.CreateParent(_logFilePath);
            _writer = new StreamWriter(_logFilePath, _logAppend, new UTF8Encoding(false));
        }