-
Notifications
You must be signed in to change notification settings - Fork 54
/
mcping.py
executable file
·36 lines (31 loc) · 1.02 KB
/
mcping.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
import socket
import struct
from util import hook
def get_info(host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((host, port))
sock.send('\xfe')
response = sock.recv(1)
if response != '\xff':
return "Server gave invalid response: "+repr(response)
length = struct.unpack('!h', sock.recv(2))[0]
values = sock.recv(length*2).decode('utf-16be').split(u'\xa7')
sock.close()
return "%s - %d/%d players" % (values[0], int(values[1]), int(values[2]))
except:
return "Error pinging "+host+":"+str(port)+", is it up? Double-check your address!"
@hook.command
def mcping(inp):
".mcping server[:port] - Ping a Minecraft server to check status."
inp = inp.strip().split(" ")[0]
if ":" in inp:
host, port = inp.split(":", 1)
try:
port = int(port)
except:
return "Invalid port!"
else:
host = inp
port = 25565
return get_info(host, port)