码迷,mamicode.com
首页 > 编程语言 > 详细

python例子-Nmap扫描IP并更新

时间:2015-10-20 14:01:18      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:

# (1)、将111.206.217.0/24、218.75.110.0/24两个网段IP地址插入proxy.nmap_ip表中,type字段按照个人序号插入。
def readIpByAddr():
    ip_list= []
    for i in range(255):
        i = 111.206.217.%s % i
        j = 218.75.110.%s % i
        ip_list.append(i)
        ip_list.append(j)
    return ip_list

def readIpByFile(name):
    frlist = open(name,r).readlines()
    ip_list = []
    for ip in frlist:
        ip = ip.strip()
        ip_list.append(ip)

    return ip_list

def insertIP():
    #iplist = readIpByAddr()
    iplist = readIpByFile(iplist.txt);
    conn = db_conn(192.168.88.103,root,cosmysql,proxy)
    cursor = conn.cursor()
    for j in iplist:
        cmdsql = "insert into nmap_ip(ip) values(‘%s‘)" % (j)
        print cmdsql
        cursor.execute(cmdsql)
    cursor.close()
    conn.close()
    
# (2)、使用nmap扫描上述IP的开放端口,并区分http端口和其它端口,并更新数据库;
def scan_ip():
    conn = db_conn(192.168.88.103,root,cosmysql,proxy)
    cursor = conn.cursor()
    cursor.execute(select ip from nmap_ip)
    result = cursor.fetchall()                        #获取所有IP列表
    for ip in result:
        nmap = "nmap %s | grep ‘open‘" %ip[0]        #构造namp扫描语句
        nmap_result = os.popen(nmap).readlines()    #扫描并获取结果
        if nmap_result == []:                            #Host 未开启
            print ip[0],:NoOpen!
        else :
            print ip[0],:Uped,next updateDB  #format like :[‘80/tcp  open  http\n‘, ‘443/tcp open  https\n‘]; DB like: ip,http_port,other_port,title,type
            http_port = ‘‘
            other_port =‘‘
            for port_result in nmap_result:            #遍历扫描结果.并构造sql更新语句.
                port_result = port_result.strip().replace(/tcp,‘‘).split( )
                if port_result[len(port_result)-1] == http:
                    http_port = port_result[0]
                else:
                    other_port = other_port + port_result[0] + ,
            update_sql = "update nmap_ip set http_port=‘%s‘,other_port=‘%s‘ where ip=‘%s‘;" % (http_port,other_port,ip[0])
            print update_sql
            update_reslut = cursor.execute(update_sql)
    cursor.close()    #关闭光标.
    conn.close()    #关闭连接.

# (3)、访问上述IP的http端口,取得title并更新数据库。
def update_title():
    try:
        conn = db_conn(192.168.88.103,root,cosmysql,proxy)
        cursor = conn.cursor()
        cursor.execute("select IP,http_port from nmap_ip where length(http_port) > 0 ;")
        result = cursor.fetchall()
        pattern = re.compile(<title>(.*?)</title>)
        for ip in result:
            url = http://%s:%s % (ip[0],ip[1].replace(/tcp,‘‘))
            print url
            try:
                response = urllib2.urlopen(url)
                html = response.read()
                items = re.findall(pattern,html)
                update_sql = "‘update nmap_ip set title=‘%s‘ where id=‘%s‘ ;" % (items[0][0],ip)
                print update title:,update_sql
                cursor.execute(update_sql)

            except Exception, e:
                print e
                pass
    except Exception, e:
        print e
        pass

def main4():
    #insertIP()
    #scan_ip()
    update_title()
if __name__ == __main__:
    main4()

其中iplist.txt类似于如下:

101.1.16.123
101.227.252.130
101.4.136.34
101.4.136.66
101.4.60.43
101.4.60.46
101.4.60.47
101.71.27.120
103.246.244.161
103.248.254.172
103.248.254.173
103.248.254.174
103.248.254.175
103.27.24.113
103.27.24.114
103.27.24.114
106.37.177.251
110.173.0.58
110.173.0.58
110.173.54.154
110.75.120.170
111.12.128.167

 

python例子-Nmap扫描IP并更新

标签:

原文地址:http://www.cnblogs.com/xccnblogs/p/4894457.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!