c# - 从 Azure Function 中 Ping 服务器

标签 c# azure-functions ping access-denied

我有以下 Azure 函数失败并显示访问被拒绝 (忽略逻辑很奇怪的事实,我只是在做第一个测试)

public static void Run(TimerInfo myTimer, ILogger log)
{
    List<string> servers = new List<string>()
    {
        "server1"
    };

    foreach(string server in servers)
    {
        if (!Ping(server))
        {
            SendEmail($"Server {server} seems down.", log);
        }
    }
}

static bool Ping(string hostName)
{
    Ping pingSender = new Ping();
    int timeout = 120;
    PingReply reply = pingSender.Send(hostName, timeout);
    return reply.Status == IPStatus.Success;
}

static void SendEmail(string message, ILogger log)
{
    log.LogInformation(message);
}

如果我换行

PingReply reply = pingSender.Send(hostName, timeout);
return reply.Status == IPStatus.Success;

为了return true;为了测试,函数运行良好。

我需要配置什么才能让函数执行 ping 操作?

最佳答案

据我所知,我们无法在 Azure 函数中成功执行 ping 操作,因为 Azure 中不允许使用 ICMP 协议(protocol)。但是我们可以在里面做tcpping。您可以在 Azure 函数控制台中进行测试(如下截图所示):

enter image description here 我们也可以安装一些工具来进行ping操作,比如PsPing、Nmap、Telnet等。

这里是更新:

根据一些研究,我认为 Azure Function 可以满足您的要求。

首先,我们应该安装psping。您可以在此页面下载:https://learn.microsoft.com/zh-cn/sysinternals/downloads/psping#installation

然后解压 psping 文件并在您的 Azure 函数中打开 Kudu。

enter image description here

然后单击“调试控制台”-->“CMD”-->“站点”,新建一个名为“tools”的文件夹,单击“tools”并将你的psping文件(PSTools)拖到“tools”文件夹。

之后,请引用我下面贴出的代码

enter image description here

如果 ping 成功,我代码中的变量“err”将不显示任何内容。如果 ping 失败,它将显示错误。所以你可以根据它来判断成功。

enter image description here

关于c# - 从 Azure Function 中 Ping 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57600698/

相关文章:

c# - .Net中如何播放 "New Mail"系统音?

twilio - Azure函数未命中断点

batch-file - 如果 ping 响应时间过长,则运行备用功能

ping - 在 MacOS 上请求 icmp_seq 超时

ubuntu - docker -Ubuntu-bash : ping: command not found

c# - 如何更改选项卡控件的名称

c# - 为什么 Assert.IsInstanceOfType(0.GetType(), typeof(int)) 会失败?

C# - 清理事件处理程序

Azure AD B2C API 连接器不修改自定义用户属性

c# - Azure 表 : Unable to load one or more of the requested types. 检索 LoaderExceptions 属性以获取更多信息