Created
December 10, 2024 01:43
-
-
Save narner90/4c6cd23c452df990606e9b848938b8ee to your computer and use it in GitHub Desktop.
Advertise Bambu Printer over SSDP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import argparse | |
SOURCE_PORT = 1900 | |
DESTINATION_PORT = 2021 | |
MULTICAST_ADDRESS = '239.255.255.250' | |
def main(): | |
payload = get_payload() | |
send_udp_payload(MULTICAST_ADDRESS, SOURCE_PORT, DESTINATION_PORT, payload) | |
def get_payload() -> str: | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-i', '--printer-ip', required=True) | |
parser.add_argument('-p', '--printer-name', required=True) | |
parser.add_argument('-s', '--serial', required=True) | |
parser.add_argument('-c', '--model-code', required=True) | |
args = parser.parse_args() | |
printer_ip = args.printer_ip | |
printer_serial = args.serial | |
printer_model_code = args.model_code | |
printer_name = args.printer_name | |
printer_signal = -44 | |
printer_connection_mode = 'lan' | |
printer_bind_mode = 'free' | |
return f'''NOTIFY * HTTP/1.1\r | |
HOST: {MULTICAST_ADDRESS}:1900\r | |
Server: UPnP/1.0\r | |
Location: {printer_ip}\r | |
NT: urn:bambulab-com:device:3dprinter:1\r | |
USN: {printer_serial}\r | |
Cache-Control: max-age=1800\r | |
DevModel.bambu.com: {printer_model_code}\r | |
DevName.bambu.com: {printer_name}\r | |
DevSignal.bambu.com: {printer_signal}\r | |
DevConnect.bambu.com: {printer_connection_mode}\r | |
DevBind.bambu.com: {printer_bind_mode}\r | |
Devseclink.bambu.com: secure\r | |
DevVersion.bambu.com: 01.06.01.02\r | |
\r | |
''' | |
def send_udp_payload(host: str, source_port: int, destination_port: int, payload: str) -> None: | |
try: | |
process = subprocess.run( | |
['nc', '-p', str(source_port), '-u', '-w0', host, str(destination_port)], | |
input=payload, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, | |
text=True | |
) | |
# Check the process result | |
if process.returncode == 0: | |
print(f"Payload sent successfully to {host}:{source_port}") | |
else: | |
print(f"Error: {process.stderr}") | |
except FileNotFoundError: | |
print("Error: netcat (nc) command not found. Please install it.") | |
except Exception as e: | |
print(f"An unexpected error occurred: {e}") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment