失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > [批量主机存活扫描工具scanhost]扫描主机存活[python版本 非nmap版本]

[批量主机存活扫描工具scanhost]扫描主机存活[python版本 非nmap版本]

时间:2020-04-15 09:39:11

相关推荐

[批量主机存活扫描工具scanhost]扫描主机存活[python版本 非nmap版本]

地址:/hujkay

作者:Jekkay Hu(34538980@)

关键词:主机扫描,主机存活,纯python版本,非nmap

时间: /1/21

最近受好友所托写个python脚本,就是扫描局域网的主机存活的情况,这个在内网渗透时非常有用,因为你在跳板机上贸然安装namp等扫描工具,则很容易被发现,我抽时间写了个python脚本,使用ping工具来监测主机存活情况。我写的这个脚本时采用单线程的方式工作,所效率不是很快,下个版本我在优化成多线程的版本,同时也让支持扫描端口等功能。

#!/usr/bin/env python#-*- coding: utf8 -*-## Author: Jekkay Hu# Date: /1/21# Email: jekkay@# QQ: 34538980#import osimport sys# Convert IP Format: Number['3232247553] <----> String ['192.168.47.1']IPNumToString = lambda x: '.'.join([str(x/(256**i)%256) for i in range(3,-1,-1)])IPStringToNum = lambda x:sum([256**j*int(i) for j,i in enumerate(x.split('.')[::-1])])# start IP -- End IPStartIP = 0EndIP = 0def welcome():welcomeinfo = """======================================================= ==scanhost V1.0 == Jekkay Hu, Written in /1/21 == 34538980@, Jekkay@ == More please visit: /hujkay== ======================================================="""print welcomeinfodef help():helpinfo = """======================================================= ==scanhost V1.0 == Jekkay Hu, Written in /1/21 == 34538980@, Jekkay@ == More please visit: /hujkay== == Usage: == python scanhost.py 1.2.3.4 == python scanhost.py 1.2.3.4-255 == python scanhost.py 1.2.3.4 - 1.2.4.5 ======================================================="""print helpinfodef parseargs():try:commandargs = sys.argv[1:]if not commandargs:return Falsecommandargs = ''.join(commandargs)commandargs = commandargs.split('-')global StartIPglobal EndIPcommandlen = len(commandargs)if commandlen == 1:StartIP = EndIP = int(IPStringToNum(commandargs[0]))elif commandlen == 2:StartIP = commandargs[0]EndIP = commandargs[1]if len(StartIP.split('.')) !=4 :return Falseendiplen = len(EndIP.split('.'))if endiplen == 1:prefixip = StartIP.split('.')[0:3]prefixip.append(EndIP)EndIP = '.'.join(prefixip)elif endiplen == 4:passelse:return False#print "startip",StartIP,",endip:",EndIPStartIP = int(IPStringToNum(StartIP))EndIP = int(IPStringToNum(EndIP))except Exception,e:# any exception occursprint ereturn Falsereturn Truedef checkhoston(ip):try:cmd = ['ping','%s' % IPNumToString(ip),'-c','1']output = os.popen(' '.join(cmd)).readlines()for line in list(output):if not line:continueif str(line).find('ttl') >= 0 or str(line).find('TTL') >= 0:return Trueexcept:passdef processcheckhost(): global StartIPglobal EndIPalivecount = 0StartIP = int(StartIP)EndIP = int(EndIP)totalip = EndIP - StartIP + 1if totalip <= 0:help()exit(0)print 'Startint scan ',IPNumToString(StartIP),' -> ',IPNumToString(EndIP), ',please wait...'fd = open('scanhost.txt',"w")#for i in xrange(StartIP,EndIP+1,1):ip = StartIPwhile True:if ip > EndIP:break if checkhoston(ip):fd.write(IPNumToString(ip))alivecount = alivecount + 1#print IPNumToString(ip)ip = ip + 1sys.stdout.write('#')if (ip-StartIP) % 20 == 0:sys.stdout.write('\r\n')fd.close()return alivecountdef showresult(shownum):fd = open('scanhost.txt',"r")for line in fd.readlines(shownum):print linefd.close()def main():if not parseargs():help()exit(0)welcome()alivecount = processcheckhost()print "\r\n [%d] host is on,please see the scanhost.txt, top 300 will be shown below" % alivecountshowresult(300)if alivecount > 300:print "More ips please see scanhost.txt"if __name__ == '__main__':main()

胡杨, Jekkay Hu

/1/21

如果觉得《[批量主机存活扫描工具scanhost]扫描主机存活[python版本 非nmap版本]》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。