标签:timeout TE optparse result you 2.7 except close return
代码如下:
import optparse
import socket
def connScan(tgtHost,tgtPort):
try:
s = socket.socket()
s.connect((tgtHost,int(tgtPort)))
resv = s.recv(1024)
print(‘[+] %s/tcp open‘%tgtPort)
print(‘[+]‘ +str(resv.decode(‘utf-8‘)))
s.close()
except:
#print(tgtHost + ‘ ‘,tgtPort)
print(‘[-] %s/tcp closed‘%tgtPort)
def postScan(tgtHost,tgtPorts):
try:
tgtIP = socket.gethostbyname(tgtHost)
except:
print("[-] Cannot resolve ‘%s‘:Unknown host"%tgtHost)
return
try:
tgtName = socket.gethostbyaddr(tgtIP)
print(‘\n[+] Scan Results for:‘+tgtName[0])
except:
print(‘\n[+] Scan Results for:‘+tgtIP)
socket.setdefaulttimeout(1)
posts_list = tgtPorts.split(‘,‘)
print(posts_list)
for tgtPort in posts_list:
#print(tgtPort)
print(‘scanning port‘ + tgtPort)
connScan(tgtHost,int(tgtPort))
def main():
parser = optparse.OptionParser()
parser.add_option(‘-H‘,dest="tgtHost",type="string",help="specify target port[s] separated by comma0")
parser.add_option("-P",dest=‘tgtPort‘,type=(‘string‘),help=‘specify target post[s] separated by comma‘)
(options,args)=parser.parse_args()
tgHost = options.tgtHost
tgPorts = options.tgtPort
if (tgHost==None) | (tgPorts==None):
print(‘[-] You must specify a target hos t and port[s].‘)
exit(0)
postScan(tgHost,tgPorts)
if __name__ == ‘__main__‘:
main()
##################################
发这个的原因 买的一本书里面的代码全是2.7的
保存纪念以及方便后续阅读
2018.6.23
##################################
标签:timeout TE optparse result you 2.7 except close return
原文地址:https://www.cnblogs.com/xiaohonmao/p/9217309.html