我正在尝试在 Azure 中的 Linux VM 中启动 ping。我想使用 API 来做到这一点。我有一个空白回复,状态为:202 已接受。 `https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}/runCommand?api-version=2023-07-01
JSON 正文:
{
"commandId": "RunShellScript",
"script": [
"ping -c 5 8.8.8.8"]
}
我可以使用 oauth2 通过 HTTP Rest 启动和停止我的虚拟机。
为了确保这不是 API 错误,我使用“cloud-shell”来运行我的 ping,如下所示:
az vm run-command invoke -g MYRG -n VMname --command-id RunShellScript --scripts "ping -c 5 8.8.8.8"
我收到“正在运行”消息 90 分钟。在我超时之后。
我打开了出站端口规则:端口 443 协议 TCP 源任意目标任意。
我的错误在哪里?它不适用于 API 或 CLI。
谢谢您的帮助。
需要检查以下内容:
首先,验证是否有任何防火墙阻止来自用于连接虚拟机的端口的传入流量。这可能会导致运行命令无法按预期工作。
在您的场景中,ping 命令也可能会失败,因为它使用 Internet 控制消息协议 (ICMP),而 Windows 防火墙默认情况下无法启用该协议。
New-NetFirewallRule
命令允许它。
使用以下代码检查虚拟机路由的连接性。请参阅随附的 MS 文档以获取相关信息。
$subscription = "xxx"
$resourceGroup = "Jahnavi"
$vmname = "newvm"
$sourceId = "/subscriptions/xxxx/resourcegroups/Jahnavi/providers/Microsoft.Compute/virtualMachines/newvm"
$destinationId = "5 8.8.8.8"
$desPort = "80"
$requestBody = @"
{
'source': {
'resourceId': '${sourceId}',
'port': xx
},
'destination': {
'address': '${destinationId}',
'port': ${desPort}
}
}
"@
$response = armclient post "https://management.azure.com/subscriptions/{subscription}/resourceGroups/{resourceGroup}/providers/Microsoft.Compute/virtualMachines/{vmName}/runCommand?api-version=2023-07-01" $requestBody
检查完上述内容后,我尝试使用以下 CLI 命令在我的环境中执行您的要求,并能够成功 ping 通,如图所示。
az vm run-command invoke -g Jahnavi -n newvm --command-id RunShellScript --scripts "ping -c 5 8.8.8.8"
您还可以检查虚拟机资源下的活动日志以简要了解错误。
感谢您的评论。
在虚拟机上的 SSH 中,我可以 ping 8.8.8.8 并执行“ip a”。
........@Linux1:~$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=112 time=8.10 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=112 time=8.37 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=112 time=8.19 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=112 time=8.10 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=112 time=8.76 ms
我尝试了这个命令:“az vm run-command invoke -g MYRG -n VMname --command-id RunShellScript --scripts“ip a”在cloud-shell和run-command中但只是“202接受”。没有答案。
一旦有了“活动日志”,我就会添加它们。