1

I'm trying to connect to various servers to change the password of a specific user.

I need to build a Script to do that thing, the complexity is that I have to use sudo and send the password in order to connect on the remote server as root.

I'm trying something like this but is not working:

sshpass -p pass ssh user@server << EOF
  echo pass | sudo -S -u root "echo 'user2:pass2' | chpasswd"
EOF

To better explain what I want to do, these are the steps that I need to do:

  1. connect to a server with my user.
  2. using "sudo" to connect as a root.
  3. execute "echo 'user2:pass2' | chpasswd" in order to change the password of the user2.

This is the error:

Pseudo-terminal will not be allocated because stdin is not a terminal. stat: cannot stat `pipe:[2670580091]': No such file or directory [sudo] password for user1: sudo: echo 'user2:XXXXXXX' | chpasswd: command not found

2
  • 2
    Try adding -t to the ssh command to force it to allocate a tty which sudo requires to enter a password. It would also be helpful to update your answer with how/why that particular command fails (error, hangs, no output etc). Commented Jan 8, 2018 at 22:16
  • Why not using Ansible? It only requires to have Python installed on the remotes. And you even don't need to write a playbook for running a single operation. Commented Dec 7, 2022 at 14:58

3 Answers 3

1

The ansible module "user" should work for your problem. But you have to provide the already crypted password-hash instead. Make the hash with:

mkpasswd --method=sha-512

Execute a ansible adhoc-command like this with the quoted generated hash-string:

ansible -v all -i <hostname>, --become --extra-vars 'ansible_become_pass=<sudo password> ansible_password=<sudo password>' --user=<ssh user> -k -m user --args='name=root update_password=always password="$6$IZjuXoio1$zHpQQDZGDPwG8mr2R6Mrt1C8Nqstui75enT/o0oSVJ3M6rqff8993kmAaTgbc9q9HTgPD2jtZukEqgeIGKfUN0"'

Prerequesites: ansible and sshpass on client machine.

0

I've executed things as root over ssh using:

ssh -t  you@server <<EOF 
  echo pass_for_you | sudo -S bash -c "commands to do as root";   
EOF 
0

would this help ? or something similar ? assuming I understood what you are trying to do ?

for i in server{1,2,3}; do
   ssh -t user@$i 'sudo passwd user2'
done
2
  • I can't do that because the server have policies where the only user can execute passwd is root. In order to change the password I have to do the next steps: 1) connect to a server with my user 2) using "sudo" to connect as a root 3) execute "echo 'user2:pass2' | chpasswd" in order to change the password of the user2
    – Jose
    Commented Jan 9, 2018 at 13:22
  • for i in server{1,2,3}: do ssh -t user@$i 'sudo su - ; passwd user2 ' done cant remember if that prompts for password or not
    – ssvegeta96
    Commented Jan 9, 2018 at 16:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .