python扫描IP段指定端口是否开放,可一次扫描20个B网段。
代码:
 
代码:
 
#!bin/python
#-----------------------
import sys
import os
import atexit
def close():  #自动重启本程序
    try:
        print u"------------------自动重启本程序------------------"
        mysql.mysql_S()  #保存数据
        mysql.mysql_close()  #关闭数据库
    #    python = sys.executable
    #    os.execl(python, python, * sys.argv)
    #    mysql.mysql_close()  #关闭数据库
        time.sleep(10)
        #    os.system('python txt.py')
        python = sys.executable
        os.execl(python, python, * sys.argv)
        #########
    except:
        print u"------------------自动重启本程序---异常------------------"
        sys.exit(0)  #结束进程
def dqsqlite_bcini():  #扫描完成后  读取配置信息后在自+1  在读取数据库相应信息  在保存信息  重启程序就可以了
    try:
        ini.ini_get()  #读取INI
        abc=int(ini.ID)+1   #获取要查询的ID
        sql_desc = "SELECT * FROM ip where ID='%s'"%(abc)
        n = mysql.cursor.execute(sql_desc)
        mysql.cursor.scroll(0)
        for row in mysql.cursor.fetchall():
            ini.ini_write(abc,row[1],row[2])  #修改INI
    #获取游标
#        sqlite_cursor = sqlite.sqlite_conn.cursor()
#        sql_desc = "SELECT * FROM ip where ID='%s'"%(abc)
#        sqlite_cursor.execute(sql_desc)
#        for row in sqlite_cursor:
#            ini.ini_write(abc,row[1],row[2])  #修改INI
#        sqlite.sqlite_cursor.close()   #关闭游标
        #return 1
        atexit.register(close)#自动重启本程序
    except:
        print u"数据库读取异常!!"
        #return 0
#    sqlite.sqlite_S()  #保存数据
 #   sqlite.sqlite_close()  #关闭数据库
        atexit.register(close)#自动重启本程序
import ini
import mysql
if __name__=='__main__':
    try:
        mysql.mysql_open()  #连接数据库
        ini.ini_get()  #读取INI
        print u"开始IP:",ini.IP1,u"-------",u"结束IP:",ini.IP2
        if ini.IP1>=ini.IP2:
            print u"IP以扫描完成"
            dqsqlite_bcini()  #扫描完成后  读取配置信息后在自+1  在读取数据库相应信息  在保存信息  重启程序就可以了
        print u"IP还没扫描完"
        list_ip=gen_ip(ip2num(ini.IP1),ip2num(ini.IP2))
        I1 = 0 #得到list的第一个元素
        print u"开始扫描IP"
        ip=0
        while I1 < len(list_ip):
            if ip>=255:
                ini.ini_write(int(ini.ID),list_ip[I1],ini.IP2)  #修改INI
                ip=0
                print list_ip[I1]
            ip = ip + 1
            #print list_ip[I1]
            time.sleep(0.1) #确保先运行Seeker中的方法
            cond = threading.Event()
            hider = socket_port(cond,list_ip[I1])
            hider.start()
            I1 = I1 + 1   #一层
        print u"IP扫描完成将从新导入"
        dqsqlite_bcini()  #扫描完成后  读取配置信息后在自+1  在读取数据库相应信息  在保存信息  重启程序就可以了
    except:
        #dqsqlite_bcini()  #扫描完成后  读取配置信息后在自+1  在读取数据库相应信息  在保存信息  重启程序就可以了
        atexit.register(close)