import sys
import os
import socket
import threading
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QLabel, QLineEdit, QPushButton, QTableWidget, QTableWidgetItem, QAbstractItemView
from PyQt5.QtCore import Qt

class DeviceScanner(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 400, 300)
        self.setWindowTitle('Device Scanner')

        grid = QGridLayout()
        self.setLayout(grid)

        label = QLabel('Enter 5-digit number:')
        grid.addWidget(label, 0, 0)

        self.inputBox = QLineEdit()
        self.inputBox.returnPressed.connect(self.startScan)
        grid.addWidget(self.inputBox, 0, 1)

        self.startButton = QPushButton('Start')
        self.startButton.clicked.connect(self.startScan)
        grid.addWidget(self.startButton, 0, 2)

        self.tableWidget = QTableWidget()
        self.tableWidget.setRowCount(0)
        self.tableWidget.setColumnCount(3)
        self.tableWidget.setHorizontalHeaderLabels(['IP', 'Device', 'Status'])
        self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
        grid.addWidget(self.tableWidget, 1, 0, 1, 3)

        self.devices = {
            '8': 'Server',
            '10': 'Router',
            '31': 'Printer',
            '40': 'Switch',
            '41': 'UPS',
            '50': 'PC',
            '60': 'POS1',
            '61': 'POS2',
            '63': 'POS3',
            '101': 'PDA101',
            '102': 'PDA102'
        }

        self.scanning = False
        self.thread = None

    def startScan(self):
        if not self.scanning:
            self.scanning = True
            self.startButton.setText('Scanning...')
            self.inputBox.setEnabled(False)
            self.thread = threading.Thread(target=self.scanDevices)
            self.thread.start()
        else:
            self.scanning = False
            self.startButton.setText('Start')
            self.inputBox.setEnabled(True)

    def scanDevices(self):
        number = self.inputBox.text()
        if len(number) != 5:
            print('Invalid input')
            return

        pthxxxxx = 'pth' + number
        ip = socket.gethostbyname(pthxxxxx)

        octets = ip.split('.')
        device_id = octets[3]
        device_type = self.devices.get(device_id, 'Unknown')

        self.tableWidget.setRowCount(0)
        row = self.tableWidget.rowCount()
        self.tableWidget.insertRow(row)

        item = QTableWidgetItem(ip)
        self.tableWidget.setItem(row, 0, item)

        item = QTableWidgetItem(device_type)
        self.tableWidget.setItem(row, 1, item)

        self.checkStatus(ip, row)

    def checkStatus(self, ip, row):
        try:
            response = os.system("ping -c 1 " + ip)
            if response == 0:
                item = QTableWidgetItem('')
                item.setFlags(Qt.ItemIsEnabled)
                item.setBackground(Qt.green)
                self.tableWidget.setItem(row, 2, item)
            else:
                item = QTableWidgetItem('')
                item.setFlags(Qt.ItemIsEnabled)
                item.setBackground(Qt.red)
                self.tableWidget.setItem(row, 2, item)
        except:
            print('Error checking status')

        if self.scanning:
            self.tableWidget verticalHeader().resizeSection(row, 20)
            QTimer.singleShot(1000, lambda: self.checkStatus(ip, row))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    scanner = DeviceScanner()
    scanner.show()
    sys.exit(app.exec_())

แก้ไข