我在 Azure Linux 虚拟机中有一个 shell 脚本。 Azure Linux VM 中安装了 Sophos。shell 脚本可以使用 savscan 命令扫描目录中存在的文件。挑战是,我们使用的是azure数据工厂管道,它需要调用azure函数。 azure 函数应该能够 ssh 到 Linux 虚拟机,并执行脚本。该函数应传递 azure 共享文件存储的文件路径等参数,Sophos 需要在其中执行扫描。
我知道管道可以调用http触发的azure函数。但是我们如何 ssh 进入虚拟机并从 azure 函数远程运行脚本。文件路径的参数也将来自数据工厂。
inotifywait -mr -e close_write "/xyz/abc/" |
while read dir eve file; do
echo "new file '$path$file' detected - start scan"
savscan -eec $path$file
if [ $? -eq 0 ]
then
echo "1"
else
echo "0"
fi
done
最佳答案
您的 ScriptPath
参数似乎有问题。如果您使用的是Azure Automation,我们无法在其中放置静态脚本文件,但我们可以先下载脚本并将其放置在Azure Automation的“c:/temp”文件夹中。
我自己做了一些测试,我将脚本放置在Azure存储帐户中,在需要运行此脚本之前,我会将其下载到Azure自动化临时文件夹,以便我可以指定运行它的路径。
在自动化中尝试以下 PS:
$appid = "<your Azure application ID>"
$passwd = "<your Azure application password>"
$tenant = "<tenant>"
$storageName = "<storage name>"
$containerName = "<container name>"
$scrtptName = "<script name>"
$storageResourceGroupName = "<storage group name>"
$vmName = "<vm name>"
$VMResourceGroupName = "<vm group name>"
$secpasswd = ConvertTo-SecureString -String $passwd -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ($appid , $secpasswd)
login-AzAccount -Credential $cred -Tenant $tenant -ServicePrincipal
$storage = Get-AzStorageAccount -ResourceGroupName $storageResourceGroupName -Name $storageName
Get-AzStorageBlobContent -Container $containerName -Blob $scrtptName -Context $storage.Context -Destination "c:/temp"
$scriptPath = "c:/temp/$scrtptName"
$result = Invoke-AzVMRunCommand -VMname $vmName -ResourceGroupName $VMResourceGroupName -CommandId 'RunPowerShellScript' -ScriptPath $scriptPath
echo $result.Value
Remove-Item $scriptPath -Force
我将我的脚本放在我的存储帐户中,在本例中,它用于下载某些内容:
我的测试脚本内容:
$url = "https://download.microsoft.com/download/1/E/7/1E7B1181-3974-4B29-9A47-CC857B271AA2/English/X64/msodbcsql.msi"
$outpath = "c:/odbc.msi"
Invoke-WebRequest -Uri $url -OutFile $outpath
Azure 自动化测试及其结果: 可以看到文件已经下载成功。
顺便说一句,这里不需要使用远程powershell,您可以使用Azure VM的运行命令功能直接在Azure VM上运行脚本。
关于linux - 从 Azure Linux VM 上的脚本启动防病毒扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59064152/