0

I want to create and assign a password for a user in Linux, but it gives an error:

New password: Retype new password: Sorry, passwords do not match

Here is the script (executing with root):

from os import system
system('useradd user')
system('echo -e "pass\npass\n" | passwd user')
  • I wrote some script like 2 months ago everything was working fine besides if you execute the same commands in bash password changes successfully, now I opened it and this error occurred, no idea what's the problem and how to fix it – Pole Boroni Jul 27 '21 at 08:38
  • Try to separate the `system` calls by the `\n` like this: `system("echo -e pass")` and then again `system("echo -e pass")`. – Nastor Jul 27 '21 at 08:38
  • It needs to be executed simultaneously with ```passwd``` – Pole Boroni Jul 27 '21 at 08:40
  • 1
    Try to use [expect](https://stackoverflow.com/questions/4780893/use-expect-in-a-bash-script-to-provide-a-password-to-an-ssh-command) if its possible. Or try `\r` instead of `\n`. – Albin Paul Jul 27 '21 at 08:44

1 Answers1

0

God damn it :) So tried debugging for some reason echo returned -e pass\n on the first try. Removed -e argument completely so it's system('echo "pass\npass\n" | passwd user') and it's working now.

  • `-e` should be fine. If `echo` actually outputs the _-e_, I would conclude that you didn't pick up the "right" echo. What happens if you go back to your original code, but explicitly invoke `/usr/bin/echo -e ....`? – user1934428 Jul 27 '21 at 09:08
  • You are right ```/usr/bin/echo -e``` works too. – Pole Boroni Jul 27 '21 at 09:35
  • 1
    If this would be my platform, I would investigate some time to find out **which** `echo` you are executing. My feeling is that you are running dash and use the `echo` which is built into it. – user1934428 Jul 27 '21 at 09:45
  • I ran it on several OS with the same result so that's why it was bugging me. – Pole Boroni Jul 27 '21 at 12:01