亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用python檢測主機存活端口及檢查存活主機

 更新時間:2015年10月12日 10:50:49   投稿:mrr  
這篇文章主要介紹了使用python檢測主機存活端口及檢查存活主機的相關(guān)資料,需要的朋友可以參考下

監(jiān)測主機存活的端口

#!/usr/bin/env python
# coding-utf
import argparse
import socket
import sys
#author: wolf_ribble
def 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(host)
  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 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    except socket.error,error_msg:
      #print error_msg
      pass
if __name__ == '__main__':
  parser = argparse.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_args.end_port
  scan_ports(host,start_port,end_start)

使用python 檢查存活主機

這里的設(shè)備對存活狀態(tài)不是很敏感,就使用了腳本來檢測存活狀態(tài),之前用shell做了一個,實現(xiàn)的功能是使用Ping命令檢測服務(wù)器存活,不在線會將不在線的服務(wù)器及IP通過郵件發(fā)送提醒,現(xiàn)在在學(xué)習python,改了一個python版本。需要檢測的主機我存儲在/scrip/host中,格式為:

HOST  IP

好了,廢話少說,上腳本:

#coding=utf-8
import subprocess
import os,time,sys,re
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
server=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=0
for 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(fail_host)
    mi.write('\n')
mi.close()
fail=open('/scrip/text','r')
number=fail.readlines()
fail_host=''.join(number)
context = 'Scan Time:' + '\t '*2 +' ' *3 + start_Time +'\n'+ 'Not Online Devices:' +'\t'+ repr(len(number)) +'\n'+ '\n' +'HOST' + ' '*12 + 'I P' + '\n' + fail_host
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Not Online Device'
part = MIMEText(context, 'plain', 'utf-8')
msg.attach(part)
server=smtplib.SMTP('smtp.126.com')
server.login("郵箱用戶名","郵箱密碼")
maillist=['接收郵箱',]
server.sendmail("發(fā)送郵箱地址",maillist,msg.as_string())

運行的結(jié)果:


相關(guān)文章

  • Python操作串口的方法

    Python操作串口的方法

    這篇文章主要介紹了Python操作串口的方法,以一個簡單實例分析了Python操作串口echo輸出的方法,需要的朋友可以參考下
    2015-06-06
  • Python 捕獲代碼中所有異常的方法

    Python 捕獲代碼中所有異常的方法

    這篇文章主要介紹了Python 捕獲代碼中所有異常的方法,文中講解非常細致,代碼幫助大家更好的理解和學(xué)習,感興趣的朋友可以了解下
    2020-08-08
  • 使用python處理題庫表格并轉(zhuǎn)化為word形式的實現(xiàn)

    使用python處理題庫表格并轉(zhuǎn)化為word形式的實現(xiàn)

    這篇文章主要介紹了使用python處理題庫表格并轉(zhuǎn)化為word形式的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2020-04-04
  • 利用Python將彩色圖像轉(zhuǎn)為灰度圖像的兩種方法

    利用Python將彩色圖像轉(zhuǎn)為灰度圖像的兩種方法

    這篇文章主要給大家介紹了關(guān)于利用Python將彩色圖像轉(zhuǎn)為灰度圖像的兩種方法,以及python 批量將圖片轉(zhuǎn)為灰度圖的方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-12-12
  • Python3.4 tkinter,PIL圖片轉(zhuǎn)換

    Python3.4 tkinter,PIL圖片轉(zhuǎn)換

    我們給大家整理了關(guān)于Python3.4 tkinter,PIL圖片轉(zhuǎn)換的相關(guān)完整代碼,大家可以學(xué)習測試下。
    2018-06-06
  • numpy.reshape(-1,1)的具體使用

    numpy.reshape(-1,1)的具體使用

    本文主要介紹了numpy.reshape(-1,1)的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2022-07-07
  • Python實現(xiàn)統(tǒng)計單詞出現(xiàn)的個數(shù)

    Python實現(xiàn)統(tǒng)計單詞出現(xiàn)的個數(shù)

    這篇文章主要介紹了Python實現(xiàn)統(tǒng)計單詞出現(xiàn)的個數(shù),本文給出了實現(xiàn)代碼以及使用方法,需要的朋友可以參考下
    2015-05-05
  • 深入分析在Python模塊頂層運行的代碼引起的一個Bug

    深入分析在Python模塊頂層運行的代碼引起的一個Bug

    幾個星期前, 我的同事跑過來, 說發(fā)現(xiàn)一個奇怪的Bug: 在使用Python的subprocess運行子進程時, 當子進程運行失敗時居然沒有拋出錯誤!
    2014-07-07
  • python如何設(shè)置靜態(tài)變量

    python如何設(shè)置靜態(tài)變量

    在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于python如何設(shè)置靜態(tài)變量的相關(guān)文章,有興趣的朋友們可以參考下。
    2020-09-09
  • window下eclipse安裝python插件教程

    window下eclipse安裝python插件教程

    這篇文章主要為大家詳細介紹了window下eclipse安裝python插件教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04

最新評論