Skip to content

Commit d00d050

Browse files
kill leftover netcat instances on exit
check if router is down
1 parent 00c8337 commit d00d050

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

cli.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import argparse
2+
import os
23
import subprocess
4+
import psutil
35
import threading
46
import time
57
from ipaddress import IPv4Network
@@ -104,15 +106,21 @@ def update_peer_list():
104106
peer list
105107
:return: None
106108
"""
109+
octets = ip.split(".")
110+
router = "{0}.{1}.{2}.1".format(octets[0], octets[1], octets[2])
111+
if utils.Utils.check_host_up(router, 80):
112+
for i, peer in enumerate(dht1["peer_list"]):
113+
host, port = peer.split(":")
114+
peer_list = dht1["peer_list"]
115+
if not utils.Utils.check_host_up(host, int(port)):
116+
peer_list.pop(i)
117+
dht1["peer_list"] = peer_list
118+
else:
119+
print("\nrouter down! you are not connected to the Internet")
120+
os._exit(1)
107121
timer = threading.Timer(15.0, update_peer_list)
108122
timer.daemon = True
109123
timer.start()
110-
for i, peer in enumerate(dht1["peer_list"]):
111-
host, port = peer.split(":")
112-
peer_list = dht1["peer_list"]
113-
if not utils.Utils.check_host_up(host, int(port)):
114-
peer_list.pop(i)
115-
dht1["peer_list"] = peer_list
116124

117125

118126
# call peer list updater
@@ -121,14 +129,29 @@ def update_peer_list():
121129

122130
update_peer_list()
123131

132+
133+
def cleanup():
134+
"""
135+
Cleans up after shutdown, kills netcat processes
136+
:return: Nothing
137+
"""
138+
PROCNAME = "netcat"
139+
140+
for proc in psutil.process_iter():
141+
# check whether the process name matches
142+
if proc.name() == PROCNAME:
143+
print("Killed [{1}]{0}".format(PROCNAME, proc.pid))
144+
proc.kill()
145+
124146
while True:
125147
command = input("Enter a command:")
126148
command = command.split(" ")
127149
if command[0] == "/exit":
128150
peer_list = dht1["peer_list"]
129151
peer_list.remove("{0}:{1}".format(ip, PORT))
130152
dht1["peer_list"] = peer_list
131-
exit()
153+
cleanup()
154+
exit("Shutdown was successful")
132155
elif command[0] == "/push":
133156
if len(command) != 3:
134157
print("Incorrect usage: /push <key> <value>")

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ matplotlib
22
networkx
33
logbook
44
python-dotenv
5-
netifaces
5+
netifaces
6+
psutil

test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from dht import utils
44

5-
host = '192.168.0.66'
6-
port = 9789
5+
host = '192.168.0.1'
6+
port = 80
77

88

99
print(utils.Utils.check_host_up(host, port))

0 commit comments

Comments
 (0)