首页 > 代码库 > python扫描ip的端口打开情况

python扫描ip的端口打开情况

我们的韩国bss系统上线之后,要求对主机的端口、资源使用进行统计,端口每个主机去看,太费劲了,所以,就写了这样一个小程序,不是很完美但是,可以用啊!哈哈哈,别喷,本人是个菜鸟

#!/usr/bin/python
# -*- coding:utf-8 -*-
import socket
import sys


#connector
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_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": "vpn-1-30",
            "ip": "xxx.xxx.xxx..30"
        }
    ]
}
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()


python扫描ip的端口打开情况