从Azure函数ping服务器 [英] Ping server from Azure Function

查看:86
本文介绍了从Azure函数ping服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Azure函数,由于访问被拒绝而失败(忽略逻辑很奇怪的事实,我只是在进行第一次测试)

  public static void Run(TimerInfo myTimer,ILogger日志){List< string>服务器=新的List< string>(){服务器1"};foreach(服务器中的字符串服务器){如果(!Ping(服务器)){SendEmail($"Server {server}似乎已关闭.",日志);}}}静态布尔Ping(字符串hostName){Ping pingSender = new Ping();int超时= 120;PingReply回复= pingSender.Send(hostName,超时);返回Reply.Status == IPStatus.Success;}静态无效的SendEmail(字符串消息,ILogger日志){log.LogInformation(message);} 

如果我更改行

  PingReply回复= pingSender.Send(hostName,超时);返回Reply.Status == IPStatus.Success; 

返回true; 为进行测试,该函数运行良好.

我需要配置什么才能使该功能执行ping操作?

解决方案

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

我们还可以安装一些工具来执行ping操作,例如PsPing,Nmap或Telnet.

以下是更新内容:

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

首先,我们应该安装psping.您可以在以下页面上下载它:

然后单击调试控制台"->"CMD"->站点",新建一个名为工具"的文件夹,单击工具"并将psping文件(PSTools)拖动到工具"文件夹.

之后,请参考我在下面发布的代码

如果ping成功,则我的代码中的变量"err"将不显示任何内容.如果ping失败,它将显示错误.因此,您可以根据它来判断成功.

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?

解决方案

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.

这篇关于从Azure函数ping服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆