简体   繁体   English

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

[英]How to check if NTP server is running on a Linux machine by a python script

I need to get IP address of a machine and check if NTP server is running on that. 我需要获取计算机的IP地址,并检查NTP服务器是否正在运行。 In addition, if it is not running, just start it. 另外,如果它没有运行,请启动它。 I checked several posts and non of them worked for me. 我检查了几个帖子,但没有一个适合我。

You can use fabric for this. 您可以为此使用面料 It's great! 这很棒! Here's a quick and dirty way: 这是一种快速而肮脏的方法:

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

The execute this like: 执行如下:

fab -H [host name] restart_ntp_if_not_running

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM