sshd_status.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:commons 作者: Tendrl 项目源码 文件源码
def find_status():
    """This util is used to find the status of

    sshd service. It will identify sshd status using
    process id of sshd service.

    input:
        (No input required)

    output:
        {"name": "",
         "port": "",
         "status": ""}
    """

    sshd = {"name": "",
            "port": "",
            "status": ""}
    cmd = cmd_utils.Command("systemctl show sshd.service")
    out, err, rc = cmd.run()
    if not err:
        pid = _find_pid(out)
        if pid != 0:
            p = psutil.Process(pid)
            result = [con for con in p.connections() if con.status ==
                      psutil.CONN_LISTEN and con.laddr[0] == "0.0.0.0"]
            if result:
                sshd["name"] = p.name()
                sshd["port"] = int(result[0].laddr[1])
                sshd["status"] = result[0].status
            else:
                err = "Unable to find ssh port number"
                Event(
                    Message(
                        priority="debug",
                        publisher="commons",
                        payload={"message": err}
                    )
                )
        else:
            err = "sshd service is not running"
            Event(
                Message(
                    priority="debug",
                    publisher="commons",
                    payload={"message": err}
                )
            )

    else:
        Event(
            Message(
                priority="debug",
                publisher="commons",
                payload={"message": err}
            )
        )
    return sshd, err
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号