Last active
April 5, 2025 12:45
-
-
Save R3DPanda1/9fff88352dabf8df11726dae52329fc6 to your computer and use it in GitHub Desktop.
PowerShell script to make BambuStudio v2.x discover specific printers by IP address.
This file contains 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
<# | |
BSnotify PowerShell Version – Made with ChatGPT | |
-------------------------------------------------- | |
This script was derived from: | |
- https://github.com/jonans/bsnotify/tree/main | |
- https://gist.github.com/Alex-Schaefer/72a9e2491a42da2ef99fb87601955cc3 | |
- https://gist.github.com/fritzw/7d981046b509a27e204d01c9bd73f37b | |
It automatically detects the local IPv4 address (ignoring loopback), then continuously (every 10 seconds) sends a spoofed SSDP NOTIFY message for each configured printer. | |
This enables Bambu Studio or Orca Slicer to discover printers even if they’re on different subnets or VLANs. | |
Usage: | |
Edit the parameters inside the script and then simply run it so that it executes in PowerShell. | |
Replace the placeholder values with your own printer data if needed. | |
#> | |
# Function to auto-detect a local IPv4 address (ignores loopback addresses) | |
function Get-LocalIPAddress { | |
$addresses = [System.Net.Dns]::GetHostAddresses([System.Net.Dns]::GetHostName()) | | |
Where-Object { $_.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork -and $_.IPAddressToString -ne "127.0.0.1" } | |
if ($addresses.Count -gt 0) { | |
return $addresses[0].IPAddressToString | |
} | |
else { | |
Write-Error "No valid local IPv4 address found." | |
exit 1 | |
} | |
} | |
# Auto-detect the local IP address | |
$LocalIP = Get-LocalIPAddress | |
# Define the source port used for binding (default is 1900 but gives me an error in PowerShell) | |
$SourcePort = 1901 | |
# Printer configurations (replace placeholders with actual values) | |
$printers = @( | |
@{ | |
"PRINTER_USN" = "<PRINTER_SERIAL_1>" | |
"PRINTER_DEV_MODEL" = "N2S" | |
"PRINTER_DEV_NAME" = "<PRINTER_NAME_1>" | |
"PRINTER_IP" = "<PRINTER_IP_1>" | |
}, | |
@{ | |
"PRINTER_USN" = "<PRINTER_SERIAL_2>" | |
"PRINTER_DEV_MODEL" = "N2S" | |
"PRINTER_DEV_NAME" = "<PRINTER_NAME_2>" | |
"PRINTER_IP" = "<PRINTER_IP_2>" | |
} | |
) | |
# Function to build the SSDP NOTIFY message for a given printer | |
function Get-SSDPNotifyMessage ($printer) { | |
$date = [DateTime]::UtcNow.ToString("R") # RFC1123 format date (UTC) | |
$msg = "HTTP/1.1 200 OK`r`n" + | |
"Server: Buildroot/2018.02-rc3 UPnP/1.0 ssdpd/1.8`r`n" + | |
"Date: $date`r`n" + | |
"Location: $($printer.PRINTER_IP)`r`n" + | |
"ST: urn:bambulab-com:device:3dprinter:1`r`n" + | |
"EXT:`r`n" + | |
"USN: $($printer.PRINTER_USN)`r`n" + | |
"Cache-Control: max-age=1800`r`n" + | |
"DevModel.bambu.com: $($printer.PRINTER_DEV_MODEL)`r`n" + | |
"DevName.bambu.com: $($printer.PRINTER_DEV_NAME)`r`n" + | |
"DevSignal.bambu.com: -44`r`n" + | |
"DevConnect.bambu.com: lan`r`n" + | |
"DevBind.bambu.com: free`r`n`r`n" | |
return $msg | |
} | |
# Function to send the SSDP NOTIFY message for a single printer | |
function Send-SSDPNotify ($printer, $LocalIP, $SourcePort) { | |
try { | |
# Create a local IPEndPoint using the auto-detected LocalIP and SourcePort | |
$localEndpoint = New-Object System.Net.IPEndPoint ([System.Net.IPAddress]::Parse($LocalIP), $SourcePort) | |
} | |
catch { | |
Write-Error "Failed to parse local IP address: $LocalIP" | |
exit 1 | |
} | |
# Create a UDP client and bind it to the specified local endpoint | |
$udpClient = New-Object System.Net.Sockets.UdpClient | |
try { | |
$udpClient.Client.Bind($localEndpoint) | |
} | |
catch { | |
Write-Error "Failed to bind to ${LocalIP}:${SourcePort}. Ensure this IP is assigned to your machine or try a different source port." | |
exit 1 | |
} | |
$udpClient.EnableBroadcast = $true | |
# Set up the destination endpoint: broadcast address on port 2021 | |
$destEndpoint = New-Object System.Net.IPEndPoint ([System.Net.IPAddress]::Parse("255.255.255.255"), 2021) | |
# Build the SSDP NOTIFY message and convert it to bytes using ASCII encoding | |
$message = Get-SSDPNotifyMessage $printer | |
$bytes = [System.Text.Encoding]::ASCII.GetBytes($message) | |
# Send the UDP packet | |
try { | |
$udpClient.Send($bytes, $bytes.Length, $destEndpoint) | Out-Null | |
} | |
catch { | |
Write-Error "Error sending UDP packet for printer $($printer.PRINTER_DEV_NAME): $_" | |
} | |
$udpClient.Close() | |
} | |
# Display a one-time informational message | |
Write-Host "BSnotify: Sending SSDP notifications every 10 seconds for configured printers..." | |
Write-Host "Auto-detected local IP: $LocalIP" | |
Write-Host "Press Ctrl+C to stop." | |
# Main loop: repeatedly send notifications every 10 seconds for each printer | |
while ($true) { | |
foreach ($printer in $printers) { | |
Send-SSDPNotify $printer $LocalIP $SourcePort | |
} | |
Start-Sleep -Seconds 10 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment