使用 Python 在 SAMBA 上更改用户密码 [英] Change an user password on SAMBA with Python

查看:103
本文介绍了使用 Python 在 SAMBA 上更改用户密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用 Python 编写一个脚本来更改 SAMBA 服务器上的用户密码.我从来没有学过 Python,现在我在我的 VirtualBox 和 Ubuntu 上尝试了不同的东西.使用此代码,我可以更改(或更好地删除并设置新)VirtualBox 中用户的密码.但它也适用于服务器吗?我没有任何线索.请帮我.谢谢!

I shall write a script using Python to change an user password on a SAMBA server. I never learned Python and now I tried different things on my VirtualBox with Ubuntu. With this code I can change (or better delete and set a new) password of an user in my VirtualBox. But will it also work on the Server? I have no clue. Please help me. Thank you!

from subprocess import Popen

uname = raw_input("Username: ")
proc = Popen(['/usr/bin/sudo', '/usr/bin/passwd', uname, '--d'])
proc.communicate()
proc = Popen(['/usr/bin/sudo', '/usr/bin/passwd', uname])
proc.communicate()

推荐答案

user2960552 的解决方案在没有用户交互的情况下不起作用.以下修复:

user2960552's solution doesn't work without user interaction. The following fixes that:

def create_or_update_samba_user(username, password):
    proc = subprocess.Popen(['sudo', 'smbpasswd', '-a', username], stdin=subprocess.PIPE)
    proc.communicate(input=password + '\n' + password + '\n')

这篇关于使用 Python 在 SAMBA 上更改用户密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆