使用python检测主机存活端口及检查存活主机
作者:mrr 时间:2021-08-01 05:20:27
监测主机存活的端口
#!/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 检查存活主机
这里的设备对存活状态不是很敏感,就使用了脚本来检测存活状态,之前用shell做了一个,实现的功能是使用Ping命令检测服务器存活,不在线会将不在线的服务器及IP通过邮件发送提醒,现在在学习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("发送邮箱地址",maillist,msg.as_string())
运行的结果:
标签:python,主机存活,端口
0
投稿
猜你喜欢
Python列表解析配合if else的方法
2021-10-08 06:50:11
Mysql数据库百万级数据测试索引效果
2024-01-24 01:30:03
python代码实现扫码关注公众号登录的实战
2021-11-18 04:40:43
使用pandas实现筛选出指定列值所对应的行
2023-05-06 18:29:53
Go语言操作数据库及其常规操作的示例代码
2024-01-14 07:05:46
Selenium 滚动页面至元素可见的方法
2022-06-01 14:01:05
python中必会的四大高级数据类型(字符,元组,列表,字典)
2023-01-10 00:54:20
js判断设备是否为PC并调整图片大小
2024-05-02 16:12:22
python使用rsa加密算法模块模拟新浪微博登录
2022-06-04 13:36:19
解决springboot yml配置 logging.level 报错问题
2021-09-21 21:38:02
JS实现页面打印(整体、局部)
2024-04-26 17:14:27
基于SQL中SET与SELECT赋值的区别详解
2024-01-20 02:13:17
老生常谈Session和Cookie之间区别与联系(必看篇)
2023-03-09 20:53:24
python之 matplotlib和pandas绘图教程
2021-07-16 01:50:11
Python利用prettytable实现格式化输出内容
2023-10-17 11:02:32
python批量导入数据进Elasticsearch的实例
2022-06-29 16:08:02
go redis实现滑动窗口限流的方式(redis版)
2024-02-02 18:04:27
Python中int()函数的用法浅析
2022-08-18 09:45:12
PDO::beginTransaction讲解
2023-06-06 00:57:46
能让Python提速超40倍的神器Cython详解
2023-11-12 07:11:11