Пример #1
0
        public IActionResult Execute([FromForm] string ip, [FromForm] string rootUser, [FromForm] string rootPassword)
        {
            var directory = "/agents";
            var filePath  = Path.Combine(directory, "remote-agent");

            _commandExecutor.AddSSHKey(ip, rootUser, rootPassword);
            _commandExecutor.ExecuteCommandSSH(ip, rootUser, $"mkdir -p {directory}");
            _commandExecutor.Scp(ip, rootUser, filePath, filePath);
            _commandExecutor.ExecuteCommandSSH(ip, rootUser, $"chmod +x {filePath}");
            _commandExecutor.ExecuteCommandSSH(ip, rootUser, $"{filePath} config set -i {_serverIp} -p {_serverPort}");
            _commandExecutor.ExecuteCommandSSH(ip, rootUser, $"nohup {filePath} -d &", true);
            _commandExecutor.RemoveSSHKey(ip, rootUser);

            return(Ok());
        }
Пример #2
0
        public IActionResult Execute([FromForm] string ip, [FromForm] string rootUser, [FromForm] string rootPassword, [FromForm] string command)
        {
            var result = $"{rootUser}@{ip}:{command}\r\n";

            try
            {
                _commandExecutor.AddSSHKey(ip, rootUser, rootPassword);
                result += _commandExecutor.ExecuteCommandSSH(ip, rootUser, command);
                _commandExecutor.RemoveSSHKey(ip, rootUser);
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }

            return(Ok(result));
        }
Пример #3
0
        private IActionResult ExecuteShell(string ip, string rootUser, string rootPassword, GeneratorResult shellResult)
        {
            var result = $"{rootUser}@{ip}:./{shellResult.FileName}\r\n";

            try
            {
                var source = shellResult.FilePath;
                var target = $"/root/{shellResult.FileName}";
                _commandExecutor.AddSSHKey(ip, rootUser, rootPassword);
                _commandExecutor.Scp(ip, rootUser, source, target);
                _commandExecutor.ExecuteCommandSSH(ip, rootUser, $"chmod +x {target}");
                result += _commandExecutor.ExecuteCommandSSH(ip, rootUser, target);
                _commandExecutor.ExecuteCommandSSH(ip, rootUser, $"rm -f {target}");
                _commandExecutor.RemoveSSHKey(ip, rootUser);
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }

            return(Ok(result));
        }
Пример #4
0
        public async Task <IActionResult> Execute([FromForm] string ip, [FromForm] string rootUser, [FromForm] string rootPassword, IFormFile file)
        {
            var result = $"{rootUser}@{ip}:./{file.FileName}\r\n";

            var directory = Path.Combine("/files", ip);

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            var filePath = Path.Combine(directory, file.FileName);

            await using (var stream = System.IO.File.Create(filePath))
            {
                await file.CopyToAsync(stream);
            }

            try
            {
                var source = filePath;
                var target = $"/root/{file.FileName}";

                _commandExecutor.AddSSHKey(ip, rootUser, rootPassword);
                _commandExecutor.Scp(ip, rootUser, source, target);
                _commandExecutor.ExecuteCommandSSH(ip, rootUser, $"chmod +x {target}");
                result += _commandExecutor.ExecuteCommandSSH(ip, rootUser, target);
                _commandExecutor.ExecuteCommandSSH(ip, rootUser, $"rm -f {target}");
                _commandExecutor.RemoveSSHKey(ip, rootUser);
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }

            return(Ok(result));
        }