[ServerUp] Only measure the RTT

master
sfan5 2014-11-20 22:20:28 +01:00
parent 6bcc3ab9e0
commit 9328b211c8
1 changed files with 16 additions and 19 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python
"""
serverup.py - Minetest-Server Ping Module
Copyright 2012, sfan5
serverup.py - Minetest server ping Module
Copyright 2014, sfan5
"""
import socket, time
@ -9,11 +9,11 @@ import socket, time
def serverup(phenny, input):
arg = input.group(2)
if not arg:
return phenny.reply("Give me a Server Address")
return phenny.reply("give me an address (and port if you want)")
if not '.' in arg:
return phenny.reply("Invalid Address")
return phenny.reply("invalid address")
if ':' in arg:
return phenny.reply("Syntax changed, please use 'example.org 1337' instead of 'example.org:1337'")
return phenny.reply("use 'example.org 1337' instead of 'example.org:1337'")
if ' ' in arg:
address = arg.split(' ')[0]
port = arg.split(' ')[1]
@ -23,61 +23,58 @@ def serverup(phenny, input):
for p in ports_:
if '-' in p:
if len(p.split('-')) != 2:
return phenny.reply("Invalid Port List")
return phenny.reply("invalid port list")
else:
try:
a = int(p.split('-')[0])
except:
return phenny.reply("Invalid Port: %s" % p.split('-')[0])
return phenny.reply("invalid port: %s" % p.split('-')[0])
try:
b = int(p.split('-')[1]) + 1
except:
return phenny.reply("Invalid Port: %s" % p.split('-')[1])
return phenny.reply("invalid port: %s" % p.split('-')[1])
for i in range(a, b):
ports.append(i)
else:
try:
ports.append(int(p))
except:
return phenny.reply("Invalid Port: %s" % p)
return phenny.reply("invalid port: %s" % p)
else:
try:
ports = [int(port)]
except:
return phenny.reply("Invalid Port: %s" % port)
return phenny.reply("invalid port: %s" % port)
else:
address = arg
ports = [30000]
if len(ports) != 1 and input.sender.startswith('#') and not (input.admin or input.owner):
return phenny.reply("To check multiple Ports please use Query")
if len(ports) > 6 and not (input.admin or input.owner): # Owner and Admins of the Bot can bypass the Limit
return phenny.reply("Too many Ports specified")
return phenny.reply("to check multiple ports please use private chat")
if len(ports) > 6 and not (input.admin or input.owner): # owner and admins of the bot can bypass the limit
return phenny.reply("ow, too many ports!")
for port in ports:
repres = address + ':' + str(port)
try:
start = time.time()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(2.5)
buf = b"\x4f\x45\x74\x03\x00\x00\x00\x01"
sock.sendto(buf, (address, port))
start = time.time()
data, addr = sock.recvfrom(1000)
if data:
end = time.time()
peer_id = data[12:14]
buf = b"\x4f\x45\x74\x03" + peer_id + b"\x00\x00\x03"
sock.sendto(buf, (address, port))
sock.close()
end = time.time()
t = (end-start)*1000
t = (end - start) * 1000
phenny.say("%s is up (%dms)" % (repres,t))
else:
phenny.say("%s seems to be down " % repres)
except:
phenny.say("%s seems to be down " % repres)
serverup.commands = ['up']
serverup.thread = True
if __name__ == '__main__':
print(__doc__)