python扫描ip的端口打开情况
我们的韩国bss系统上线之后,要求对主机的端口、资源使用进行统计,端口每个主机去看,太费劲了,所以,就写了这样一个小程序,不是很完美但是,可以用啊!哈哈哈,别喷,本人是个菜鸟
#!/usr/bin/python
# -*- coding:utf-8 -*-
import Socket
import sys
#connector,使用socket模块,设定超时时间为0.1【内网环境下】,连接成功返回1,连接失败返回0
def connector(ip,port):
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.settimeout(0.1)
s.connect((ip,int(port)))
s.shutdown(2)
return 1
except:
return 0
finally:
s.close()
#程序开始,定义所有server,此处用了JSON,便于后面脚本好写
server_list={
"server-list": [
{
"name": "api-1-18",
"ip": "xxx.xxx.xxx.18"
},
{
"name": "api-2-19",
"ip": "xxx.xxx.xxx.19"
},
{
"name": "WEB-1-24",
"ip": "xxx.xxx.xxx.24"
},
{
"name": "db-1-21",
"ip": "xxx.xxx.xxx.21"
},
{
"name": "db-2-23",
"ip": "xxx.xxx.xxx.23"
},
{
"name": "ftp-1-26",
"ip": "xxx.xxx.xxx.26"
},
{
"name": "***-1-30",
"ip": "xxx.xxx.xxx..30"
}
]
}
#对列表中所有server的所有端口进行遍历扫描,并将打开的端口输出到终端
for server in server_list['server-list']:
for port in range(1,65535):
state=connector(server['ip'], port)
if state == 1:
print "["+str(port)+"] is open on this server ["+str(server['name'])+"]"
sys.exit()
相关文章