Ping server from Azure Function

大城市里の小女人 提交于 2021-02-08 09:14:42

问题


I have the following Azure Function that fails with Access is Denied (ignore the fact that the logic is all weird, I'm just doing a first test)

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);
}

If I change the lines

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

to return true; for the sake of testing, the function runs well.

What do I need to configure so the function can do the ping?


回答1:


As far as I know, we can't do ping operation in Azure function successful because ICMP protocol is not permitted in Azure. But we can do tcpping in it. You can test it in Azure function console(shown as screenshot below):

We can also install some tools to do ping operation, such as PsPing, Nmap, or Telnet.

Here is the updating:

According to some research, I think Azure Function can meet your requirements.

First, we should install psping. You can download it on this page:https://docs.microsoft.com/zh-cn/sysinternals/downloads/psping#installation

Then unzip the psping file and open Kudu in your Azure function.

Then click "Debug console" --> "CMD" --> "site", new a folder named "tools", click "tools" and drag your psping file(PSTools) to "tools" folder.

After that, please refer to the code I post below

If ping success, the variable "err" in my code will show nothing. If ping fail, it will show the error. So you can judge success based on it.



来源:https://stackoverflow.com/questions/57600698/ping-server-from-azure-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!