Skip to content

Instantly share code, notes, and snippets.

@Z1ni
Last active March 13, 2021 01:29
Show Gist options
  • Save Z1ni/b96a34793a688d7cd130c981c13a855a to your computer and use it in GitHub Desktop.
Save Z1ni/b96a34793a688d7cd130c981c13a855a to your computer and use it in GitHub Desktop.
Valheim / Valve Game Networking Sockets server online checker
#!/usr/bin/env python3
# Valheim/Valve Game Networking Sockets server online checker
# Zini, 2021
# Usage:
# ./server_online_check.py hostname_or_ip port
# Hostname defaults to localhost, port defaults to 2456
import socket
import sys
def is_server_online(server_addr) -> bool:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Create k_ESteamNetworkingUDPMsg_ConnectionClosed packet with zero "to_connection_id".
# 24 05 00 25 00 00 00 00
# ^ ^---^ ^ ^---------^
# | | | |
# | | | 32-bit "to_connection_id"
# | | |
# | | Protobuf field ID (0 0100 101)
# | | ^ ^
# | | | |
# | | | Wire type (5, 32-bit (fixed32))
# | | Field number (4)
# | Message length (Little endian)
# Message ID (ConnectionClosed)
connclosed_pkt = b"\x24\x05\x00\x25\x00\x00\x00\x00"
# Pad the packet to be 512 bytes
padded = connclosed_pkt + (b"\x00" * (512 - len(connclosed_pkt)))
# Send to the server
sock.sendto(padded, server_addr)
sock.settimeout(3.0)
try:
resp, _ = sock.recvfrom(1)
except:
return False
if resp == b"\x25":
# Server using Valve Game Networking Sockets is alive at this address.
# The server responded with k_ESteamNetworkingUDPMsg_NoConnection, indicating
# that there is no connection to close. The server doesn't want a reply to this message.
return True
# Server responded, but with something else
# There is something online, but it most likely isn't using Valve Game Networking Sockets
return False
if __name__ == "__main__":
server_addr = ("localhost", 2456)
if len(sys.argv) == 2:
server_addr = (sys.argv[1], server_addr[1])
elif len(sys.argv) > 2:
server_addr = (sys.argv[1], int(sys.argv[2]))
if is_server_online(server_addr):
print("Server online")
sys.exit(0)
else:
print("Server offline")
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment