Piping password to smbpasswd
Asked Answered
M

8

37

How can I pipe the new password to smbpasswd so I can automate my installation process.

Manuscript answered 15/8, 2008 at 6:8 Comment(0)
M
52

Thanks to Mark I found the answer:

(echo newpassword; echo confirmNewPassword) | smbpasswd -s

BTW: (echo oldpasswd; echo newpasswd) | smbpasswd -s does not work.

Manuscript answered 15/8, 2008 at 6:38 Comment(2)
-1 This is exactly the answer of Mark, just changing variable names.Diptych
+1 to offset JorgeeFG; I think that the new variable names contain a distinction, if you read closely. Mark's answer implies that smbpasswd is expecting an old password and a new one, but this answer implies that smbpasswd is expecting a password and a password confirmation. The second interpretation is the correct one, and so I feel Mark's answer is less helpful than this one.Anitraaniweta
V
28

I use the following in one of my scripts:

   echo -ne "$PASS\n$PASS\n" | smbpasswd -a -s $LOGIN

With echo:

-e : escape sequences, like \n

-n : don't add implicit newline at end

With smbpasswd:

-a : add new user

-s : silent

Vesiculate answered 15/9, 2008 at 13:20 Comment(3)
or echo -ne "$PASS\n" | tee - | smbpasswd -a -s $LOGIN?Disproportionation
smbpasswd’s -s stands for “use stdin for password prompt”, not “silent”Sprite
@Sprite according to samba.org/samba/docs/man/manpages/smbpasswd.8.html it means both "silent" (no prompts) and "read from stdin" (instead of /dev/ptty)Vesiculate
D
13

Try something like this:

(echo oldpasswd; echo newpasswd) | smbpasswd -s
Druse answered 15/8, 2008 at 6:31 Comment(0)
I
5

Use this echo 'somepassword' | tee - | smbpasswd -s

Ion answered 4/11, 2013 at 14:47 Comment(2)
thanks, fwiw that's the only syntax I could get working in a Dockerfile: RUN PASS=myrootpassword ; echo ${PASS} | tee - | smbpasswd -a -sAirlia
I also found this answer useful as the only syntax I could get working in a Puppet exec resource; the approach beginning with a paren failed because Puppet couldn't find the command "(echo". Thank you!Omalley
P
2

This unfortunately is not desirable for two reasons: 1) if the user uses a combination of '\n' in the password there will be a mismatch in the input 2) if there are unix users on the system, then a user using the utility ps may see the password

A better way would be to put the names in a file and read from the file and use python pexpect to read them, not like below, but the simple script is enough to see how to use pexpect

#!/usr/bin/python
#converted from: http://pexpect.sourceforge.net/pexpect.html
#child = pexpect.spawn('scp foo [email protected]:.')
#child.expect ('Password:')
#child.sendline (mypassword)
import pexpect
import sys
user=sys.argv[1]
passwd=sys.argv[2]
child = pexpect.spawn('/usr/bin/smbpasswd -a '+str(user))
child.expect('New SMB password:')
child.sendline (passwd)
child.expect ('Retype new SMB password:')
child.sendline (passwd)

then try: ./smbpasswd.py userName1 'f#@(&*(_\n895'

Priesthood answered 5/4, 2013 at 18:37 Comment(0)
O
2

I had to create a new Samba user in a Puppet 5.x Exec resource and for various reasons none of the above worked. Fortunately this rather silly-looking command worked:

yes vagrant|head -n 2|smbpasswd -a -s vagrant

Password here is of course "vagrant".

Orthocephalic answered 22/11, 2018 at 9:56 Comment(0)
N
0

You don't need to use smbpasswd. You can take your new password and calculate its NT hash (encode with UTF-16-LE and calculate the MD4 digest), encode it as a hexadecimal string, and then run pdbedit -u USER --set-nt-hash=HASH.

Nieves answered 25/5 at 13:11 Comment(0)
M
-4

using either pipelines or redirection.

Mosemoseley answered 15/8, 2008 at 6:16 Comment(1)
This simply doesn't answer the question.Pause

© 2022 - 2024 — McMap. All rights reserved.