forked from jerrynandak/AUNMS---Network-Monitoring-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.py
56 lines (47 loc) · 2 KB
/
ping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from scapy.all import *
from scapy.layers.inet import IP, ICMP, icmpcodes, icmptypes
class Ping(QWidget):
def __init__(self):
super().__init__()
self.hostn = QLabel("Enter Address/Hostname to ping:")
self.host = QLineEdit()
self.startPingBtn = QPushButton()
self.startPingBtn.setText('Ping')
self.startPingBtn.clicked.connect(self.startPing)
self.result = QTextEdit()
self.result.setReadOnly(True)
self.layoutPing = QVBoxLayout(self)
self.layoutPing.addWidget(self.hostn)
self.layoutPing.addWidget(self.host)
self.layoutPing.addWidget(self.startPingBtn)
self.layoutPing.addWidget(self.result)
self.setLayout(self.layoutPing)
def startPing(self):
if(len(self.host.text()) >= 1):
try:
packet = IP(dst=self.host.text())/ICMP()
result = ""
for i in range(1, 6):
output = sr1(packet, timeout=3, verbose=0)
if output is not None:
res_type = icmptypes[output.type]
res_code = ""
if output.type in icmpcodes:
res_code = " - "+icmpcodes[output.type][output.code]
ping_time = int((output.time - packet.sent_time)*1000)
result += "{}{} from {}:\ttime={}ms\tTTL={}\n".format(
res_type,
res_code,
packet[IP].dst,
ping_time,
packet[IP].ttl
)
else:
result += "Request timed out.\n"
self.result.setText(result)
except socket.gaierror:
self.result.setText(
"Invalid address/Could not get address info")