[Python] 纯文本查看 复制代码
#!/usr/bin/env python
# -- coding: utf-8 --
import sys,os,time,datetime
from scapy.all import *
ports = 2202
iplist = ['14.215.177.39',
'14.215.177.48',
'14.215.177.53',
'14.215.177.46',
'14.215.177.54',
'14.215.177.55']
online_num = 0
offline_num = 0
# msgbox
def msgbox(ip,ports,state):
if state == "Offline":
print("\n%s:%s -- \033[1;31;40m %s \033[0m\n" % (ip, ports, state))
else:
print("\n%s:%s -- \033[1;31;32m %s \033[0m\n" % (ip, ports, state))
def clog(ip,state):
if state == "Offline":
os.system('echo '+ ip + ' -- Offline >> offlinehost.log')
elif state == "Online":
os.system('echo '+ ip + ' -- Online >> onlinehost.log')
else:
os.system('echo > offlinehost.log')
os.system('echo > onlinehost.log')
# scan
def online_check(iplist,ports,online_num,offline_num):
for ipaddr in iplist: #iplist
#for ipaddr in host: #host.txt
pkt = sr1(IP(dst=ipaddr)/TCP(dport=ports, flags="S"), timeout=2)
if pkt == None:
msgbox(ipaddr,ports,"Offline")
clog(ipaddr,"Offline")
offline_num += 1
#elif pkt.haslayer(TCP):
elif pkt.getlayer(TCP).flags == 0x12:
send_rst = sr(IP(dst=ipaddr)/TCP(dport=ports, flags="AR"), timeout=2)
msgbox(ipaddr,ports,"Online")
clog(ipaddr,"Online")
online_num += 1
elif pkt.getlayer(TCP).flags == 0x14:
msgbox(ipaddr,ports,"Offline")
clog(ipaddr,"Offline")
offline_num += 1
return (online_num,offline_num)
# online_check
if __name__ == "__main__":
mtime = time.strftime('%Y-%m-%d %H:%M:%S')
clog(0,"")
print("---------------------------------------------Ready start scanning---------------------------------------------")
print(mtime)
try:
f =open("host.txt",'r')
f.close()
except IOError:
f = open("host.txt",'w')
f = open('host.txt')
host = f.read().splitlines()
f.close()
run = online_check(iplist,ports,online_num,offline_num)
run
online_num = run[0]
offline_num = run[1]
count_host = online_num + offline_num
print("---------------------------------------------------Scan done---------------------------------------------------")
print(mtime)
print("\nCount Scan host: \033[1;31;33m %s \033[0m, Online: \033[1;31;32m %s \033[0m , Offline: \033[1;31;40m %s \033[0m , More info please see offlinehost.log/onlinehost.log.\n\n" % (count_host, online_num, offline_num))
os.system("pause")