#!/usr/bin/env pythonimport socket
import sys
red='\033[31m'
green='\033[32m'
end='\033[0m'
Ports =[22,8080,8443,8088,3306,6379,61613,61616,22122,23000]
Services =['SSH','HTTP','HTTPS','Office','MySQL','Redis','ActiveMQ','ActiveMQ','Tracker','Storage']defget_host_by_name(domain):try:return socket.gethostbyname(domain)except socket.error, e:print red +'error: '+ end +'%s - %s'%(domain, e)return0deftcp_port_check(ip, port, timeout=1.0):
s = socket.socket()
s.settimeout(timeout)
s.connect((ip, port))
s.close()defifs_tcp_port_check(domain):
ip = get_host_by_name(domain)if ip:print('*** Host: '+ ip +' ***')for port,service inzip(Ports,Services):try:
tcp_port_check(ip, port)print(green +'[pass] '+ end + service +'('+str(port)+')'+' is up')except:print(red +'[failed] '+ end + service +'('+str(port)+')'+' is down')defsg_tcp_port_check(domain,port):
ip = get_host_by_name(domain)if ip:print('*** Host: '+ ip +' ***')try:
tcp_port_check(ip,int(port))print(green +'[pass] '+ end +'Port '+ port +' is open')except:print(red +'[failed] '+ end +'Port '+ port +' is down')defuseage():print(' -l Check all local services')print(' -r Check all remote services')print(' -s Check if the port is open')defmain():iflen(sys.argv)==2and sys.argv[1]=='-l':
ifs_tcp_port_check('127.0.0.1')eliflen(sys.argv)==3and sys.argv[1]=='-r':
ifs_tcp_port_check(sys.argv[2])eliflen(sys.argv)==4and sys.argv[1]=='-s':
sg_tcp_port_check(sys.argv[2], sys.argv[3])else:
useage()if __name__=='__main__':
main()