-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.py
31 lines (23 loc) · 961 Bytes
/
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
import sys
import json
import struct
import subprocess
def getMessage():
rawLength = sys.stdin.buffer.read(4)
messageLength = struct.unpack("@I", rawLength)[0]
message = sys.stdin.buffer.read(messageLength).decode("utf-8")
return json.loads(message)
def encodeMessage(messageContent):
encodedContent = json.dumps(messageContent).encode("utf-8")
encodedLength = struct.pack("@I", len(encodedContent))
return {"length": encodedLength, "content": encodedContent}
def sendMessage(encodedMessage):
sys.stdout.buffer.write(encodedMessage["length"])
sys.stdout.buffer.write(encodedMessage["content"])
sys.stdout.buffer.flush()
while True:
host = getMessage()
run_args = ["cmd", "/c", "ping_batch.bat", host] # Show cmd.
# run_args = ["ping", host] # Do not use cmd.
res = subprocess.run(run_args, stdout=subprocess.PIPE)
sendMessage(encodeMessage(str(res.returncode)))