Saturday, August 26, 2017

Remote execution needing root access

Using a program on a remote system that needs root privileges and transferring the output to the local system.

The problem:
My scanner is attached to an old computer, let's call it remote-host.  The installed program that runs it needs root privileges, and outputs a PNM image.  (It needs to be run with sudo because the manufacturer did not supply a Linux driver that handled the scanner, and some kind member of the Ubuntu community wrote one and cut some corners.)
I want a single command that I can run on another computer, say local-host, that will scan whatever is on the scanner plate, and save the result as a JPEG in a specified file.

This solution worked:

I made a bash script, scanto, taking one argument
--- the path to the ultimate JPEG file, without
the .jpg extension, and I put it in my path.
Thus, for instance, the terminal command:
scanto fan
saves the scanned image in the file fan.jpg


Here are the contents of the script:

******************************************

#!/bin/bash
# file scanto
# AOF 25-8-2017

[ $# -eq 0 ] \
        && echo Needs an argument  \
        && echo -- an output filename, without extension \
        && exit

echo WARNING:
echo Existing files $1.pnm and $1.jpg will be overwritten!
echo Type ctrl-C next if you don't want that to happen.

echo Preparing to scan on remote-host
echo You will be asked for your privileged password
echo '#!/bin/bash' > scan.sh
echo 'sudo scanimage > ./scratch/scans/temp.pnm' >> scan.sh
ssh myname@remote-host 'bash -s' < scan.sh
echo page scanned to file scratch/scans/temp.pnm on remote-host

echo Preparing to transfer the file from remote-host
echo You will be asked again for your privileged password
scp myname@remote-host:scratch/scans/temp.pnm $1.pnm
echo scanned page copied to $1.pnm

echo Making a JPEG version:
convert $1.pnm $1.jpg
echo JPEG version now in $1.jpg

********************************

The only essential lines are:

echo '#!/bin/bash' > scan.sh
echo 'sudo scanimage > ./scratch/scans/temp.pnm' >> scan.sh
ssh myname@remote-host 'bash -s' < scan.sh
scp myname@remote-host:scratch/scans/temp.pnm $1.pnm
convert $1.pnm $1.jpg


The first two lines create a shell script, scan.sh
The third line uses ssh (secure shell) to execute scan.sh on the remote system, with root privileges.
The fourth line uses scp (secure copy) to copy temp.pnm to the local system.
The last line uses the convert utility to make a JPEG version.

The script also leaves a PNM version on the local system. This is the raw scan, and is useful if I want to use gimp to modify the image.  Add a line with rm $1.pnm  to the script if you don't want this file.  The script also leaves behind a copy of the scan on the remote host, but that file is overwritten each time scanto is called.

I have to be a sudoer on the remote-host, and I have to give my password twice, as the script executes.  I could not see a secure way to give it just once.
                                                           

No comments:

Post a Comment