0

New, so apologies for the wording. Essentially, I'm trying to run a script that contains a sudo command. It's part of a larger process that I'm trying to automate. Is there any way to respond to the password request within the script? I've tried a few things / googling but I haven't had any luck

This is what i tried

sshpass -p $password ssh -t $username@$ip "echo $password |  sudo -S  docker load < testimage.tar"

when i run this the docker command fails with ,"incorrect password"

I appreciate any time or guidance, thanks!

Toto
  • 89,455
  • 62
  • 89
  • 125
  • In general, don't -- configure `sudo` to not need a password. Or, here, use a RSA key that has explicit permission to run only `docker load`. You need to configure sudo to be able to operate without a TTY for `echo "$password" | sudo -S` to work, so if you're going to change its configuration, why not just configure it to not _need_ a password at all? – Charles Duffy Jun 01 '21 at 14:12
  • Note that your current code exposes your `$password` to everyone with access to the machine where you run `ssh`, because full process command lines are world-readable. That's less unsafe for just `echo "$password" | sudo -S`, because when you run that directly the `echo` is a builtin and doesn't have its own process table entry, but when you put the whole thing in `ssh` the password shows up in the ssh command line itself. – Charles Duffy Jun 01 '21 at 14:13
  • See also [BashFAQ #69](https://mywiki.wooledge.org/BashFAQ/069). But with respect to the better approach, setting up an RSA key configured to only run the specific command you want to allow -- run `man sshd`, and search for the *AUTHORIZED_KEYS FILE FORMAT* section, and look at the `command=` parameter. – Charles Duffy Jun 01 '21 at 14:15

0 Answers0