From ed8337414c35771fabb8334a7c95decfa6bd762c Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 29 Oct 2023 09:46:58 +0100 Subject: [PATCH] Minor changes --- clusterman/commands/node.py | 10 ++++------ clusterman/utils.py | 9 +++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/clusterman/commands/node.py b/clusterman/commands/node.py index 4d7b762..1b5a2ed 100644 --- a/clusterman/commands/node.py +++ b/clusterman/commands/node.py @@ -32,11 +32,10 @@ def scan(timeout): print("Skipping {}".format(ip)) continue print("Contacting {}...".format(ip),end='') - try: - subprocess.run(["ping", "-c", "1", "-W", str(timeout), ip],capture_output=True,check=True) + if not utils.ping_test(ip): nodes.append(ip) print("=> Found!!") - except: + else: print() with open(CONF.NODE_FILE, "w") as f: f.write(json.dumps(nodes,indent=4)) @@ -52,10 +51,9 @@ def check(timeout): fail=False for ip in nodes: print("Contacting {}...".format(ip),end='') - try: - subprocess.run(["ping", "-c", "1", "-W", str(timeout), ip],capture_output=True,check=True) + if not utils.ping_test(ip,timeout): print() - except: + else: fail=True print("=> Not responding!!") if not fail: diff --git a/clusterman/utils.py b/clusterman/utils.py index 36822fe..5fa66b2 100644 --- a/clusterman/utils.py +++ b/clusterman/utils.py @@ -11,6 +11,15 @@ def ssh_exec(host,command,use_key=True): output=subprocess.check_output(["ssh","-o", "StrictHostKeyChecking=no", "{}@{}".format(user,ip), command]) return output.decode("utf-8") +def ping_test(host, timeout=None): + t=CONF["timeout"] + if timeout is not None: + t=timeout + try: + subprocess.run(["ping", "-c", "1", "-W", str(t), host],capture_output=True,check=True) + return 0 + except: + return 1 def get_node_list(): nodes_path=CONF.NODE_FILE