使用 sshpass 时找不到用绝对路径指定的可执行文件

使用 sshpass 时找不到用绝对路径指定的可执行文件

尽我所能,我无法在脚本中使用 sshpass 远程执行 CUPS 的lpstat和命令。lp在运行 CUPS 的服务器上使用交互式终端时,能够执行涉及这些命令的所有行。任何帮助将不胜感激。

输入:

./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

输出:

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 内容:

Host printer
        User print
        Hostname 192.168.0.16
        Port 22

答案1

感谢@UlrichSchwarz 为我指明了正确的方向。后学习如果启动EOFheredoc主体周围没有引号,则在本地处理,我添加了引号,并且脚本按预期运行。

更正:

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

相关内容