Use Python to detect host survival ports and check for surviving hosts

Source: Internet
Author: User
monitoring the host's surviving ports

#!/usr/bin/env python# coding-utfimport argparseimport socketimport sys#author:wolf_ribbledef scan_ports (host,start_ Port,end_port): "" Scan Remote Hosts "" "Try:sock = Socket.socket (socket.af_inet, socket. SOCK_STREAM) except Socket.error,error_msg:print ' socket creation failed. Error code: ' + str (error_msg[]) + ' Erroe message: ' + error_msg[] sys.exit () try:remote_ip = Socket.gethostbyname (h ost) except Socket.error,error_msg:print error_msg sys.exit () End_port + = for port in range (Start_port,end_port) : Try:sock.connect ((remote_ip,port)) print ' Port ' + str (port) + ' is Ipen ' sock.close () sock = sock Et.socket (socket.af_inet, socket. SOCK_STREAM) except socket.error,error_msg: #print error_msg passif __name__ = = ' __main__ ': parser = Argpars E.argumentparser (description= ' remote port Scaner ') parser.add_argument ('--host ', action= "store", dest= "host", default= ' www.wealink.com ') parser.add_argument ('--start-port ', action= "store", dest= "Start_port", default= ", Type=int) parser.add_argument ('--end-port ', action=" store ", dest=" End_port ", default=", Type=int) Given_args = Parser.parse_args () host, start_port, End_start = Given_args.host, given_args.start_port,given_a Rgs.end_port Scan_ports (Host,start_port,end_start)

Using Python to check for surviving hosts

The equipment here is not very sensitive to the survival status, the use of the script to detect the survival state, before using the shell to do one, the implementation of the function is to use the ping command to detect the server to survive, not the line will not be online server and IP sent by email reminders, and now learning Python, Changed to a python version. The host I want to detect is stored in/scrip/host in the format:

HOST IP

Well, nonsense less, on the script:

#coding =utf-8import subprocessimport os,time,sys,reimport smtplibfrom email.mime.text Import Mimetextfrom Email.mime.multipart import Mimemultipartserver=smtplib. SMTP (' smtp.126.com ') start_time=time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime (Time.time ())) Mj=open ('/scrip/ Host ', ' R ') mi=open ('/scrip/text ', ' W ') Mihost=mj.readlines () mj.close () Online=[]i=0for i in range (len (mihost)): host= Mihost[i][0:14].strip () Ip=mihost[i][14:].strip () p = subprocess. Popen (["Ping-c 1-i 0.2-w 3" + IP], stdin = subprocess. PIPE, stdout = subprocess. PIPE, stderr = subprocess. PIPE, Shell = True) out = P.stdout.read () regex = Re.compile ("time=\d*", re. IGNORECASE | Re. MULTILINE) If Len (Regex.findall (out)) > 0:online.append (Host) else:fail_host =host + ' \ t ' + IP mi.write (f Ail_host) mi.write (' \ n ') mi.close () fail=open ('/scrip/text ', ' R ') number=fail.readlines () fail_host= '. Join (number) context = ' Scan time: ' + ' \ t ' * + ' * + start_time + ' \ n' + ' not Online Devices: ' + ' \ t ' + repr (len (number)) + ' \ n ' + ' \ n ' + ' HOST ' + ' ' *12 + ' I P ' + ' \ n ' + fail_hostmsg = Mimemultipa RT (' alternative ') msg[' Subject ') = ' not Online Device ' part = mimetext (context, ' plain ', ' utf-8 ') Msg.attach (part) server= Smtplib. SMTP (' smtp.126.com ') server.login ("Mailbox User name", "Mailbox Password") maillist=[' receive Mailbox ',]server.sendmail ("Send email address", Maillist,msg.as_ String ())

Results of the operation:

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.