如何通过python脚本检查NTP服务器是否在Linux机器上运行

时间:2015-10-27 05:52:47

标签: python process ntp

我需要获取计算机的IP地址并检查NTP服务器是否正在运行。此外,如果它没有运行,只需启动它。 我检查了几个帖子,其中没有一个为我工作。

1 个答案:

答案 0 :(得分:1)

您可以使用fabric。这很棒!这是一种快速而又肮脏的方式:

from fabric.api import run

def restart_ntp_if_not_running():
    run('if [[ $(netstat -p tcp -n | grep [your ip].123 | grep ESTABLISHED) ]]; then true; else [your command to restart here]; fi;')

执行此操作:

fab -H [host name] restart_ntp_if_not_running