pyqt IP段端口扫描工具

  • A+
所属分类:神兵利刃

pyqt IP段端口扫描工具pyqt IP段端口扫描工具pyqt IP段端口扫描工具pyqt IP段端口扫描工具


STCP.py

# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
import sys
import socket
import threading,time
socket.setdefaulttimeout(10)  #设置了全局默认超时时间
import thread
import ini
import time #获取时间和延时
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(272, 482)
        self.textEdit = QtGui.QTextEdit(Form)
        self.textEdit.setGeometry(QtCore.QRect(60, 10, 201, 31))
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.textEdit_2 = QtGui.QTextEdit(Form)
        self.textEdit_2.setGeometry(QtCore.QRect(60, 50, 201, 31))
        self.textEdit_2.setObjectName(_fromUtf8("textEdit_2"))
        self.textEdit_3 = QtGui.QTextEdit(Form)
        self.textEdit_3.setGeometry(QtCore.QRect(60, 90, 81, 31))
        self.textEdit_3.setObjectName(_fromUtf8("textEdit_3"))
        self.label = QtGui.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(10, 30, 54, 12))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(10, 70, 54, 12))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.label_3 = QtGui.QLabel(Form)
        self.label_3.setGeometry(QtCore.QRect(20, 110, 54, 12))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(160, 90, 101, 31))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        #self.pushButton.clicked.connect(self.mess)
        self.textEdit_4 = QtGui.QTextEdit(Form)
        self.textEdit_4.setGeometry(QtCore.QRect(10, 150, 251, 321))
        self.textEdit_4.setObjectName(_fromUtf8("textEdit_4"))
        self.label_4 = QtGui.QLabel(Form)
        self.label_4.setGeometry(QtCore.QRect(70, 130, 251, 25))
        self.label_4.setObjectName(_fromUtf8("label_4"))


        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)


        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.test)
    def test(self):
        thread.start_new_thread(self.mess, ())
    def mess(self):
        #self.label_4.setText(u"正在扫描:")
        #QtGui.QMessageBox.information(None, u"消息", u"消息提示")
#        data=self.textEdit_2.toPlainText() #获取内容
#        QtGui.QMessageBox.information(None, u"消息",data)
        ip1=self.textEdit.toPlainText() #获取内容
        ip2=self.textEdit_2.toPlainText() #获取内容
        port=self.textEdit_3.toPlainText() #获取内容
        ini.ini_write(ip1,ip2,port)  #修改INI
#        if ip1>=ip2:
#            self.textEdit_4.append(u"开始IP大于结束IP请重新输入")
#            QtGui.QMessageBox.information(None, u"消息", u"开始IP大于结束IP请重新输入")
#            return 0


        self.textEdit_4.append(u"扫描结果会保存在程序目录下ip.txt")
        list_ip=self.gen_ip(self.ip2num(ip1),self.ip2num(ip2))
        self.pushButton.setEnabled(0)  #给按钮改成禁用
        self.textEdit_4.append(u"需要扫描"+str(len(list_ip))+u"个IP")
        I1 = 0 #得到list的第一个元素
        ip=0
        self.textEdit_4.append(u"开始扫描IP--"+time.strftime('%Y.%m.%d-%H.%M.%S'))


        while I1 < len(list_ip):
            if ip>=200:
                ini.ini_write(list_ip[I1],ini.IP2,port)  #修改INI
                ip=0
                print list_ip[I1]
            ip = ip + 1
            #self.textEdit_4.append(list_ip[I1])
            time.sleep(0.1) #确保先运行Seeker中的方法
            thread.start_new_thread(self.socket_port,(list_ip[I1],int(port)))
            I1 = I1 + 1   #一层
        self.textEdit_4.append(u"IP扫描完成--"+time.strftime('%Y.%m.%d-%H.%M.%S'))
        self.pushButton.setEnabled(1)  #给按钮改成可用


    def socket_port(self,ip,PORT):
        try:
            self.label_4.setText(U"正在扫描IP:"+str(ip)+u":"+str(PORT))
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((ip,PORT))
            #print ip,u":",PORT,u"端口开放"
            self.textEdit_4.append(str(ip)+u":"+str(PORT)+u"端口开放")
            xxx=file('ip.txt','w')
            xxx.write(str(ip))
            xxx.write('\n')
            xxx.close()
        except:
            print ip,u":",PORT,u"端口未开放"
            #self.textEdit_4.append(ip)
#----------------------------------------------------------------
    def ip2num(self,ip):
        ip = [int(x) for x in ip.split('.')]
        return ip[0]<<24 | ip[1]<<16 | ip[2]<<8 | ip[3]


    def num2ip(self,num):
        if num>=IPend:
            self.textEdit_4.append(u"IP导入数组完成")
        return '%s.%s.%s.%s' % (  (num & 0xff000000) >> 24,
                                  (num & 0x00ff0000) >> 16,
                                  (num & 0x0000ff00) >> 8,
                                  num & 0x000000ff  )


    def gen_ip(self,Aip1,Aip2):  #返回数组
        global IPend
        IPend=Aip2
        return [self.num2ip(num) for num in range(Aip1,Aip2+1) if num & 0xff]
#----------------------------------------------------------------
    def iniA(self):
        ini.ini_get()  #读取INI
        self.textEdit.setPlainText(ini.IP1)
        self.textEdit_2.setPlainText(ini.IP2)
        self.textEdit_3.setPlainText(ini.port)


    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "QQ316118740", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("Form", "开始IP:", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("Form", "结束IP:", None, QtGui.QApplication.UnicodeUTF8))
        self.label_3.setText(QtGui.QApplication.translate("Form", "端口:", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("Form", "开始扫描", None, QtGui.QApplication.UnicodeUTF8))
        self.label_4.setText(QtGui.QApplication.translate("Form", "扫描结果--pyqt端口扫描器", None, QtGui.QApplication.UnicodeUTF8))


class Start(QtGui.QMainWindow):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.ui=Ui_Form()#这儿跟上面那个class一样
        self.ui.setupUi(self)
        self.ui.iniA()


if __name__=="__main__":
    app=QtGui.QApplication(sys.argv)
    myapp=Start()
    myapp.show()
    sys.exit(app.exec_())

ini.py

#!/usr/local/bin/python
#-*- coding: UTF-8 -*-
##################################################
#qq:316118740
#BLOG:http://hi.baidu.com/alalmn
# Python 操作ini文件
#  刚学写的不好请大家见谅
##################################################
IP1=""  #扫描IP
IP2=""   #当前已经扫到的IP
port=""  #扫描端口
INITXT="IP.ini"  #INI文件名字


import ConfigParser
def ini_get():  #读取INI
    try:
        global IP1
        global IP2
        global port
        global INITXT
        config = ConfigParser.ConfigParser()
        config.readfp(open(INITXT))
        IP1 = config.get("ipdata","ip1")
        IP2 = config.get("ipdata","ip2")
        port = config.get("ipdata","port")
    except:
        print "读取INI错误"
        ini_add("","","")  #写入INI


def ini_add(ip1,ip2,pt):  #写入INI
    try:
        global INITXT
        config = ConfigParser.ConfigParser()
        config.add_section("ipdata")# 设置section段及对应的值
        config.set("ipdata","ip1",ip1)
        config.set("ipdata","ip2",ip2)
        config.set("ipdata","port",pt)
        config.write(open(INITXT, "w"))# 写入文件
    except:
       print "写入INI错误"


def ini_write(ip1,ip2,pt):  #修改INI
    try:
        global INITXT
        config = ConfigParser.ConfigParser()
        config.read(INITXT)
        if not config.has_section("ipdata"):#看是否存在该Section,不存在则创建
            temp = config.add_section("")
        config.set("ipdata","ip1",ip1)
        config.set("ipdata","ip2",ip2)
        config.set("ipdata","port",pt)
        config.write(open(INITXT, "r+"))
    except:
        print "修改INI错误"
        ini_add("","")  #写入INI




#if __name__=='__main__':
##    ini_get()  #读取INI
##    print IP1
##    print IP2
#
##    ini_add("222222222","3333333333333")  #写入INI
##    ini_get()  #读取INI
##    print IP1
##    print IP2
#
#    ini_write("999999999","0000000000")  #修改INI
#    ini_get()  #读取INI
#    print IP1
#    print IP2

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: