1

I'm on the machine A, and I want to run a bash script from A to copy some files from B via scp. Those files are stored in a path that needs root access to be accessed but root remote access is disabled for ssh in B. I cannot change that configuration.

Standard scp statements do not work since I'm not asked to type B's root password during scp execution on A.

How can I do?

floatingpurr
  • 455
  • 1
  • 6
  • 9

1 Answers1

0

I could get this to work on my box, but I'm not sure if sudoers option requiretty would break it.

On machine B create program that sudo will use to ask for passwords. e.g. /home/myname/askpass.sh, chmod it +x

#!/bin/bash
echo "my_password"

On machine A create connect script that will establish the ssh connection for the scp and inject sudo + related commands into remote command. e.g. call it ./fakessh.sh, chmod it +x

#!/bin/bash

oldargs=( $@ )

newargs=( )

while : ; do
  [ "${oldargs[0]}" == "scp" ] && break

  newargs+=( "${oldargs[0]}" )
  oldargs=( "${oldargs[@]:1}" )
done

newargs+=( 'export SUDO_ASKPASS=/home/myname/askpass.sh' \; exec sudo -A -- "${oldargs[@]}" )

exec ssh "${newargs[@]}"

Note that the script above references /home/myname/askpass.sh, update path as required.

Now run scp almost normally:

scp -S ./fakessh.sh user@hostname:/etc/shadow ./

This does work for me. YMMV.

I can think of other hacks if you can run something like ncat or socat with sudo, and point scp at the custom listening port, but that's getting silly from security point.

AnyDev
  • 739
  • 8
  • 20
  • Ok but in that way I have to type unencrypted password in the file – floatingpurr Jul 19 '16 at 10:51
  • 1
    @superciccio14 yes, example as it was written is far from great. You can get /home/myname/askpass.sh to read password from a named pipe and supply the password to that named pipe in another ssh session. You probably could pass the password via environment, which is a little better than saving it in a file but still not as good as with a named pipe because it will remain in memory longer and be readable by ps and such tools. I'm afraid if box is not under your control then no method is safe. – AnyDev Jul 20 '16 at 00:51