-1

I am trying to write a bash script to login to ssh and then run docker login command as sudo.

SSH login works fine , but issue is with sudo login. Command:

sshpass -p password ssh -oStrictHostKeyChecking=no -oServerAliveInterval=120 myuser@172.2.14.1 -p 2222 "echo password | sudo -S - docker login -u=myuser -p='dockerpa#$%' dockerhub.com/docker-repo"

Also tried

sshpass -p password ssh -tt -oStrictHostKeyChecking=no -oServerAliveInterval=120 myuser@172.2.14.1 -p 2222 "echo password | sudo -S - docker login -u=myuser -p='dockerpa#$%' dockerhub.com/docker-repo"

Error being:

[sudo] password for myser: sudo: docker: command not found

If I execute sudo docker login -u=myuser -p='dockerpa#$%' dockerhub.com/docker-repo directly on the remote server, it works fine.

What am I missing here ? Thank you.

Chel MS
  • 386
  • 1
  • 10
  • 1
    Where is the "docker" command installed on the remote system? How is that directory added to your (sudo) command path on the remote system? – Kenster Sep 26 '22 at 19:47

1 Answers1

0

My 2 cents here but shouldn`t you use ?

sshpass -p password ssh -tt -oStrictHostKeyChecking=no -oServerAliveInterval=120 myuser@172.2.14.1 -p 2222 "echo password | sudo -S docker login -u=myuser -p='dockerpa#$%' dockerhub.com/docker-repo"

without the - because:

 -S          The -S (stdin) option causes sudo to read the password from
                the standard input instead of the terminal device.

Later edit: The issue is from docker login , as you use it will prompt for password, you should use:

sshpass -p password ssh -tt -oStrictHostKeyChecking=no -oServerAliveInterval=120 myuser@172.2.14.1 -p 2222 "echo password | sudo -S echo 'dockerpa#$%' | docker login --username myuser -password-stdin dockerhub.com/docker-repo"

You can find an example for the non-interactive docker login in the documentation: https://docs.docker.com/engine/reference/commandline/login/

Claudiu T
  • 256
  • 1
  • 5
  • Without `-S` it prompts for a password, which is not possible while running a script – Chel MS Sep 26 '22 at 18:42
  • I mean remove only only the the minus from "-S -" . You use only the -S flag. I have removed it in my original answer – Claudiu T Sep 26 '22 at 18:44
  • Nope, its prompting for password then – Chel MS Sep 26 '22 at 18:47
  • Tested a little , youtr issue is from docker login , use: sudo -S echo 'dockerpa#$%' | docker login --username myuser -password-stdin docerhub.com/docker-repo' . The ideea is that -p= or --password in docker login will prompt for password. – Claudiu T Sep 26 '22 at 19:01