为什么“运行命令”在 Azure 中不返回任何响应?

问题描述 投票:0回答:2

我正在尝试在 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。

谢谢您的帮助。

azure command-line-interface azure-cli
2个回答
0
投票

需要检查以下内容:

  • 首先,验证是否有任何防火墙阻止来自用于连接虚拟机的端口的传入流量。这可能会导致运行命令无法按预期工作。

  • 在您的场景中,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
  • 确保虚拟机具有传出 Internet 连接以到达指定的 IP 地址。这可能包括检查 NSG 规则并确保虚拟机已正确配置为出站 Internet 访问。

检查完上述内容后,我尝试使用以下 CLI 命令在我的环境中执行您的要求,并能够成功 ping 通,如图所示。

az vm run-command invoke -g Jahnavi -n newvm --command-id RunShellScript --scripts "ping -c 5 8.8.8.8"

enter image description here

您还可以检查虚拟机资源下的活动日志以简要了解错误。

enter image description here


0
投票

感谢您的评论。

在虚拟机上的 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接受”。没有答案。

一旦有了“活动日志”,我就会添加它们。

© www.soinside.com 2019 - 2024. All rights reserved.