I've an Azure function app written in C# which runs in dotnet 8 isolated mode. The function app is hosted under a Linux app service plan and needs to invoke smbclient command. This is what my code looks like:
public class FileShareDownload
{
private readonly ILogger<FileShareDownload> _logger;
public FileShareDownload(ILogger<FileShareDownload> logger)
{
_logger = logger;
}
[Function("FileShareDownload")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
{
_logger.LogInformation("Processing GetFileList request.");
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var fileShareRequest = System.Text.Json.JsonSerializer.Deserialize<FileShareDownloadRequest>(requestBody);
if (fileShareRequest == null
|| string.IsNullOrEmpty(fileShareRequest.FileShareBasePath)
|| string.IsNullOrEmpty(fileShareRequest.FileShareDirectoryPath)
|| string.IsNullOrEmpty(fileShareRequest.Username)
|| string.IsNullOrEmpty(fileShareRequest.Password))
{
return new BadRequestObjectResult("Invalid request.");
}
// Use smbclient to list files in the file share path
string smbClientCommand = $"smbclient {fileShareRequest.FileShareBasePath} -U {fileShareRequest.Username}%{fileShareRequest.Password} -c \"cd {fileShareRequest.FileShareDirectoryPath.Replace("/", "; cd ")} ; ls\" -d 2";
_logger.LogWarning("Executing Command: " + smbClientCommand);
_logger.LogWarning("Result of date Process " + RunCommandWithBash("date"));
_logger.LogWarning("Result of help Process " + RunCommandWithBash("help"));
var processInfo = new ProcessStartInfo("bash", $"-c \"{smbClientCommand}\"")
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = new Process
{
StartInfo = processInfo
};
process.Start();
//string result = await process.StandardOutput.ReadToEndAsync();
//string error = await process.StandardError.ReadToEndAsync();
string result = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
_logger.LogWarning($"smbclient standard output: {result}");
_logger.LogWarning($"smbclient standard error: {error}");
if (process.ExitCode != 0)
{
_logger.LogError($"smbclient error: {error}");
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
}
result = RunCommandWithBash(smbClientCommand);
_logger.LogWarning("New result: " + result);
_logger.LogWarning($"Received response: {result}");
return new OkObjectResult(result);
}
public string RunCommandWithBash(string command)
{
var psi = new ProcessStartInfo();
psi.FileName = "/bin/bash";
psi.Arguments = command;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
using var process = Process.Start(psi);
process.WaitForExit(30000);
var output = process.StandardOutput.ReadToEnd();
return output;
}
}
When I test the function app, the response from running the bash commands is always blank. Here is what it looks like:
When I copy paste the command and execute it inside kudu ssh console, it works fine and gives me this response:
2:
Can someone please explain why is my function app either not executing the bash commands properly or not showing me the output correctly?
A bit of extra background, we needed a function app that would connect to file shares on Azure VMs and read files. We first tried making the function app which used mpr.dll to connect to file shares under Windows App Service Plan but discovered that the Azure Sandbox for Windows App service plan blocks connectivity on port 445 which is used for SMB. We were advised by Microsoft to switch to Linux App Service Plan because it does not have this limitation however we can't use mpr.dll on linux and have to use smbclient instead.