0

Try as I might, I am unable to execute the lpstat and lp commands of CUPS remotely using sshpass in my script. I am able to execute all of the lines involving these commands when using an interactive terminal on the server running CUPS. Any help would be greatly appreciated.

Input:

./cups_print_job.sh printer ./file.pdf 1

cups_print_job.sh:

#!/bin/bash

#about
#script to print a file using CUPS via the command line. more options at https://www.cups.org/doc/options.html

#directions
#following the script name, arg1 = ~/.ssh/config host running CUPS; arg2 = path to file; arg3 = page range (e.g. 1-4,7,9-12)

#definitions
server=$1
path=$2
filename=$(basename $path)
range="page-ranges=$3"

#file transfer and printing

read -s -p "Password for CUPS: " password

sshpass -p $password scp $path $server:/tmp
sshpass -p $password ssh -o StrictHostKeyChecking=no $server /bin/bash << EOF

#definitions
printer_name=$(/usr/bin/lpstat -p -d | head -n1 | awk -F ' ' '{print $2}')
options="-o fit-to-page -o media=Letter -o $range"

#print job and cleanup
/usr/bin/lp -d $printer_name $options /tmp/$filename
echo $password | sudo -S rm -r /tmp/$filename

EOF

echo
echo 'Print job sent'
echo

Output:

Password for CUPS: ./cups_print_job.sh: line 20: /usr/bin/lpstat: No such file or directory
/usr/bin/lp: No such file or directory
[sudo] password for print:
Print job sent

~/.ssh.conf contents:

Host printer
        User print
        Hostname 192.168.0.16
        Port 22
  • 1
    The substitution printer_name=$(…) would be evaluated on the local machine anyway, wouldn't it? (The thing can see $2, after all.) – Ulrich Schwarz Aug 20 '18 at 15:19
  • @UlrichSchwarz printer_name=$(…) should be evaluated on the remote, since it's inside of the heredoc, the contents of which are passed to the remote bash. – marktimsmith Aug 20 '18 at 15:47
  • @UlrichSchwarz upon further investigation of the heredoc documentation and enclosing my opening EOF in double quotes, it works! thanks for your help – marktimsmith Aug 20 '18 at 16:03

1 Answers1

0

Thanks to @UlrichSchwarz for pointing me in the right direction. After learning that without quotes around the initiating EOF the heredoc body is processed locally, I added the quotes and the script runs as expected.

The correction:

sshpass -p $password ssh -o StrictHostKeyChecking=no $server /bin/bash << "EOF"