Python/Python for Linux

[Python] linux ping check(ICMP) program

Pydole 2019. 4. 9. 13:40
#!/usr/bin/python  

# Python Version : 2.7.5 

from os import system  
from datetime import datetime  

checkList = ('127.0.0.1','192.168.x.99','192.168.x.98')  
logTime = str(datetime.today())[:19]

for host in checkList:  
        cmdstring = 'ping -c1 '+host+' 1>/dev/null'    # Linux Ping Check Command 
        var = system(cmdstring)                               
        if var == 0:                                   # value = 0, OK 
                print host, 'ping ok : ', logTime  
        else:  
                print host, 'ping fail : ', logTime
------------------------------------------------------------
127.0.0.1 ping ok :  2019-04-09 13:39:34 
192.168.1.99 ping ok :  2019-04-09 13:39:34 
192.168.1.98 ping fail :  2019-04-09 13:39:34