Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ping server from Azure Function

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?

like image 661
PedroC88 Avatar asked May 13 '26 18:05

PedroC88


1 Answers

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):

enter image description here 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://learn.microsoft.com/zh-cn/sysinternals/downloads/psping#installation

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

enter image description here

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

enter image description here

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.

enter image description here

like image 163
Hury Shen Avatar answered May 15 '26 08:05

Hury Shen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!