Skip to content

Instantly share code, notes, and snippets.

@odenak
Last active May 1, 2024 22:18
Show Gist options
  • Save odenak/b08110ce6ea56be558b8986ebc58344b to your computer and use it in GitHub Desktop.
Save odenak/b08110ce6ea56be558b8986ebc58344b to your computer and use it in GitHub Desktop.
Canon M644Cdw AirPrint Rename
#!/bin/sh
# This script will log in to Canon MF644Cdw's Remote UI and rename the AirPrint/Bonjour/mDNS name.
# This is a workaround to Canon MF644Cdw AirPrint keeps renaming itself.
# You can either run this manually or run it as a cronjob.
# Reference: https://community.usa.canon.com/t5/Printer-Software-Networking/imageCLASS-MF644Cdw-Airprint-name-keeps-changing/td-p/298543/page/2
# === CONFIG ===
PRINTER_HOST="192.168.#.#" # Canon MF644 local IP
PRINTER_PASS="password" # Canon Remote UI System Manager Mode Password
PRINTER_AIRPRINT_NAME="Canon Printer" # Desired AirPrint name
# === END CONFIG ===
# === DO NOT EDIT BELOW ===
PRINTER_WEB_LOGIN_URL="http://${PRINTER_HOST}/checkLogin.cgi"
PRINTER_WEB_AIRPRINT_FORM_URL="http://${PRINTER_HOST}/m_network_airprint_edit.html"
PRINTER_WEB_AIRPRINT_EDIT_URL="http://${PRINTER_HOST}/cgi/m_network_airprint_edit.cgi"
PRINTER_WEB_LOGOUT_URL="http://${PRINTER_HOST}/logout.cgi"
# Step 1: Login and extract session id
echo "Logging into Printer Web Portal: ${PRINTER_WEB_LOGIN_URL}"
SESS_ID="$(curl --data-urlencode "i0012=1" --data-urlencode "i2101=${PRINTER_PASS}" --silent -i ${PRINTER_WEB_LOGIN_URL} | grep "Set-Cookie" | sed -En 's/.*sessid=([^;]*).*/\1/p')"
# Step 2: Fetch AirPrint Settings Edit Form
echo "Fetching current AirPrint Settings"
EDIT_FORM="$(curl -b "sessid=${SESS_ID}" --silent ${PRINTER_WEB_AIRPRINT_FORM_URL})"
# Step 3: Parse Form For Current AirPrint Name
CURRENT_AIRPRINT_NAME="$(echo -n "${EDIT_FORM}" | grep "i2072" | sed -n 's/.*value="\([^"]*\)".*/\1/p')"
echo "Current AirPrint Name: \"${CURRENT_AIRPRINT_NAME}\"."
# Check if name is the same
if [ "${CURRENT_AIRPRINT_NAME}" = "${PRINTER_AIRPRINT_NAME}" ]
then
echo "Same as desired \"${PRINTER_AIRPRINT_NAME}\". No Renaming will occur."
else
echo "AirPrint name has changed. Renaming back to \"${PRINTER_AIRPRINT_NAME}\""
# Step 4: Get Airprint Edit Form Token
FORM_TOKEN="$(echo -n "${EDIT_FORM}" | grep "iToken" | sed -n 's/.*value="\([^"]*\)".*/\1/p')"
# Step 5: Set AirPrint (mDNS) Name
curl -b "sessid=${SESS_ID}" --silent --data-urlencode "iToken=${FORM_TOKEN}" --data-urlencode "i2072=${PRINTER_AIRPRINT_NAME}" ${PRINTER_WEB_AIRPRINT_EDIT_URL}
echo "Rename complete!"
fi
# Step 6: Logout
curl -b "sessid=${SESS_ID}" --silent ${PRINTER_WEB_LOGOUT_URL}
echo "Logged Out"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment