Created
April 10, 2018 12:48
-
-
Save JuniorPolegato/c87180d04ee1a84ff4b41d76d605a547 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import socket | |
socket.setdefaulttimeout(.1) # Espera 100 milissegundos pela resposta | |
TRUE = 1 # pode-se retornar True ou 0 (sucesso) se preferir | |
FALSE = 0 # pode-se retornar False ou 1 (sem sucesso) se preferir | |
def rodando(service_run_file, port=None, udp=False, | |
host='127.0.0.1', msg='Hello'): | |
pid_file = '/var/run/%s.pid' % service_run_file | |
if os.path.exists(pid_file): | |
pid = open(pid_file).read().strip() | |
if not os.path.exists('/proc/' + pid): | |
print ' *** Erro: process not found *** ', | |
return FALSE # retorna FALSE se o processo não existir | |
else: | |
print ' *** Erro: pid file not found *** ', | |
return FALSE # retorna FALSE se o arquivo com o pid não existir | |
if port: | |
try: | |
if udp: | |
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
udp.sendto(msg, (host, port)) | |
msg, client = udp.recvfrom(1024) | |
udp.close() | |
else: | |
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
tcp.connect((host, port)) | |
tcp.send(msg) | |
msg = tcp.recv(1024) | |
tcp.close() | |
except Exception as e: | |
print ' *** Erro: %s *** ' % e, | |
return FALSE # retorna FALSE se algo der errado | |
return TRUE # retorna TRUE se passar pelos testes acima | |
if __name__ == '__main__': | |
# Use "netstat -tulpn | grep xxxxx" para saber protocolo e porta de xxxxxx | |
print '-' * 40 | |
print 'Cron:', rodando('crond') | |
print '-' * 40 | |
print 'Nada:', rodando('nada') | |
print '-' * 40 | |
print 'NTP:', rodando('ntpd', port=123, udp=True, | |
msg='\x1b' + 47 * '\0') | |
print '-' * 40 | |
print 'NTP na 1234:', rodando('ntpd', port=1234, udp=True, | |
msg='\x1b' + 47 * '\0') | |
print '-' * 40 | |
print 'Apache 2:', rodando('apache2/apache2', port=80, | |
msg='GET / HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n') | |
print '-' * 40 | |
print 'Apache X:', rodando('apache2/apache2', port=80, | |
msg='Espera de um milagre') | |
print '-' * 40 | |
print 'Porta 1:', rodando('apache2/apache2', port=1, | |
msg='GET / HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n') | |
print '-' * 40 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment