Python函数仅在打印时起作用
这是为sftp身份验证创建unix凭据的代码。我注意到奇怪的症状,如果我在终端上运行,一切都正常,它输出的用户名和密码在sftp上工作。但是如果我删除了两行“print(stdout.read())”,那么这个功能就不能完全工作。帐户已设置,但密码未进行身份验证。不确定“chpasswd”行是否有问题 如果没有实际打印出标准输出,会导致代码失败(即使它看起来工作正常并且没有遇到异常),那么会出现什么问题呢 以下是我关注的几行:Python函数仅在打印时起作用,python,Python,这是为sftp身份验证创建unix凭据的代码。我注意到奇怪的症状,如果我在终端上运行,一切都正常,它输出的用户名和密码在sftp上工作。但是如果我删除了两行“print(stdout.read())”,那么这个功能就不能完全工作。帐户已设置,但密码未进行身份验证。不确定“chpasswd”行是否有问题 如果没有实际打印出标准输出,会导致代码失败(即使它看起来工作正常并且没有遇到异常),那么会出现什么问题呢 以下是我关注的几行: stdin, stdout, stderr = client
stdin, stdout, stderr = client.exec_command("adduser --disabled-password --gecos '' {}".format(ftp_username))
print(stdout.read())
stdin, stdout, stderr = client.exec_command("echo '{}:{}' | chpasswd".format(ftp_username, new_pass))
print(stdout.read())
这是整个代码:
def create_user(ftp_username):
client = connect_paramiko()
new_pass = passgen.passgen()
appendSSH = '''
Match User {}
ForceCommand internal-sftp
PasswordAuthentication yes
ChrootDirectory /var/sftp7/{}
PermitTunnel no
AllowAgentForwarding no
AllowTcpForwarding no
X11Forwarding no
'''.format(ftp_username, ftp_username)
client.exec_command("mkdir -p /var/sftp7/{}/{}".format(ftp_username, ftp_username))
client.exec_command("chown root:root /var/sftp7/{}".format(ftp_username))
client.exec_command("chown root:root /var/sftp7")
client.exec_command("chmod 755 /var/sftp7/{}".format(ftp_username))
stdin, stdout, stderr = client.exec_command("adduser --disabled-password --gecos '' {}".format(ftp_username))
print(stdout.read())
stdin, stdout, stderr = client.exec_command("echo '{}:{}' | chpasswd".format(ftp_username, new_pass))
print(stdout.read())
client.exec_command("chown {}:{} /var/sftp7/{}/{}".format(ftp_username, ftp_username, ftp_username, ftp_username))
client.exec_command("echo '{}' >> /etc/ssh/sshd_config".format(appendSSH))
client.exec_command("systemctl restart sshd")
client.exec_command("usermod {} -d /{}".format(ftp_username, ftp_username))
return {"username":ftp_username, "password":new_pass
以及:
第一行,它只是创建没有任何密码的用户
stdin, stdout, stderr = client.exec_command("adduser --disabled-password --gecos '' {}".format(ftp_username))
print(stdout.read())
在第二行中,我们echo username:password
,结果被传递到命令chpasswd
。之所以这样做,是因为当您运行命令chpasswd
时,它将请求username:password
。因此,如果我们将echousername:password
的结果传递给chpasswd
命令,它将不会要求进一步的输入
stdin, stdout, stderr = client.exec_command("echo '{}:{}' | chpasswd".format(ftp_username, new_pass))
print(stdout.read())
更多
只需打开终端,首先必须运行以下命令:
man adduser
如果运气好的话,你会发现的。然后,在该手动页面中键入:/--disabled login,然后按Enter,然后重复按n以查找包含--disabled login的所有事件。对--gecos执行相同操作
运气好的话,你会发现:
--disabled-login
Do not run passwd to set the password. The user won't be able
to use her account until the password is set.
您得到的错误是什么?您能提供堆栈跟踪吗?@RishikeshJha如前所述,它不会出错,也不会命中异常。这两次看起来都执行得很好,但是如果排除print语句,则新创建的unix用户不会使用该密码进行身份验证。如果我把打印行放回去,一切都正常。我猜,但是如果你处理管道和stdout,我对
read()
没有任何了解。。如果没有人连接到读取,则进程从不写入,并且可能在I/O调用(写入)中被阻止。。。然后您的整个脚本完成并带上它的子脚本(包括对其他程序的调用)。换句话说。。。如果你想保持代码不变。。。尝试读取stdout
,但要么将其分配给var,要么放弃它。如果这有帮助的话,我很可能走上了正确的道路。如果没有,我已经试过了。:)哦,如果在前面的评论中正确的话,另一个选项似乎是,因为那些.exec_command
方法似乎支持重定向输出(到文件或/dev/null
)。我在代码中添加了这样的选项,您可以看到connect_paramiko做了什么这是一个答案吗?解决办法是什么?它只是解释了我所拥有的,除非我遗漏了什么。我只是告诉你这两行代码在做什么。不知道你的代码在做什么,你怎么能期望它按照你的要求工作呢。我仍然很困惑,为什么你要删除这两行代码。那么,为什么当我删除打印行时,代码不起作用呢?你读了答案了吗,这就是我写的。这些to行用于为用户设置登录名和密码。第一行确保没有设置密码,第二行手动设置密码。这不是答案。
--disabled-login
Do not run passwd to set the password. The user won't be able
to use her account until the password is set.