python网络扫描编程_python端口扫描程序详解

这是一个简单的网络程序,可以用来扫描指定ip的常用端口

from socket import *

tgtHost=raw_input("What is the web address?: ")

tgtPorts=raw_input("What are the ports seperated by commas? or would you like to test all?: ")

def connScan(tgtHost, tgtPort):

try:

connSkt=socket(AF_INET, SOCK_STREAM)

connSkt.settimeout(10)

connSkt.connect((tgtHost, tgtPort))

connSkt.settimeout(None)

print("%d/tcp open"%tgtPort)

except:

print("%d/tcp closed"%tgtPort)

def portScan(tgtHost, tgtports):

global tgtPorts

if tgtPorts=="yes" or tgtPorts=="ya" or tgtPorts=="sure" or tgtPorts=="ok" or tgtPorts=="y" or tgtPorts=="Y":

tgtPorts=21, 22, 23, 25, 42, 43, 53, 67, 79, 80, 102, 110, 115, 119, 123, 135, 137, 143, 161, 179, 379, 389, 443, 445, 465, 636, 993, 995, 1026, 1080, 1090, 1433, 1434, 1521, 1677, 1701, 1720, 1723, 1900, 2409, 3101, 3306, 3389, 3390, 3535, 4321, 4664, 5190, 5500, 5631, 5632, 5900, 7070, 7100, 8000, 8080, 8799, 8880, 9100, 19430, 39720

try:

tgtIP=gethostbyname(tgtHost)

except:

print(" Cannot resolve '%s': Unknown host"%tgtHost)

return

try:

tgtName=gethostbyaddr(tgtIP)

print('\n Scan results for: %s' %tgtName)

except:

print('\n Scan results for: %s' %tgtIP)

tgtPorts=tgtPorts.split(',')

for port in tgtPorts:

print('Scanning port %s'%port)

connScan(tgtHost, int(port))

def main():

portScan(tgtHost, tgtPorts)

if __name__=='__main__':

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值