-
-
Save numericOverflow/ac84ffb2344409429fc3dd41aea2855f to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/env bash | |
| # Automated application of WiFi Print Server: From a Raspberry Pi Zero W to Windows 10/11 By brighterfusion from instructables | |
| # This helper script was written by Brian Khuu on 2023 for https://www.instructables.com/WiFi-Print-Server-From-a-Raspberry-Pi-Zero-W-to-Wi/ | |
| # To run this shell script on gist (ref: https://gist.github.com/numericOverflow/ac84ffb2344409429fc3dd41aea2855f) | |
| # Run this script via curl: | |
| # bash <(curl -sL https://gist.github.com/numericOverflow/ac84ffb2344409429fc3dd41aea2855f/raw/rpi_print_server_setup.bash) | |
| # Run this script via wget: | |
| # bash <(wget -o /dev/null -nv -O - https://gist.github.com/numericOverflow/ac84ffb2344409429fc3dd41aea2855f/raw/rpi_print_server_setup.bash) | |
| echo "Automated application of WiFi Print Server: From a Raspberry Pi Zero W to Windows 10/11 By brighterfusion from instructables" | |
| echo "Assuming you have already done Step 1 of prepping and loading in the MicroSD card and loggin in here via ssh" | |
| ### Install Packages ### | |
| echo "### Step 2: Initialize the RPi and Install Packages ###" | |
| # Sync RPI package lists (Removed 'upgrade -y' to prevent script hangs/reboots) | |
| sudo apt update -y | |
| # Install CUPS and SAMBA. | |
| # DEBIAN_FRONTEND=noninteractive prevents apt from hanging on config prompts | |
| sudo DEBIAN_FRONTEND=noninteractive apt install -y cups samba | |
| # Install the CUPS print server and SAMBA file sharing packages. This will take a several additional minutes. | |
| # Always add the current user to lpadmin so they have web access | |
| sudo usermod -a -G lpadmin $USER | |
| ### Config CUPS ### | |
| echo "### Step 3: Config CUPS ###" | |
| ######################################### | |
| # Ask if the user wants to set up a dedicated, shell-less admin user | |
| while true; do | |
| read -p "Do you want to setup a dedicated CUPS web-admin user? (y/n): " SETUP_ADMIN < /dev/tty | |
| case $SETUP_ADMIN in | |
| [Yy]* ) | |
| # Prompt for username (default: cups_admin_user) | |
| read -p "Enter CUPS admin username [cups_admin_user]: " ADMIN_USER < /dev/tty | |
| ADMIN_USER=${ADMIN_USER:-cups_admin_user} | |
| # Check if user already exists | |
| if id "$ADMIN_USER" &>/dev/null; then | |
| echo "User '$ADMIN_USER' already exists. Skipping user creation." | |
| else | |
| # Allow Web Authentication (Bypass PAM shell check) so we can add user | |
| if ! grep -q "/usr/sbin/nologin" /etc/shells; then | |
| echo "/usr/sbin/nologin" | sudo tee -a /etc/shells > /dev/null | |
| fi | |
| # Add user with no login shell | |
| sudo useradd -m -s /usr/sbin/nologin "$ADMIN_USER" | |
| echo "Created user '$ADMIN_USER'." | |
| # Prompt for password | |
| while true; do | |
| read -s -p "Enter password for '$ADMIN_USER': " ADMIN_PASS < /dev/tty | |
| echo "" < /dev/tty # Move to a new line after hidden input | |
| read -s -p "Confirm password for '$ADMIN_USER': " ADMIN_PASS_CONFIRM < /dev/tty | |
| echo "" < /dev/tty | |
| if [ "$ADMIN_PASS" = "$ADMIN_PASS_CONFIRM" ]; then | |
| if [ -z "$ADMIN_PASS" ]; then | |
| echo "Error: Password cannot be empty." | |
| continue | |
| fi | |
| break | |
| else | |
| echo "Error: Passwords do not match. Please try again." | |
| fi | |
| done | |
| # Set the password non-interactively | |
| echo "$ADMIN_USER:$ADMIN_PASS" | sudo chpasswd | |
| echo "Password set for '$ADMIN_USER'." | |
| fi | |
| # Ensure the user is in the administrative group for the printers | |
| # (This runs regardless of whether the user was just created or already existed) | |
| sudo usermod -aG lpadmin "$ADMIN_USER" | |
| echo "Ensured '$ADMIN_USER' is in the 'lpadmin' group." | |
| break;; | |
| [Nn]* ) | |
| echo "Skipping dedicated admin user setup. Current user ('$USER') will be used for CUPS admin." | |
| break;; | |
| * ) echo "Please answer y or n.";; | |
| esac | |
| done | |
| ######################################### | |
| # Tell cups to accept print jobs from other computers on the network | |
| # WARNING: --remote-any opens the port to all networks. Use with caution on public WiFi. | |
| sudo cupsctl --remote-any | |
| # Enable web-based admin pages | |
| sudo cupsctl WebInterface=yes | |
| # Restart CUPS | |
| sudo systemctl restart cups | |
| ### Configure SAMBA ### | |
| echo "### Step 4: Config SAMBA ###" | |
| # Instead of using fragile 'patch' line-numbers, use awk to find the exact sections | |
| # and change the variables safely, regardless of the Samba version. | |
| sudo awk ' | |
| /^\[printers\]/ { section="printers" } | |
| /^\[print\$\]/ { section="printers_share" } | |
| /^\[/ && !/^\[printers\]/ && !/^\[print\$\]/ { section="" } | |
| { | |
| if (section == "printers" && /guest ok = no/) { | |
| sub(/guest ok = no/, "guest ok = yes") | |
| } | |
| if (section == "printers_share" && /read only = yes/) { | |
| sub(/read only = yes/, "read only = no") | |
| } | |
| } | |
| ' /etc/samba/smb.conf > /tmp/smb.conf.tmp && sudo mv /tmp/smb.conf.tmp /etc/samba/smb.conf | |
| # Restart Samba service. | |
| sudo systemctl restart smbd | |
| echo "Automated Portion Done..." | |
| echo "Now you can proceed to Step 5: On CUPS Config Web Site..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment